-
Notifications
You must be signed in to change notification settings - Fork 0
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
Can't import #1
Comments
Do you have a custom start command? |
{
"compilerOptions": {
"target": "es2021",
"module": "es2022",
"lib": ["es2021"],
"declaration": true,
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./src",
"strict": false,
"moduleResolution": "node",
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true
},
"exclude": ["dist", "node_modules", "tests"]
} |
I've been playing with this, and it's definitely a CJS vs ESM problem. But I'm not sure why yet. When I try to launch my built package via node I get: import { Gradient } from '@gradientai/nodejs-sdk';
^^^^^^^^
SyntaxError: Named export 'Gradient' not found. The requested module '@gradientai/nodejs-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 '@gradientai/nodejs-sdk';
const { Gradient } = pkg;
at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
at async loadESM (node:internal/process/esm_loader:91:5)
at async handleMainPromise (node:internal/modules/run_main:65:12)
Node.js v18.12.1 So for some reason it's picking it up as CJS, despite I see that your package.json is attempting to support both. |
Fwiw, I asked chatgpt, and it told me to simplify your package.json export section to just: "exports": { and this fixed it for me. |
Thanks a lot @johnflux ! Supporting both is more complex than expected, I'll try to see if your fix handles both and release a patch! |
I feel you!! |
(Closed by mistake, sorry) |
Hey @johnflux , we updated the TS example with the newer version of the file, can I ask you if it works correctly now? Here's an example I ran: https://gist.github.com/mhaligowski/0e8240ce0e45d3ee3dffc5f514a45c1c |
The npm package says to import as "@gradientai/nodejs-sdk" ( https://socket.dev/npm/package/@gradientai/nodejs-sdk ) which makes typescript happy, and I can autocomplete, but then when I do "npm start" gives the error:
"The requested module does not provide an export named "Gradient".
The example app on the other hand does:
import { Gradient } from "@gradientai/nodejs-sdk/src/gradient";
but this gives the error "Cannot find module or its corresponding type declarations"
If you look in the package.json in nodejs-sdk you can see the listed exports, and src/gradient is not listed in the exports.
The text was updated successfully, but these errors were encountered: