I have a nodejs app, that is meant to run locally in my mac, not in a browser. I want to use typescript, compile it, and run the script.
Here is the code:
// package.json
{
"type": "module",
"main": "index.js",
"scripts": {
"build": "rm -rf dist && yarn tsc"
},
"devDependencies": {
"@types/node": "^20.5.3",
"typescript": "^5.1.6"
},
"dependencies": {
"pdf2json": "^3.0.4"
}
}
I just want to import a service. In TS, the extension is not needed.
// index.ts
import { IdentifyFileUseCase } from './application/use-cases/IdentifyFile.useCase';
const identifyFileUseCase = new IdentifyFileUseCase()
identifyFileUseCase.execute()
when running
yarn build
I get in the dist/index.js
import { IdentifyFileUseCase } from './application/use-cases/IdentifyFile.useCase';
const identifyFileUseCase = new IdentifyFileUseCase();
identifyFileUseCase.execute();
since I told to be this ESmodules,
from './application/use-cases/IdentifyFile.useCase';
this path in the dist/index.js
is wrong because .js
extension is mandatory and my script crashes when running
node dist/index.js
This is the error:
node dist/index.js
node:internal/errors:484
ErrorCaptureStackTrace(err);
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/albert.montolioagua/Documents/coding_area/personal/ts-pdf-reader/dist/application/use-cases/IdentifyFile.useCase' imported from /Users/albert.montolioagua/Documents/coding_area/personal/ts-pdf-reader/dist/index.js
at new NodeError (node:internal/errors:393:5)
at finalizeResolution (node:internal/modules/esm/resolve:323:11)
at moduleResolve (node:internal/modules/esm/resolve:916:10)
at defaultResolve (node:internal/modules/esm/resolve:1124:11)
at nextResolve (node:internal/modules/esm/loader:163:28)
at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
at link (node:internal/modules/esm/module_job:75:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
Node.js v18.12.1
How can I make TS to compile ts into js with the right extension in the path?
I need webpack or can I avoid it?
is my tsconfig.json ok?