在babel

时间:2018-06-11 07:13:36

标签: javascript typescript babel

问题

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

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

我不知道哪个配置需要更改,所以我提出质疑。

如果你知道这件事,请用你的回复回复我。谢谢:))

源代码

./ SRC / index.ts

import Test from 'Test';

const test = new Test();

./ SRC /测试/ 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']
}

1 个答案:

答案 0 :(得分:2)

import Test from './Test';中尝试./src/index.ts。因为没有相对路径,typescript会查找全局模块Test。如果要使用本地模块,则应使用模块的相对路径。