在JS文件中导入TS文件nodejs找不到模块

Import TS file in JS file nodejs cannot find module

我正在将 NodeJS 项目迁移到 Typescript。我已经创建了配置,它 运行s 只有 JS。 我在开发环境中使用 @babel/node 到 运行 它,但是当我尝试将 TS 文件导入 JS 时它不起作用。

它抛出以下错误:

Error: Cannot find module './findById'
Require stack:
- src/CDBR/dynamicJob/infrastructure/repository/DynamicJobPGRepository.js
- src/CDBR/dynamicJob/infrastructure/DynamicJobModule.js
- src/Main.js
- src/index.js

这是我声明函数的 TS 文件:

const findById = async (dynamicId: string, database: Postgresql) => {
    ...
};

export default findById;

这是我尝试导入它的 JS 文件:

import findById from './findById';

tsconfig.json

{
  "compilerOptions": {
    "target": "es2015",
    "lib": [
      "esnext"
    ],
    "allowJs": true,
    "baseUrl": "./src",
    "outDir": "./dist",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "CommonJS",
    "moduleResolution": "node",
    "sourceMap": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "experimentalDecorators": true
  },
  "include": [
    "src",
  ]
}

.babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-typescript"
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", {"legacy": true}],
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-proposal-optional-chaining",
    ["module-resolver", {
      "root": ["./src"],
      "extensions": [".js", ".ts"]
    }]
  ]
}

package.json

"scripts": {
    "build": "tsc",
    "serve": "NODE_PATH=dist/ node dist/index.js",
    "lint": "eslint ./src",
    "start": "babel-node src/index.js"
},
"dependencies": {
    "@babel/core": "^7.15.0",
    "@babel/node": "^7.14.9",
    "@babel/polyfill": "^7.12.1"
},
"devDependencies": {
    "@babel/cli": "^7.14.8",
    "@babel/plugin-proposal-class-properties": "7.8.3",
    "@babel/plugin-proposal-decorators": "7.8.3",
    "@babel/plugin-proposal-optional-chaining": "7.8.3",
    "@babel/preset-env": "^7.15.0",
    "@babel/preset-typescript": "^7.15.0",
    "@babel/register": "^7.15.3",
    "@typescript-eslint/parser": "^4.30.0",
    "babel-eslint": "10.0.3",
    "babel-jest": "^26.6.0",
    "babel-plugin-module-resolver": "^4.0.0",
    "eslint": "6.8.0",
    "eslint-import-resolver-babel-module": "5.1.2",
    "eslint-import-resolver-typescript": "^2.4.0",
    "eslint-plugin-import": "2.20.1",
    "eslint-plugin-jest": "^24.1.0",
    "eslint-plugin-promise": "4.2.1",
    "jest": "^26.6.0",
    "typescript": "^4.4.2"
}

index.js (root of the project)

import 'core-js/stable';
import 'regenerator-runtime/runtime';

require('@babel/register')({
  extensions: ['.js', '.ts'],
});

import Main from './Main';

const start = async () => {
  try {
    const app = new Main();

    await app.initConfig();
    app.initContainer();
    app.initServer();
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
};

start();

已解决:我必须将“@babel/plugin-transform-runtime”添加到 .babelrc 插件 属性 并将入口点更改为我的应用程序。这是现在的样子:

require('core-js/stable');
require('regenerator-runtime/runtime');

require('@babel/register')({
  extensions: ['.js', '.ts'],
});

const Main = require('./Main').default;

const start = async () => {
  try {
    const app = new Main();

    await app.initConfig();
    app.initContainer();
    app.initServer();
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
};

start();