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

Cannot use import statement outside a module #876

Closed
JonathanWilbur opened this issue Aug 6, 2024 · 5 comments · Fixed by #897
Closed

Cannot use import statement outside a module #876

JonathanWilbur opened this issue Aug 6, 2024 · 5 comments · Fixed by #897

Comments

@JonathanWilbur
Copy link

Hello, when importing this in a Javascript (clarifying that it is not Typescript) program, I get the following error:

(node:64227) 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)
[REDACTED]/node_modules/nsfwjs/dist/esm/index.js:46
import * as tf from "@tensorflow/tfjs";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (node:internal/modules/cjs/loader:1281:20)
    at Module._compile (node:internal/modules/cjs/loader:1321:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
    at Module.load (node:internal/modules/cjs/loader:1208:32)
    at Module._load (node:internal/modules/cjs/loader:1024:12)
    at cjsLoader (node:internal/modules/esm/translators:348:17)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:297:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:123:5)

Even if I go in nsfwjs's package.json and add "type": "module" manually, I get another strange error:

node:internal/modules/esm/resolve:265
    throw new ERR_MODULE_NOT_FOUND(
          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '[REDACTED]/node_modules/nsfwjs/dist/esm/nsfw_classes' imported from [REDACTED]/node_modules/nsfwjs/dist/esm/index.js
    at finalizeResolution (node:internal/modules/esm/resolve:265:11)
    at moduleResolve (node:internal/modules/esm/resolve:933:10)
    at defaultResolve (node:internal/modules/esm/resolve:1169:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:227:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:39)
    at link (node:internal/modules/esm/module_job:86:36) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///[REDACTED]/node_modules/nsfwjs/dist/esm/nsfw_classes'
}

My application is a single file that has an extension .mjs and I have "type": "module" in my package.json.

My suspicion is that this has been tested with (1) TypeScript that compiles to CommonJS, and (2) Javascript / CommonJS, and the ESM module never worked properly. I am not sure what it would take, though; the package.json looks correct to me.

For reference, I am also importing several other ESM modules in my main.mjs prior to this one, so this isn't a problem with loading ESM modules in general.

I am using Node.js v20.16.0.

Thanks in advance for your help. I'd be happy to offer more information or even contribute code to fix this.

@AnnikenYT
Copy link

Bump. I have the same issue on [email protected] with node v20.16.0 and SvelteKit & vite.

@ChristianVaughn
Copy link

I am having this issue with SvelteKit but using TypeScript not JavaScript. Did anyone figure out how to get this imported

@JonathanWilbur
Copy link
Author

I did this in a TypeScript project and it worked for me: import * as nsfwjs from "./node_modules/nsfwjs/dist/cjs/index.js";.

That's obviously sloppy; you shouldn't have to do it. But it works.

@ChristianVaughn
Copy link

Thanks for the temp fix @JonathanWilbur. I got it to work with that but importing the /esm/index.js. Haven't tested my full code yet, this looks more hopeful than manually importing TensorFlow and recreating the library

@HotoRas
Copy link

HotoRas commented Oct 9, 2024

Same here, but more bigger project:
image
Tried to upgrade nsfwjs to reflect the tfjs version, and boom.

// .../dist/esm/index.js
const tf = require("@tensorflow/tfjs");
const NSFW_CLASSES = require("./nsfw_classes").NSFW_CLASSES;

and

// .../dist/esm/index.js
module.exports.load = load;
module.exports.NSFWJS = NSFWJS;

and

// .../dist/esm/nsfw_classes.js
module.exports.NSFW_CLASSES = NSFW_CLASSES;

should work, but esm should be fixed to not require doing this shit.

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

Successfully merging a pull request may close this issue.

4 participants