raydium-io / raydium-sdk

An SDK for building applications on top of Raydium.
https://sdk.raydium.io
GNU General Public License v3.0
308 stars 125 forks source link

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 #50

Open francestu96 opened 5 months ago

francestu96 commented 5 months ago

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??

ertan2002 commented 5 months ago

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) `

MAXDCML commented 5 months ago

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

kelvinkn17 commented 5 months ago

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

Inffix commented 4 months ago

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

tculig commented 4 months ago

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

image

RyanWilkins commented 3 months ago

npm i @raydium-io/raydium-sdk@1.2.0-beta.8

Same issue here, reverting back to old version worked

gizzye commented 3 months ago

wtf

gusarsleeping186 commented 3 months ago

fucking shit lol

daron4ever commented 3 months ago

same issue

Deezzir commented 2 months ago

same, please fix

err0r403 commented 2 months ago

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",

benjiqq commented 2 months ago

tsconfig setup correctly? what OS?

ibockowsky commented 2 months ago

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

Sylor-huang commented 2 months ago

@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

{
  "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"
  }
}
  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.
ducktype commented 2 months ago

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

benjiqq commented 2 months ago

deno with web3js 2.0 can also be made to work

Sylor-huang commented 2 months ago

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

rtamin commented 1 month ago

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";
softwarecurator commented 1 month ago

will this be fixed?