climba03003 / fastify-formidable

14 stars 3 forks source link

Error after importing SyntaxError: Cannot use import statement outside a module #61

Closed ai-leonid closed 1 year ago

ai-leonid commented 2 years ago

Hello! After importing this plugin into index.mjs file in my fastify project with esm modules, I've get this error:

(node:15992) 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)
MY_POJECT_NAME\node_modules\fastify-formidable\lib\mjs\index.js:1
import FastifyPlugin from 'fastify-plugin';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:190:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)

I think you could try, to add "type": "module", into package.json file

climba03003 commented 2 years ago

This module support CJS user. It will never add "type": "module" inside package.json.

climba03003 commented 1 year ago

@leonid-ilin I have revisit the problem. Can you check if version 3.0.1 resolve the problem?

N1kl8s commented 1 year ago

@climba03003 Hello, now we can import fastify-formidable but not use it:

file:///workspace/node_modules/fastify-formidable/lib/mjs/index.js:2
import { IncomingForm } from 'formidable';
         ^^^^^^^^^^^^
SyntaxError: Named export 'IncomingForm' not found. The requested module 'formidable' 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 'formidable';
const { IncomingForm } = 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:526:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

It works if we import formidable like this.

import pkg from 'formidable';
const { IncomingForm } = pkg;

Thank you for this package and your work!

climba03003 commented 1 year ago

@N1kl8s Can you check if version 3.0.2 fix your issue?

N1kl8s commented 1 year ago

@climba03003 Thank you very much! Now it works just fine.