5

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.

Jay Bhatt
  • 5,601
  • 5
  • 40
  • 62
  • @phill apologies, it was just a typo in the question. I’ve corrected it now. – Jay Bhatt May 12 '22 at 01:46
  • Yep, I am sure there is no typo. It was only in the example of this question. – Jay Bhatt May 12 '22 at 01:55
  • You could try `import {classA} from "src/services/module1";` – tromgy May 12 '22 at 02:10
  • 1
    Problem is that there are hundreds of ts files in module1 and they all use the path @src/ – Jay Bhatt May 12 '22 at 02:11
  • Seems related to this still-open PR https://github.com/TypeStrong/ts-node/pull/1585 – Phil May 12 '22 at 02:26
  • 1
    Does this answer your question? [Typescript path aliases not resolved correctly at runtime](https://stackoverflow.com/questions/60067281/typescript-path-aliases-not-resolved-correctly-at-runtime) – Phil May 12 '22 at 02:28

0 Answers0