raydium-sdk icon indicating copy to clipboard operation
raydium-sdk copied to clipboard

How to use this SDK from NodeJS? I got: The requested module '@raydium-io/raydium-sdk' is a CommonJS module, which may not support all module.exports as named exports

Open francestu96 opened this issue 1 year ago • 19 comments

When trying to import some package from the SDK, i got the error.

I have main.mjs file and I run it with: node ./main.mjs. It starts with: import { Liquidity } from '@raydium-io/raydium-sdk';

It throw the error:

SyntaxError: Named export 'Liquidity' not found. The requested module '@raydium-io/raydium-sdk' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@raydium-io/raydium-sdk';
const { Liquidity } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

I also tried the suggestions and import it with:

import pkg from '@raydium-io/raydium-sdk';
const { Liquidity } = pkg;

Yet, other errors occur.

How can I use it??

francestu96 avatar Jan 19 '24 20:01 francestu96

I have the similar problem. I am new on typescript/nodejs.

I found an explanation here

https://stackoverflow.com/questions/71022803/the-requested-module-does-not-provide-an-export-named-default-error-but

but I don't know how to solve this issue.

My error is a bit different than yours

`d:\Projects\Ongoing\Temp Projects\raydium-sdk-V1-demo-mastersrc/raydium-test.ts:29 const slippage = new Percent(1, 100); ^

SyntaxError: The requested module './node_modules/@raydium-io/raydium-sdk/lib/esm/index.js' does not provide an export named 'Percent' at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21) at async ModuleJob.run (node:internal/modules/esm/module_job:214:5) at async ModuleLoader.import (node:internal/modules/esm/loader:329:24) at async loadESM (node:internal/process/esm_loader:28:7) at async handleMainPromise (node:internal/modules/run_main:113:12) `

ertan2002 avatar Jan 21 '24 13:01 ertan2002

I have this exact issue. still searching for a solution.

MAXDCML avatar Jan 24 '24 19:01 MAXDCML

same issue

(node:81348) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/node_modules/@raydium-io/raydium-sdk/lib/esm/index.js:1
export * from './base';
^^^^^^

SyntaxError: Unexpected token 'export'
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1149:20)
    at Module._compile (node:internal/modules/cjs/loader:1190:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

Node.js v18.14.0

kelvinkn17 avatar Feb 02 '24 22:02 kelvinkn17

1.2.0-beta.8 was the last version that worked Roll back using npm i @raydium-io/[email protected] and the problem will be solved

Inffix avatar Feb 17 '24 21:02 Inffix

1.2.0-beta.8 was the last version that worked Roll back using npm i @raydium-io/[email protected] and the problem will be solved

image

tculig avatar Feb 22 '24 13:02 tculig

npm i @raydium-io/[email protected]

Same issue here, reverting back to old version worked

RyanWilkins avatar Mar 09 '24 04:03 RyanWilkins

wtf

gizzye avatar Mar 20 '24 14:03 gizzye

fucking shit lol

gusarsleeping186 avatar Mar 25 '24 13:03 gusarsleeping186

same issue

daron4ever avatar Apr 03 '24 00:04 daron4ever

same, please fix

Deezzir avatar Apr 04 '24 22:04 Deezzir

Same issue, both tested with both node 18 and 20 Tested versions: "@raydium-io/raydium-sdk": "^1.3.1-beta.45" "@raydium-io/raydium-sdk": "^1.3.1-beta.51"

to fix this I have switched to the older version mentioned above. "@raydium-io/raydium-sdk": "1.2.0-beta.8",

err0r403 avatar Apr 09 '24 03:04 err0r403

tsconfig setup correctly? what OS?

benjiqq avatar Apr 25 '24 07:04 benjiqq

You can use this as fix, clone this branch local, build app and use npm link to import that

https://github.com/raydium-io/raydium-sdk/pull/79

ibockowsky avatar Apr 25 '24 08:04 ibockowsky

@ibockowsky Hi, I still have this problem with the latest version sdk: 1.3.1-beta.52. I found in this version, you pr is merged and published. Please help, thanks

  • [x] node.js: v20.10.0
  • [x] my package.json:
{
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "type": "module",
  "imports": {
    "#src/*": "./src/*"
  },
  "dependencies": {
    "@metaplex-foundation/mpl-token-metadata": "2.5.2",
    "@raydium-io/raydium-sdk": "^1.3.1-beta.52",
    "@solana/spl-token": "^0.4.6",
    "@solana/web3.js": "^1.91.7",
    "axios": "^1.6.8",
    "better-sqlite3": "^9.6.0",
    "bip32": "^4.0.0",
    "bip39": "^3.1.0",
    "bs58": "^5.0.0",
    "dotenv": "^16.4.5",
    "node-cache": "^5.1.2",
    "notify_bots": "^1.1.1",
    "pino": "^9.0.0",
    "pino-pretty": "^11.0.0",
    "promise-retry": "^2.0.1",
    "ts-node": "^10.9.2",
    "typescript": "^5.4.5",
    "ws": "^8.17.0"
  }
}
  • [ ] Got error:
  LIQUIDITY_STATE_LAYOUT_V4,
  ^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'LIQUIDITY_STATE_LAYOUT_V4' not found. The requested module '@raydium-io/raydium-sdk' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@raydium-io/raydium-sdk';

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.10.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Sylor-huang avatar May 03 '24 13:05 Sylor-huang

Use commonjs or bun, seems to work....but yeah esm needs to be fixed

ducktype avatar May 03 '24 14:05 ducktype

deno with web3js 2.0 can also be made to work

benjiqq avatar May 03 '24 15:05 benjiqq

Thanks. Change to commonjs may be a huge project. hhhhh

Sylor-huang avatar May 04 '24 01:05 Sylor-huang

I tried the latest version of @raydium-io/raydium-sdk 1.3.1-beta.52 that includes the fix from pullrequest #79 but it still did not work for me.

I was able to get around the problem by modifying file node_modules/@raydium-io/raydium-sdk/package.json and removing the following section from the file:

 "exports": {
   "require": "./lib/cjs/index.js",
   "import": "./lib/esm/index.js"
 },

After doing that change I was able to do ESM imports:

import { LIQUIDITY_STATE_LAYOUT_V4  } from "@raydium-io/raydium-sdk";

rtamin avatar May 09 '24 02:05 rtamin

will this be fixed?

softwarecurator avatar May 22 '24 05:05 softwarecurator