I have the following code (example):
User.ts
...
import { UserFavoriteRoom } from "./UserFavoriteRoom.js";
import { Room } from "./Room.js";
import { Reservation } from "./Reservation.js";
import { Message } from "./Message.js";
import { Review } from "./Review.js";
...
But when typescript transpiles the code into javascript, the result looks like this:
User.js
...
import { UserFavoriteRoom } from "./UserFavoriteRoom";
import { Room } from "./Room";
import { Reservation } from "./Reservation";
import { Message } from "./Message";
import { Review } from "./Review";
...
As you can see the .js
is missing in the imports, and it is a huge problem because when I try to run the code this happens:
'Error [ERR_MODULE_NOT_FOUND]: Cannot find module: 'UserFavoriteRoom' imported from User.js
If I manually append the .js
to userFavoriteRoom
(the import inside the User.js file) for example, then the error goes away and another error like that happens with another import that doesn't have the .js
at the end of the import.
Here is my tsconfig.json:
{
"compilerOptions": {
"rootDirs": [
"src"
],
"outDir": "dist",
"lib": [
"es2020",
"esnext.asynciterable"
],
"target": "es2020",
"module": "esnext",
"moduleResolution": "node",
"esModuleInterop": true,
"types": [
"node"
],
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
},
}
And my package.json: (I am using node 18.16.0)
{
"name": "project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc --build tsconfig.json",
"start": "npm run build && node ./dist/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"@apollo/server": "^4.7.5",
"class-validator": "^0.14.0",
"graphql": "^16.7.1",
"mysql": "^2.18.1",
"reflect-metadata": "^0.1.13",
"type-graphql": "^2.0.0-beta.2",
"typeorm": "^0.3.17"
},
"devDependencies": {
"@types/node": "^20.4.2",
"typescript": "^5.1.6"
}
}
What can I do to append the .js
to the imports in the transpiled code automatically?