I am currently trying to be familiarize with TypeScript and i was creating a Node JS project from scratch and i have a question that why i am getting error while adding "type":"module"
to package.json
.
Error :
TypeError: Unknown file extension ".ts" for F:node_pracindex.ts
[1] at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
[1] at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
[1] at defaultLoad (node:internal/modules/esm/load:143:22)
[1] at async ModuleLoader.load (node:internal/modules/esm/loader:409:7)
[1] at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
[1] at async link (node:internal/modules/esm/module_job:76:21) {
[1] code: 'ERR_UNKNOWN_FILE_EXTENSION'
[1] }
[1] [nodemon] app crashed - waiting for file changes before starting...
Why i am unable to use ECMAScript module with TypeScript here and What changes are required to make it working and why?
Note : It is working good if i am not adding “type”:”module” in package.json.
Currently i only have a base folder structure which involves following files:
Project/
|
|- node_modules/
|- index.ts
|- package-lock.json
|- package.json
**package.json **
{
"name": "node_prac",
"version": "1.0.0",
"description": "Practice project",
"main": "index.ts",
"scripts": {
"start": "concurrently "ts-node" "nodemon index.ts"",
"test": "echo "Error: no test specified" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^22.4.0"
},
"dependencies": {
"concurrently": "^8.2.2"
}
}
I am writing this to understand what is actually the issue is and why it is? as well as to know what is the best possible way to solve it.
I had tried removing "type" : "module"
in package.json and it was working but i want to use ECMAScript module in my project.