在Babel上npm运行脚本错误

问题描述:

我打算在编译期间而不是在运行期间转换JSX。首先,我安装使用下面的命令2个巴贝尔工具:在Babel上npm运行脚本错误

$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 

当我运行命令:故宫运行编译,有一系列的错误。

> [email protected] 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! [email protected] compile: `babel src -presets react -out- dir static` 
npm ERR! Exit status 2 
npm ERR! 
npm ERR! Failed at the [email protected] 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软件包的问题,​​ npm ERR!而不是npm本身。

我想寻求什么在这里丢失的建议,或者是与安装/配置或文件夹结构的东西?

+0

你应该在询问之前检查脚本。在你的两个第一个命令中,你用'--presets'运行该命令,但是在那之后,你删除了''-''。 –

你丢失了一些“ - ”在你的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'在大多数情况下是相同的,但需要不同的标记。