Reputation: 31
I am working on a personal project to try to get a new dev role. I am using NestJS and Typescript to try to make a simple API (will later hook up some endpoints to a website, just want to show some basic dev skills).
My current goal is to run the API locally so that I can try to hit a basic endpoint using Postman. When I run 'npm run start:dev' though, I get an error "Cannot find module 'C:\Users\my-name\code\my-api\dist\app.module' imported from C:\Users\my-name\code\my-api\dist\main.js"
I am still learning some about how TypeScript works but I learned that TS files are going to be copied and transformed into JS files, so I wondered if the issue might be related to js and ts extensions.
So I appended .js to the "import { AppModule } from './app.module';" statement in my main.ts file, and sure enough, that error was resolved and replaced with errors that it could not find app.controller, then app.service, etc. until I had replaced all imports without extensions to have .js extensions.
My question then is can I resolve this without the explicit .js extensions on each import? It feels a bit like a code smell (or I guess a config smell or something) to just add those extensions to resolve this issue. At my previous job (I was only a dev for a few months, so still super green), we also used Nest & TS, and I know we did not add .js extensions at the end of imports.
If it is useful, these are the settings I currently have in my tsconfig.json file:
{
"compilerOptions": {
"module": "ESNext",
"target": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"outDir": "./dist",
"sourceMap": true,
"rootDir": "./src",
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"baseUrl": "./"
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
This is my first time setting up a completely greenfield type of project and I'm pretty inexperienced, so there may be things that are obvious to others that may not be obvious to me. In addition to any recommended solutions, I also am trying to learn how to resolve problems like this independently when possible, so I appreciate any recommendations too about how you arrived at your solution/how you approached the problem.
What I have tried so far:
Upvotes: 0
Views: 34