我按照here的说明安装了babel-cli。我在我的package.json中想要运行它的目录中添加了"build": "babel src -d lib"。然而,在运行时,我得到了这个错误:
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上我没有遗漏任何额外的步骤。
发布于 2015-12-28 03:34:45
是否应该生成src?
这是包含您要转换的脚本的文件夹。如果它不存在,那么babel将抛出你发布的错误。
此外,请注意您链接到的页面底部的内容:
在
6.x之前的版本中,Babel默认情况下启用了某些转换。但是,Babel 6.x没有启用任何转换。您需要显式地告诉它要运行哪些转换。执行此操作的最简单方法是使用预设,例如ES2015预设。
这意味着即使您创建了一个src目录并在其中放置了一个包含ES6代码的文件,Babel也会愉快地运行,但输出将(几乎)与输入相同。
这是一个快速示例,说明如何使用babel-cli启动和运行。
创建一个工程,然后安装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 homepage中的命令略有不同,因为我们告诉它使用已安装的预设。
接下来,在src目录中创建一个文件:
mkdir src && cd src
touch main.js在main.js add中:
[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;
});发布于 2016-05-19 17:37:52
当你的节点模块没有安装时,你也会得到这个错误,如果你从互联网上下载代码并立即尝试运行代码,它会抛出上面的错误,只要运行
npm install然后
npm run build //或其他命令应起作用
发布于 2018-04-17 11:37:40
如果有人仍在寻找解决方案,请检查是否缺少.babelrc,如果是,只需创建一个新的.babelrc文件并将上面的代码片段粘贴到其中。
{
"presets": ["es2015", "stage-0"]
}https://stackoverflow.com/questions/34483748
复制相似问题