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

Can't import #1

Open
johnflux opened this issue Oct 4, 2023 · 8 comments
Open

Can't import #1

johnflux opened this issue Oct 4, 2023 · 8 comments

Comments

@johnflux
Copy link

johnflux commented Oct 4, 2023

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.

@fmmoret
Copy link

fmmoret commented Oct 4, 2023

Do you have a custom start command?
What version of the sdk is in your lock file?
What version of node are you using?
What version of typescript are you using?
What's your tsconfig & how are you compiling the target of your start command?
Or is the script a plain.js file that uses typescript just in the IDE for inferred types / jsdoc typing?

CC @mhaligowski @lilwilsond2 @malloc-error

@johnflux
Copy link
Author

johnflux commented Oct 4, 2023

Do you have a custom start command?
I guess? It's doing: npm run build && node --enable-source-maps dist/start.js

What version of node are you using?
node v18.12.1

What version of typescript are you using?
5.0.4

What's your tsconfig & how are you compiling the target of your 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"]
}

@johnflux
Copy link
Author

johnflux commented Oct 4, 2023

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.

@johnflux
Copy link
Author

johnflux commented Oct 4, 2023

Fwiw, I asked chatgpt, and it told me to simplify your package.json export section to just:

"exports": {
".": "./dist/cjs/index.js",
"./esm": "./dist/esm/index.js",
"./types": "./dist/cjs/index.d.ts",
"./types/esm": "./dist/esm/index.d.ts"
},

and this fixed it for me.

@mhaligowski
Copy link
Contributor

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!

@johnflux
Copy link
Author

johnflux commented Oct 4, 2023

Supporting both is more complex than expected

I feel you!!
Offtopic, but I am loving gradientai. Very much looking forward to playing with it some more.

@johnflux johnflux closed this as completed Oct 4, 2023
@johnflux johnflux reopened this Oct 4, 2023
@johnflux
Copy link
Author

johnflux commented Oct 4, 2023

(Closed by mistake, sorry)

@mhaligowski
Copy link
Contributor

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

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

3 participants