Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript babel传输typescript文件后,要求路径不正确_Javascript_Typescript_Babeljs - Fatal编程技术网

Javascript babel传输typescript文件后,要求路径不正确

Javascript babel传输typescript文件后,要求路径不正确,javascript,typescript,babeljs,Javascript,Typescript,Babeljs,问题 当我使用npm run start运行nodemon时,会出现错误:找不到模块“Test”,当我使用npm run build和run./dist/index.js构建文件时,会出现相同的错误 如您所见,./dist/index.js上的require路径无法正确识别 我不知道哪些配置需要更改,所以我提出了疑问 如果你知道的话,请用你的回复回复我。谢谢:) 源代码 ./src/index.ts import Test from 'Test'; const test = new Test(

问题

当我使用npm run start运行nodemon时,会出现错误:找不到模块“Test”,当我使用npm run build和run./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"]
    }
}
./

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

尝试从“/Test”导入测试/src/index.ts中的code>。因为没有相对路径,typescript会查找全局模块
Test
。当您想使用本地模块时,您应该使用模块的相对路径。

尝试
从“/Test”导入测试/src/index.ts
中的code>。因为没有相对路径,typescript会查找全局模块
Test
。当您想使用本地模块时,您应该使用模块的相对路径。

谢谢:)是否有其他方法将模块路径维护为测试路径?我使用路径设置来像一个全局模块一样加载,因为使用相对路径非常复杂。您可以使用
路径
so别名作为相对路径。我可以帮你。谢谢你,但我想这似乎是我在上面做的设置。。“路径:{”“:[“node_modules/*”,“src/*”]},*我将该设置更改为“路径:{”“:[“node_modules/*”],“@app/*”:[“src/*”]},*并使用“@app/Test”中的导入测试;,但我遇到了同样的错误:找不到模块'@app/Test'。感谢您的持续帮助!我得到了相同的错误(错误:找不到模块'测试'),在我改变它,如你所说。(该答案现在似乎已被删除。)谢谢:)是否有其他方法将模块路径维护为测试?我使用路径设置来像一个全局模块一样加载,因为使用相对路径非常复杂。您可以使用
路径
so别名作为相对路径。我可以帮你。谢谢你,但我想这似乎是我在上面做的设置。。“路径:{”“:[“node_modules/*”,“src/*”]},*我将该设置更改为“路径:{”“:[“node_modules/*”],“@app/*”:[“src/*”]},*并使用“@app/Test”中的导入测试;,但我遇到了同样的错误:找不到模块'@app/Test'。感谢您的持续帮助!我得到了相同的错误(错误:找不到模块'测试'),在我改变它,如你所说。(该答案现在似乎已被删除。)我已使用模块别名pakage解决了此问题:我已使用模块别名pakage:D解决了此问题