从npm脚本运行babel-cli不起作用

我按照这里的指示来安装babel-cli 。 我在我想运行它的目录中添加了"build": "babel src -d lib"到我的package.json中。但是,在运行时,我得到这个错误:

 🐕 npm run build > ipfs-readme-standard@1.0.0 build /Users/richard/src/ipfs-readme-standard > babel src -d lib src doesn't exist npm ERR! Darwin 14.5.0 npm ERR! argv "/Users/richard/.nvm/versions/node/v5.0.0/bin/node" "/Users/richard/.nvm/versions/node/v5.0.0/bin/npm" "run" "build" npm ERR! node v5.0.0 npm ERR! npm v3.5.2 npm ERR! code ELIFECYCLE npm ERR! ipfs-readme-standard@1.0.0 build: `babel src -d lib` npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the ipfs-readme-standard@1.0.0 build script 'babel src -d lib'. npm ERR! Make sure you have the latest version of node.js and npm installed. npm ERR! If you do, this is most likely a problem with the ipfs-readme-standard package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! babel src -d lib npm ERR! You can get information on how to open an issue for this project with: npm ERR! npm bugs ipfs-readme-standard npm ERR! Or if that isn't available, you can get their info via: npm ERR! npm owner ls ipfs-readme-standard npm ERR! There is likely additional logging output above. npm ERR! Please include the following file with any support request: npm ERR! /Users/richard/src/ipfs-readme-standard/npm-debug.log 

我不知所措 不应该生成src? babeljs.io没有额外的步骤,我失踪了。

不应该生成src?

这是包含您想要转换的脚本的文件夹。 如果它不存在,那么babel将会抛出您发布的错误。

另外,请注意您链接到页面底部的内容:

Pre-6.x,Babel默认启用了某些转换。 但是,Babel 6.x没有启用任何转换。 您需要明确告诉它要运行的转换。 最简单的方法是使用预置,如ES2015预设。

这意味着即使你创build了一个src目录并且在其中放置了一个包含ES6代码的文件,Babel也会很开心的运行,但是输出将会和input一样。


这是一个如何与babel-cli起床和跑步的简单例子。

创build一个项目,然后安装babel-cli软件包和ES2015预设:

 mkdir babeltest && cd babeltest touch package.json npm install babel-cli babel-preset-es2015 --save-dev 

下一步编辑package.json

 { "name": "my-project", "version": "1.0.0", "scripts": { "build": "babel src -d lib" }, "scripts": { "build": "babel --presets es2015 src -d lib" }, "devDependencies": { "babel-cli": "^6.0.0" } } 

请注意,npm脚本中的命令与babel主页中的命令略有不同,只要我们告诉它使用已安装的预设。

接下来在src目录下build立一个文件:

 mkdir src && cd src touch main.js 

在main.js中添加:

 [1,2,3].map(x => x * x) 

然后通过npm运行babel:

 npm run build 

并检查lib / main.js中的输出

 "use strict"; [1, 2, 3].map(function (x) { return x * x; }); 

当你的节点模块没有安装的时候,你也会得到这个错误,如果你从网上下载代码并立即尝试运行代码,它会抛出上面的错误,只是运行

 npm install 

接着

npm run build或其他命令应该工作