【问题标题】:Can't run global babel-node in es2015 mode无法在 es2015 模式下运行全局 babel-node
【发布时间】:2016-06-27 15:40:13
【问题描述】:

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

 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! project@0.0.0 start: `babel-node --presets es2015-node -- bin/myScript.js "myfile.js"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the project@0.0.0 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,

【问题讨论】:

    标签: npm ecmascript-6 babeljs babel-node


    【解决方案1】:

    错误信息中有提示:

    找不到预设的“es2015-node”相对于目录“/Users/user/project/bin”

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

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

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

    【讨论】:

    • 在 babel-node 中构建 es2015 不是更明智,因此人们可以使用 flag 快速配置它,而不是处理寻找 dirpath 模块/配置的全局可执行文件?
    • Babel 不仅仅是一个 ES6 -> ES5 转译器,因此它需要进行配置。
    • 也许不是,但我觉得没有 es2015 的 babel-node 作为节点运行器是不合适的,因为它不了解模块模式、解构以及可能其他重要特性,为什么不将 babel-node 作为包含 babel-node-es2015 的单独包。
    【解决方案2】:
    npm update
    

    为我解决了这个问题。

    【讨论】:

    • 我认为 babel 最近采用了 es2015,因为 2016 年即将结束。
    猜你喜欢
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 2020-03-06
    • 2019-08-12
    相关资源
    最近更新 更多