Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript - Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension

I run an app with nodejs and typescript. It is my first step. While I want to deploy the backend app in heroku, I have an error (no error in localhost):

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

Of course I've set "type": "module", but this time I have this error:

[ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

I've tried multiple solution proposed in different thread related to those error

  • To load an ES module, set "type": "module" in the package.json or use the .mjs extension
  • Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts
  • To load an ES module, set "type": "module" in the package.json or use the .mjs extension
  • Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts

Unfortunately, none of them works (reminder, it worked in local when I didn't use type:module)

Node version: v14.15.4

Here my tsconfig.json

{
  "compilerOptions": {
    /* Basic Options */
    "target": "ESNext",  
                       /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "sourceMap": true,                     /* Generates corresponding '.map' file. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true,                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
  },
  "include" : [
    "src/**/*.ts"   /* Include every ts file in source folder */
, "src/server..mjs"],
"exclude" : [
    "node_modules"  /* exclude everything in  node_modules */
], 
}

Here my package.json


{
  "name": "backend-typescript",
  "version": "1.0.0",
  "description": "",
  "main": "src/server.ts",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node src/server.ts",
    "dev": "nodemon src/server.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/body-parser": "^1.19.0",
    "@types/cors": "^2.8.10",
    "@types/express": "^4.17.12",
    "@types/mongoose": "^5.11.97",
    "@types/node": "^15.12.4",
    "axios": "^0.21.1",
    "body-parser": "^1.19.0",
    "config": "^3.3.6",
    "cors": "^2.8.5",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "googleapis": "^78.0.0",
    "http-status-codes": "^2.1.4",
    "jsonwebtoken": "^8.5.1",
    "moment": "^2.29.1",
    "nodemon": "^2.0.7",
    "passport": "^0.4.1",
    "passport-google-oauth20": "^2.0.0",
    "throat": "^6.0.1",
    "typescript": "^4.3.4"
  },
  "devDependencies": {
    "@types/config": "^0.0.38",
    "@types/jsonwebtoken": "^8.5.2",
    "@types/passport": "^1.0.6",
    "@types/passport-google-oauth20": "^2.0.8"
  }
}
like image 746
kev rabe Avatar asked Nov 07 '22 01:11

kev rabe


1 Answers

Install ts-node through npm and use the console to run ts-node nameoffile.ts.

like image 176
WasteofSpaceYT Avatar answered Nov 11 '22 06:11

WasteofSpaceYT