1inch / cross-chain-sdk

MIT License
2 stars 0 forks source link

SyntaxError: Unexpected token 'export' when importing @1inch/cross-chain-sdk in Node.js #47

Open wildlifechorus opened 1 week ago

wildlifechorus commented 1 week ago

SyntaxError: Unexpected token 'export' when importing @1inch/cross-chain-sdk in Node.js

Description

I encountered issues when trying to use @1inch/cross-chain-sdk in my project. Below are the steps and errors for two different import approaches.

Attempt 1:

Using named imports:

import { SDK } from '@1inch/cross-chain-sdk';

Result:

file:///Users/wildlifechorus/Projects/tangany-circle/index.js:2
import { SDK } from '@1inch/cross-chain-sdk';
              ^^^
SyntaxError: Named export 'SDK' not found. The requested module '@1inch/cross-chain-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 '@1inch/cross-chain-sdk';
const { SDK } = pkg;

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

Attempt 2:

Switching to a default import with destructuring:

import pkg from '@1inch/cross-chain-sdk';
const { SDK } = pkg;

Result:

(node:4924) 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)
/Users/wildlifechorus/Projects/tangany-circle/node_modules/@1inch/cross-chain-sdk/dist/esm/index.js:1
export { Address, NetworkEnum, MakerTraits, Extension, AuctionDetails, SettlementPostInteractionData, Interaction, AuctionCalculator,
^^^^^^

SyntaxError: Unexpected token 'export'
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1176:20)
    at Module._compile (node:internal/modules/cjs/loader:1218:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958: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.16.0

Steps to Reproduce

  1. Install the package:
    npm install @1inch/cross-chain-sdk
  2. Attempt the following imports:

First Attempt:

import { SDK } from '@1inch/cross-chain-sdk';

Second Attempt:

import pkg from '@1inch/cross-chain-sdk';
const { SDK } = pkg;
  1. Run the file with:
    node index.js

Expected Behavior

The package should work seamlessly in Node.js without requiring additional configurations.


Actual Behavior

Both import approaches failed with the errors mentioned above.


Environment


Additional Notes

I also tried the following workarounds:

  1. Setting "type": "module" in package.json.
  2. Renaming the file to index.mjs.

However, these changes introduced other compatibility issues in my project setup.


Suggested Fix

Please ensure that the package is compatible with both CommonJS and ES module environments. This could include providing proper dual exports or clear documentation on how to configure Node.js projects to use this package.

vbrvk commented 2 days ago

@wildlifechorus do you use typescript? Which version of node you use?

wildlifechorus commented 2 days ago

@vbrvk 22 but I tried with several others