【问题标题】:Import TS file in JS file nodejs cannot find module在JS文件nodejs中导入TS文件找不到模块
【发布时间】:2021-09-01 11:42:24
【问题描述】:

我正在将一个 NodeJS 项目迁移到 Typescript。我已经创建了配置,它只使用 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(项目的根目录)

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();

【问题讨论】:

  • 当然不知道,nodejs 知道 JavaScript,不知道 typescript,也知道 typescript 在运行之前它会将代码编译为 js
  • 这就是 babel-node 的用途
  • 不,不是。你已经尝试过 ts-node 了吗?
  • babel-node is a CLI that works exactly the same as the Node.js CLI, with the added benefit of compiling with Babel presets and plugins before running it. 我在其他项目中也是这样做的,但由于某种原因在这里不起作用。

标签: node.js typescript babeljs


【解决方案1】:

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

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();

【讨论】:

    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 2018-02-06
    • 2020-01-11
    • 1970-01-01
    相关资源
    最近更新 更多