Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wildlifechorus opened this issue Nov 20, 2024 · 2 comments

Comments

@wildlifechorus
Copy link

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

  • Node.js version: 18.16.0
  • @1inch/cross-chain-sdk version: 0.1.10
  • OS: macOS

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
Copy link
Member

vbrvk commented Nov 26, 2024

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

@wildlifechorus
Copy link
Author

@vbrvk 22 but I tried with several others

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants