babel-node在NPM脚本中不起作用

时间:2019-02-25 19:21:41

标签: node.js

我有以下非常简单的“ TypeScript”文件:

// == IMPORT STATEMENTS.
import webpack from 'webpack';

// == CONSTANTS.
const DEFAULT_PORT: number = parseInt(process.env.PORT as string, 10) || 3000;

// == FUNCTIONS.
const startup = (): void => {
    console.log('demo');
};

// MAIN ENTRY POINT.
startup();

我的.babelrc文件如下:

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

当我在终端窗口中调用以下命令时,确实看到了正确的输出:

.\node_modules\.bin\babel-node --extensions '.ts' .\scripts\start.ts

当我为此命令创建NPM脚本时:

"scripts": {
    "start": ".\\node_modules\\.bin\\babel-node --extensions '.ts' .\\scripts\\start.ts"
},

并执行npm run start,将返回以下错误:

C:\Users\kevin\Development\Datalytics.Core\App\scripts\start.ts:25
import webpack from 'webpack';
       ^^^^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at Object.<anonymous> (C:\Users\kevin\Development\Datalytics.Core\App\node_modules\@babel\node\lib\_babel-node.js:234:23)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ start: `babel-node --extensions '.ts' .\scripts\start.ts`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @ start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\kevin\AppData\Roaming\npm-cache\_logs\2019-02-25T19_18_33_685Z-debug.log

这有什么问题吗?

0 个答案:

没有答案