Before adding
"type": "module",
in package.json it used to give the following error
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Work\ts-api\node_modules\node-fetch\src\index.js
require() of ES modules is not supported.
require() of C:\Work\ts-api\node_modules\node-fetch\src\index.js from C:\Work\ts-api\src\app.ts is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Work\ts-api\node_modules\node-fetch\package.json.
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Work\ts-api\src\app.ts:3:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module.m._compile (C:\Work\ts-api\node_modules\ts-node\src\index.ts:1371:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.require.extensions.<computed> [as .ts] (C:\Work\ts-api\node_modules\ts-node\src\index.ts:1374:12) {
code: 'ERR_REQUIRE_ESM'
}
but then I added "type": "module",
in package.json
and it started to giving me the error which I mentioned in title
./src/app.ts
import express,{Application, Request, Response, NextFunction} from 'express';
import fetch from 'node-fetch';
const app:Application = express();
app.get("/",(req:Request, res:Response)=>{
res.send("Hello world");
})
app.get("/api",async (req:Request, res:Response)=>{
const response = await fetch('https://httpbin.org/post', {method: 'POST', body: 'a=1'});
const data = await response.json();
console.log(data);
});
app.listen(3000,()=>{
"Server is started at Port No. 3000"
});
package.json
{
"name": "ts-api",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node dist/app.js",
"dev": "nodemon src/app.ts",
"build": "tsc -p ."
},
"keywords": [],
"type": "module",
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^17.0.7",
"nodemon": "^2.0.15",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
},
"dependencies": {
"express": "^4.17.2",
"node-fetch": "^3.1.0"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es2017", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "commonjs", /* Specify what module code is generated. */
"rootDir": "./src", /* Specify the root folder within your source files. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}