I am facing issues when running a *.ts
file using ts-node.
The directory structure of my project looks like this.
-- project dir
-- src
-- services
-- module1
-- file1.ts (classA)
-- file2.ts (classB)
-- index.ts (contains export statements like export * from file1.ts)
-- application.ts
And here's what I have configured in my tsconfig.json
file.
{
"compilerOptions": {
"lib": [
"es2020",
"dom"
],
"baseUrl": ".",
"paths": {
"@src/*": ["src/*"],
"@tests/*": ["tests/*"],
},
"module": "CommonJS",
"target": "es2020",
"strict": true,
"alwaysStrict": true,
"declaration": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"noImplicitReturns": true,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"typeRoots": [
"./node_modules/@types"
]
},
"include": [
"src/**/*.ts",
"tests/**/*.ts"
],
"exclude": [
"node_modules"
]
}
Here's what I have in my package.json
.
{
"name": "xxxx",
"version": "1.0.0",
"description": "xxxx",
"main": "index.js",
"scripts": {
"export": "ts-node src/application.ts",
"build": "tsc",
"watch": "tsc -w",
"lint": "eslint . --ext ts --cache --fix",
"test": "jest --ci --verbose=false --config jest.config.json --passWithNoTests",
"migrate": "ts-node src/application.ts"
},
"author": "xxxx",
"devDependencies": {
"@types/jest": "^27.5.0",
"@types/minimist": "^1.2.2",
"@types/node": "^17.0.31",
"@typescript-eslint/eslint-plugin": "^5.22.0",
"@typescript-eslint/parser": "^5.22.0",
"eslint": "^8.14.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.1.5",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0",
"jest": "^28.0.3",
"minimist": "^1.2.6",
"prettier": "^2.6.2",
"ts-jest": "^28.0.1",
"ts-node": "^10.7.0",
"typescript": "^4.6.4"
},
"dependencies": {
"axios": "^0.27.2",
"mysql": "^2.18.1",
"mysql2": "^2.3.3",
"retry-axios": "^3.0.0",
"sequelize-typescript": "^2.1.3"
}
}
In my application.ts
I am importing classes like this.
import {classA} from "@src/services/module1";
But it keeps failing with following error.
Error: Cannot find module '@src/services/module1'
I've been searching for days and have tried heaps of solutions provided in other answers but nothing worked for me. I would appreciate if anyone can guide me in the right direction.