gpt4 book ai didi

javascript - babel 转译 typescript 文件后,要求路径不正确

转载 作者:行者123 更新时间:2023-12-01 01:58:44 26 4
gpt4 key购买 nike

问题

当我使用npm run start运行nodemon时,我收到错误:找不到模块“Test”,以及当我使用npm run build构建文件时 并运行 ./dist/index.js,我得到同样的错误。

如您所见,./dist/index.js 上的 require 路径无法正确识别。

我不知道要更改哪些配置,所以我提出了疑问。

如果您知道这一点,请回复我您的答案。谢谢:)

源代码

./src/index.ts

import Test from 'Test';

const test = new Test();

./src/Test/index.ts

export default class Test {
constructor () {
console.log('Test');
}
}

./dist/index.js

"use strict";

var _Test = _interopRequireDefault(require("Test"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var test = new _Test.default();

./package.json

{
"version": "1.0.0",
"description": "",
"scripts": {
"build": "babel ./src --extensions .ts,.js --out-dir ./dist",
"start": "nodemon ./src --exec babel-node --extensions .ts,.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/cli": "^7.0.0-beta.47",
"@babel/core": "^7.0.0-beta.47",
"@babel/node": "^7.0.0-beta.47",
"@babel/plugin-proposal-class-properties": "^7.0.0-beta.47",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.47",
"@babel/polyfill": "^7.0.0-beta.47",
"@babel/preset-env": "^7.0.0-beta.47",
"@babel/preset-typescript": "^7.0.0-beta.47",
"cross-env": "^5.1.5",
"nodemon": "^1.17.4"
}
}

./tsconfig.json

{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"lib": ["ES2015", "ES2017", "DOM"],
"noEmit": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"module": "esnext",
"moduleResolution": "node",
"paths": { "*":["node_modules/*", "src/*"] },
"pretty": true,
"strictNullChecks": true,
"target": "es5",
"typeRoots": ["./node_modules/@types"]
}
}

./.babelrc

{
presets: ['@babel/preset-env', '@babel/preset-typescript'],
plugins: ['@babel/plugin-proposal-class-properties', '@babel/plugin-proposal-object-rest-spread']
}

最佳答案

尝试在 ./src/index.tsimport Test from './Test';。因为没有相对路径 typescript 寻找全局模块Test。当您想使用本地模块时,您应该使用模块的相对路径。

关于javascript - babel 转译 typescript 文件后,要求路径不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50792208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com