infinitered / nsfwjs

NSFW detection on the client-side via TensorFlow.js
https://nsfwjs.com/
MIT License
7.94k stars 529 forks source link

Cannot use import statement outside a module #876

Open JonathanWilbur opened 1 month ago

JonathanWilbur commented 1 month ago

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 commented 4 weeks ago

Bump. I have the same issue on nsfwjs@4.1.0 with node v20.16.0 and SvelteKit & vite.

ChristianVaughn commented 5 days ago

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

JonathanWilbur commented 5 days ago

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 commented 4 days ago

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