简体   繁体   English

从npm脚本运行babel-cli无法正常工作

[英]Running babel-cli from npm script not working

I followed the directions here to install babel-cli . 我按照这里的指示安装了babel-cli I added "build": "babel src -d lib" to my package.json in the directory I want to run it in. However, on running, I get this error: 我在我想要运行它的目录中的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

I'm at a loss. 我不知所措。 Shouldn't src be generated? 不应该生成src吗? There's no extra step on babeljs.io that I am missing. 我错过了babeljs.io没有额外的一步。

Shouldn't src be generated? 不应该生成src吗?

This is the folder that contains the script that you want to be transpiled. 这是包含要编译的脚本的文件夹。 If it doesn't exist then babel will throw the error you posted. 如果它不存在那么babel会抛出你发布的错误。

Also, take note of what it says at the bottom of page you linked to: 另外,请注意您链接到的页面底部的内容:

Pre-6.x, Babel enabled certain transformations by default. 在6.x之前,Babel默认启用了某些转换。 However, Babel 6.x does not ship with any transformations enabled. 但是,Babel 6.x不附带任何启用的转换。 You need to explicitly tell it what transformations to run. 您需要明确告诉它要运行什么转换。 The simplest way to do this is by using a preset, such as the ES2015 Preset. 最简单的方法是使用预设,例如ES2015预设。

This means that even if you create a src directory and place a file containing ES6 code in it, Babel will happily run, but the output will be (almost) identical to the input. 这意味着即使你创建一个src目录并在其中放置一个包含ES6代码的文件,Babel也会愉快地运行,但输出将(几乎)与输入相同。


This is a quick example of how to get up and running with the babel-cli. 这是一个如何使用babel-cli启动和运行的快速示例。

Create a project, then install the babel-cli package and ES2015 preset: 创建一个项目,然后安装babel-cli包和ES2015预设:

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

Next edit package.json : 接下来编辑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"
  }
}

Notice that the command in the npm scripts is slightly different to that on the babel homepage , in so far as we're telling it to use the installed presets. 请注意,npm脚本中的命令与babel主页上的命令略有不同,只要我们告诉它使用已安装的预设。

Next make a file in the src directory: 接下来在src目录中创建一个文件:

mkdir src && cd src
touch main.js

In main.js add: 在main.js中添加:

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

Then run babel via npm: 然后通过npm运行babel:

npm run build

And inspect the output in lib/main.js 并检查lib / main.js中的输出

"use strict";

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

In case someone is still looking for solution, check if .babelrc is missing If so simply create a new .babelrc file and paste the above snippet in it. 如果有人仍在寻找解决方案,请检查.babelrc是否缺失如果是这样,只需创建一个新的.babelrc文件并将上面的代码段粘贴到其中。

{
  "presets": ["es2015", "stage-0"]
}

You also get this error when your node modules are not installed, If you download the code from the internet and immediately try running the code, it throws the above error, just run 未安装节点模块时也会出现此错误。如果从Internet下载代码并立即尝试运行代码,则会抛出上述错误,只需运行

npm install

and then 接着

npm run build // or other commands should work npm run build //或其他命令应该有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM