Babel 转码 es6项目问题失败?

发布于 2022-09-04 03:43:12 字数 3244 浏览 10 评论 0

全局执行是没有问题

babel ./ -d babel

但是放到script里面就不行了

package.json

//...
"devDependencies": {
"babel": "^6.5.2",
    "babel-cli": "^6.14.0",
    "babel-core": "^6.18.2",
    "babel-eslint": "^6.1.0",
    "babel-loader": "^6.2.7",
    "babel-polyfill": "^6.9.1",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.5.0",
    "babel-preset-stage-0": "^6.5.0",
    "babel-preset-stage-2": "^6.18.0",
    "babel-preset-stage-3": "^6.17.0",
},
"scripts": {
//...
    "build": "babel ./ -d ./babel"
//...
  },
//...

.babelrc

{
    "presets": [
        "es2015",
        "react",
        "stage-2"
    ]
}

错误提示

app\redux - fetch\store\configureStore.jsx -> babel\app\redux - fetch\store\configureStore.js
app\routes\index-koa.js -> babel\app\routes\index-koa.js
Error: app/routes/index-koa2.js: You gave us a visitor for the node type "ForAwaitStatement" but it's not a valid type
    at verify (D:\luoo\nodejs\node_modules\babel-cli\node_modules\babel-traverse\lib\visitors.js:196:13)
    at Object.explode (D:\luoo\nodejs\node_modules\babel-cli\node_modules\babel-traverse\lib\visitors.js:72:3)
    at traverse (D:\luoo\nodejs\node_modules\babel-cli\node_modules\babel-traverse\lib\index.js:81:12)
    at NodePath.traverse (D:\luoo\nodejs\node_modules\babel-cli\node_modules\babel-traverse\lib\path\index.js:144:25)
    at exports.default (D:\luoo\nodejs\node_modules\babel-helper-remap-async-to-generator\lib\index.js:10:8)
    at PluginPass.Function (D:\luoo\nodejs\node_modules\babel-plugin-transform-async-to-generator\lib\index.js:13:56)
    at newFn (D:\luoo\nodejs\node_modules\babel-cli\node_modules\babel-traverse\lib\visitors.js:276:21)
    at NodePath._call (D:\luoo\nodejs\node_modules\babel-cli\node_modules\babel-traverse\lib\path\context.js:76:18)
    at NodePath.call (D:\luoo\nodejs\node_modules\babel-cli\node_modules\babel-traverse\lib\path\context.js:48:17)
    at NodePath.visit (D:\luoo\nodejs\node_modules\babel-cli\node_modules\babel-traverse\lib\path\context.js:105:12)

npm ERR! Windows_NT 10.0.14393
npm ERR! argv "D:\\nodejs\\node.exe" "D:\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
npm ERR! node v6.2.0
npm ERR! npm  v3.8.9
npm ERR! code ELIFECYCLE
npm ERR! nodejs@1.0.0 build: `babel ./ -d ./babel`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nodejs@1.0.0 build script 'babel ./ -d ./babel'.
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 nodejs package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     babel ./ -d ./babel
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs nodejs
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls nodejs
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     D:\luoo\nodejs\npm-debug.log

非常感谢!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

時窥 2022-09-11 03:43:12

试试加一个 compact: false

谁的新欢旧爱 2022-09-11 03:43:12

貌似windows下的路径问题,用标准的babel src -d lib试试。

那小子欠揍 2022-09-11 03:43:12

问题解决了,虽然来回答的人都没能帮我解决,但还是非常感谢能抽空过来回答!
是这样的,我google了一下把babel-cli babel-core都重新装了一遍,然后把bable删除了问题就暂时解决了(有点知其然不知其所以然的感觉,谁知道原理希望不吝赐教)
过程中发现两个问题

  1. package.json'生成'的文件不能被正常require(),可以使用babel-plugin-add-module-exports来解决

  2. 使用koa + node.js搭建的服务器解析后会出现这个下面错误

var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(ctx, next) {
                                     ^

ReferenceError: regeneratorRuntime is not defined

原因因为阮老师原话:

Babel默认只转换新的JavaScript句法(syntax),而不转换新的API,比如Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise等全局对象,以及一些定义在全局对象上的方法(比如Object.assign)都不会转码。
解决办法也很简单只要在头部加上require('babel-polyfill');或者import 'babel-polyfill';就可以了

.babelrc

{
    "presets": [
        "es2015",
        "react",
        "stage-2"
    ],
    "plugins": [
        "add-module-exports"
    ]
}

package.json

{
//...
    "devDependencies": {
        "babel-cli": "^6.14.0",
        "babel-core": "^6.18.2",
        "babel-eslint": "^6.1.0",
        "babel-loader": "^6.2.7",
        "babel-plugin-add-module-exports": "^0.2.1",
        "babel-polyfill": "^6.9.1",
        "babel-preset-es2015": "^6.9.0",
        "babel-preset-react": "^6.5.0",
        "babel-preset-stage-0": "^6.5.0",
        "babel-preset-stage-2": "^6.18.0",
        "babel-preset-stage-3": "^6.17.0"
    }
//...
}
夏日浅笑〃 2022-09-11 03:43:12

babel5和babel6不一样 可以参考http://www.ruanyifeng.com/blo...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文