node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7k stars 680 forks source link

Issue with import and require statement in v3, no problem in v2 #932

Closed Shawful closed 1 year ago

Shawful commented 1 year ago

Support plan

Context

What are you trying to achieve or the steps to reproduce?

Trying to install formidable without esm errors.

What was the result you got?

Initially, my project was experiencing the following error:

Error: Cannot find module 'formidable'
Require stack:
- /home/ubuntu/lovesRealty/app/middleware.js
- /home/ubuntu/lovesRealty/app/server.js
- /home/ubuntu/lovesRealty/index.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:81:25)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/home/ubuntu/lovesRealty/app/middleware.js:8:20)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    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 Module.require (node:internal/modules/cjs/loader:1005:19)
    at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/ubuntu/lovesRealty/app/middleware.js',
    '/home/ubuntu/lovesRealty/app/server.js',
    '/home/ubuntu/lovesRealty/index.js'
  ]
}

I expected to be able to install formidable using

npm install formidable

and found that the latest version uses an import command in its index.js file, which causes my build to have issues. and errors like:

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/ubuntu/project/node_modules/formidable/src/index.js from /home/ubuntu/project/app/middleware.js not supported.
Instead change the require of index.js in /home/ubuntu/project/app/middleware.js to a dynamic import() which is available in all CommonJS modules.
    at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
    at Object.<anonymous> (/home/ubuntu/project/app/middleware.js:8:20)
    at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
    at Object.<anonymous> (/home/ubuntu/project/app/server.js:4:20)
    at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
    at Object.<anonymous> (/home/ubuntu/project/index.js:2:16)
    at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23) {
  code: 'ERR_REQUIRE_ESM'
}

What result did you expect?

I expected it to work as it did with previous versions of formidable.

BIG FYI - I found that when I rolled back to the previous version with :

npm install formidable@v2

that my server wasn't crashing anymore but wanted to let y'all know.

nicolasheady commented 1 year ago

Experiencing same thing

GrosSacASac commented 1 year ago

The problem is that your entry point is not an ES module. https://nodejs.org/api/packages.html#determining-module-system