npm - es2015模式下无法运行全局babel-node

标签 npm ecmascript-6 babeljs babel-node

所以当我在本地安装这些软件包时它们可以工作,但是当我全局安装(并在本地删除它们)时

 npm i babel-cli -g
 npm i -g babel-preset-es2015
 npm i -g babel-preset-es2015-node

似乎该标志没有将 es2016-node 设置为在全局包列表中查找。无论如何,会发生以下错误:

npm run start myfile.js

babel-node --presets es2015-node -- bin/myScript.js "myfile.js"

/usr/local/Cellar/node/6.1.0/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:395
          throw new Error("Couldn't find preset " + (0, _stringify2.default)(val) + " relative to directory " + (0, _stringify2.default)(dirname));
          ^

Error: Couldn't find preset "es2015-node" relative to directory "/Users/user/project/bin"
    at /usr/local/Cellar/node/6.1.0/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:395:17
    at Array.map (native)
    at OptionManager.resolvePresets (/usr/local/Cellar/node/6.1.0/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:387:20)
    at OptionManager.mergePresets (/usr/local/Cellar/node/6.1.0/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:370:10)

npm ERR! Darwin 14.5.0
npm ERR! argv "/usr/local/Cellar/node/6.1.0/bin/node" "/usr/local/bin/npm" "run" "start" "myfile.js"
npm ERR! node v6.1.0
npm ERR! npm  v3.7.3
npm ERR! code ELIFECYCLE
npm ERR! <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="166664797c737562562638263826" rel="noreferrer noopener nofollow">[email protected]</a> start: `babel-node --presets es2015-node -- bin/myScript.js "myfile.js"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="daaaa8b5b0bfb9ae9aeaf4eaf4ea" rel="noreferrer noopener nofollow">[email protected]</a> start script 'babel-node --presets es2015-node -- bin/myScript.js "myfile.js"'.
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 project package,

最佳答案

错误消息中有提示:

Couldn't find preset "es2015-node" relative to directory "/Users/user/project/bin"

在本地安装预设,并使用 --save 标志将它们保存为 package.json 中的依赖项:

npm install --save babel-preset-es2015-node babel-preset-es2015

Babel 的明智之举是仅在本地查找预设。这样,您就被迫通过在 package.json 中描述其操作所需的必要依赖项来生成可移植的模块,然后由用户通过 npm install 安装.

关于npm - es2015模式下无法运行全局babel-node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38058079/

相关文章:

javascript - ECMA 和谐 – 回调到生成器

基于 grunt 的工作流程的 Flask 项目结构

node.js - 未找到命令 'gulp'

macos - 在 mac 10.7.2 上安装 coffeescript 时出错

javascript - React Native Router Flux 动态路由

javascript - typescript 装饰器不适用于箭头功能

javascript - 通天塔加载器 : Module build failed: SyntaxError: Deleting local variable in strict mode

reactjs - ES7双冒号绑定(bind)语法解析错误

javascript - 是否可以一起使用 TypeScript 和 Babel

node.js - grunt serve 没有在 ubuntu 中运行?