ERR_REQUIRE_ESM
wdamaral opened this issue · 1 comments
Hey folks,
I'm playing around with formidable. More like learning its details than anything, as I'm fairly new to Typescript.
I'm facing with an issue that might be a bug or some sort of incompatibility that I can't figure out the reason.
Am I doing something wrong here?
The error states:
C:\Users\wfura\Documents\Projects\formidable\node_modules\ts-node\dist\index.js:842
return old(m, filename);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\wfura\Documents\Projects\formidable\node_modules\formidable\src\index.js from C:\Users\wfura\Documents\Projects\formidable\src\app.ts not supported.
Instead change the require of index.js in C:\Users\wfura\Documents\Projects\formidable\src\app.ts to a dynamic import() which is available in all CommonJS modules.
at require.extensions. [as .js] (C:\Users\wfura\Documents\Projects\formidable\node_modules\ts-node\dist\index.js:842:20)
at Object. (C:\Users\wfura\Documents\Projects\formidable\src\app.ts:7:38)
at m._compile (C:\Users\wfura\Documents\Projects\formidable\node_modules\ts-node\dist\index.js:848:29)
at require.extensions. [as .ts] (C:\Users\wfura\Documents\Projects\formidable\node_modules\ts-node\dist\index.js:850:16)
at phase4 (C:\Users\wfura\Documents\Projects\formidable\node_modules\ts-node\dist\bin.js:414:16)
at bootstrap (C:\Users\wfura\Documents\Projects\formidable\node_modules\ts-node\dist\bin.js:49:12)
at main (C:\Users\wfura\Documents\Projects\formidable\node_modules\ts-node\dist\bin.js:32:12)
at Object. (C:\Users\wfura\Documents\Projects\formidable\node_modules\ts-node\dist\bin.js:526:5) {
code: 'ERR_REQUIRE_ESM'
}
I only have formidable on this project with express. My package.json is as below.
{
"name": "formidable",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon ./src/app.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.1",
"formidable": "^3.2.4"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/formidable": "^2.0.5",
"@types/node": "^17.0.41",
"nodemon": "^2.0.16",
"ts-node": "^10.8.1",
"typescript": "^4.7.3"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"esModuleInterop": true
}
}
Finally, the single file in this project.
import express from "express";
import formidable from "formidable";
const app: express.Application = express();
app.post("/file-upload", (req, res, next) => {
const form = formidable({});
console.log(req);
form.parse(req);
form.on("fileBegin", (name, file) => {
console.log(file)
});
form.on("file", (name, file) => {
console.log(name)
});
res.sendStatus(200)
});
app.listen(3000, () => console.log("listening on 3000"));
@wdamaral hello there.
For now the v3 version is only ESM compatible (and not CJS/CommonJS, e.g. with require
s), so you need to make your project accepting ES Modules. That's by adding "type": "module"
in your package.json.