npm在Babel上运行脚本错误

时间:2017-06-03 08:47:33

标签: javascript node.js express babeljs mern

我打算在构建期间而不是在运行时转换JSX。首先,我使用以下命令安装了2个babel工具:

$npm install --save-dev babel-cli babel-preset-react
$node_modules/.bin/babel src --presets react --out-dir static

然后在package.json中,我在脚本部分下添加了以下内容。

...
"scripts": {
    "compile": "babel src -presets react -out-dir static",
    "watch": "babel src -presets react -out-dir static -watch",
    "test": "echo \"Error: no test specified\" && exit 1"
},
...

我的文件夹目录如下:

Mern\node_modules
Mern\src\app.jsx
Mern\static\app.js
Mern\static\index.html
Mern\package.json
Mern\server.js

当我运行命令时:npm run compile,出现了一系列错误。

> pro_mern_stack@1.0.0 compile         /Users/comp/Documents/mern
> babel src -presets react -out-dir static

-d doesn't exist. -i doesn't exist. -r doesn't exist

npm ERR! Darwin 16.6.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "compile"
npm ERR! node v7.10.0
npm ERR! npm  v4.2.0
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! pro_mern_stack@1.0.0 compile: `babel src -presets react -out- dir static`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the pro_mern_stack@1.0.0 compile script 'babel src -presets react -out-dir static'.
npm ERR! Make sure you have the latest version of node.js and npm installed.

npm ERR!如果你这样做,这很可能是mern包的一个问题,    错误的ERR!不是与npm本身。

我想就此处缺少的内容寻求建议,或者是安装/配置或文件夹结构的建议?

1 个答案:

答案 0 :(得分:2)

你在npm脚本中遗漏了一些' - ',必须是这样的:

"scripts": {
"compile": "babel src --presets react --out-dir static",
"watch": "babel src --presets react --out-dir static --watch",
"test": "echo \"Error: no test specified\" && exit 1"
},

当你使用' - ' - 它是一个短版本的选项时,每个选项只需要1个字符,当你像-dir一样编写时,对于命令它看起来像:-d -i -r,但是' - '取全名选项。比如:'-v'和' - version'在大多数情况下是相同的,但需要不同的标记。