0

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. */
  }
}


  • How do you get this error? – Liam Jan 06 '22 at 15:26
  • I don't think nodemon supports typescript files? https://stackoverflow.com/questions/57998762/run-nodemon-with-typescript-compiling – evolutionxbox Jan 06 '22 at 15:29
  • Does this answer your question? [Can't run my Node.js Typescript project TypeError \[ERR\_UNKNOWN\_FILE\_EXTENSION\]: Unknown file extension ".ts" for /app/src/App.ts](https://stackoverflow.com/questions/62096269/cant-run-my-node-js-typescript-project-typeerror-err-unknown-file-extension) –  Jan 06 '22 at 15:30
  • Chris G I tried but not working! – Saurabh Dhakne Jan 06 '22 at 16:11

1 Answers1

-4

I think you should use axios instead of node-fetch because node-fetch doesn't support commonjs ES modules, also you can remove "type": "module"

app.ts

import express,{Application, Request, Response, NextFunction} from 'express';

import axios from 'axios';


const app:Application = express();

app.get("/",(req:Request, res:Response)=>{
    res.send("Hello world");
})

app.get("/api",async (req:Request, res:Response)=>{
    const { data } = await axios.post('https://httpbin.org/post','a=1');
    console.log(data);
});

app.listen(3000,()=>{
    "Server is started at Port No. 3000"
});
  • I think this is not at all a solution to question. The issue I can see on the OP's case is that he is trying to run a typescript file using node. – SAMUEL Apr 02 '22 at 16:27