I'm trying to import node-fetch into my Typescript project but I fall into this error:
[ERR_REQUIRE_ESM]: Must use import to load ES Module : /Users/xxx/xxx/xxx/xxx/xxx/xxx/xxx/node_modules/node-fetch/src/index.js require() of ES module is not supportes.
This is my setup:
- node: v16.1.0
- typescript: 4.5.2
The node-fetch package is imported in my project as followed: import fetch from 'node-fetch';'
This is my tscongif.json:
{
"compilerOptions": {
"module": "CommonJS",
"sModuleInterop": true,
"target": "ES2020",
"allowJs": true,
"noImplicitAny": true,
"moduleResolution": "node",
"outDir": "dist",
"baseUrl": ".",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true,
"typeRoots": [
"src/@types/",
"node_modules/@types",
],
"strict": true,
"strictNullChecks": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noEmit": true,
"skipLibCheck": true,
},
"include": [
"src/**/*",
"config/**/*"
]
}
I tried alse to add "type": "module" into the package.json and set "module": "ES2020" into the tsconfig.json but I got a new error:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
To run my code I use nodemon configured as follow:
{
"watch": ["src"],
"ext": "ts, js, json",
"execMap": {
"ts": "NODE_ENV=development ts-node -T"
}
}