ts-node 无法找到模块

Jay*_*att 5 node.js node-modules typescript ts-node

*.ts我在使用 ts-node运行文件时遇到问题。

我的项目的目录结构如下所示。

-- project dir
   -- src
      -- services
         -- module1
             -- file1.ts  (classA)
             -- file2.ts  (classB)
             -- index.ts  (contains export statements like export * from file1.ts)
   -- application.ts
Run Code Online (Sandbox Code Playgroud)

这是我在tsconfig.json文件中配置的内容。

{
  "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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

这是我的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"
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的中,application.ts我正在导入这样的课程。

import {classA} from "@src/services/module1";
Run Code Online (Sandbox Code Playgroud)

但它一直失败并出现以下错误。

Error: Cannot find module '@src/services/module1'

我已经搜索了好几天,并尝试了其他答案中提供的大量解决方案,但没有任何对我有用。如果有人能指导我正确的方向,我将不胜感激。