javascript - js 新手问题,如何搭建es6环境?
怪我咯
怪我咯 2017-04-10 17:43:28
[JavaScript讨论组]

如下,有个依赖es6的模块,运行后提示import错误,以前都是用别人弄好的手脚架来运行的,现在发现不知道该怎么弄了.

E:\weixin\wechaty>node mybot.js
E:\weixin\wechaty\mybot.js:1
(function (exports, require, module, __filename, __dirname) { import {Wechaty} f
rom 'wechaty'
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:513:28)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:449:3
    

package.json如下

{
  "dependencies": {
    "wechaty": "^0.5.1",
    "babel-core": "^6.0.0",
    "babel-eslint": "^6.1.2",
    "babel-loader": "^6.0.0",
    "babel-plugin-transform-runtime": "^6.0.0",
    "babel-preset-es2015": "^6.0.0",
    "babel-preset-stage-2": "^6.0.0",
    "babel-register": "^6.0.0",
    "babel-runtime": "^6.0.0"
  },
  "devDependencies": {
    "babel-cli": "^6.18.0"
  }
}
怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(3)
ringa_lee

这个应该ES6环境的问题,V8引擎已经支持ES6了~应当查看下模块的安装是否出错,比如babel有没有安装到本地依赖。

大家讲道理

需要配置两个文件

index.js

require("babel-core/register");
require('./app') //app.js为真正入口

.babelrc

{
  "presets": ["es2015"]
}
阿神

babel6 默认不添加任何 perset,也就是不会转换你的代码,转换 es6 代码需要你配置 perset-es2015,在 .babelrc 或命令行参数中指定。你的情况应该就是没有进行配置导致的。

对于 es6 module,babel6 默认会为你转换为 commonjs 的格式,当然也支持 umd、systemjs、amd。如果不需要转换,例如 webpack2 支持 es6 module,那么你也可以配置 module 参数为 false 来禁用转换。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号