如下,有个依赖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:3package.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"
}
}Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
babel6 默认不添加任何 perset,也就是不会转换你的代码,转换 es6 代码需要你配置 perset-es2015,在 .babelrc 或命令行参数中指定。你的情况应该就是没有进行配置导致的。
对于 es6 module,babel6 默认会为你转换为 commonjs 的格式,当然也支持 umd、systemjs、amd。如果不需要转换,例如 webpack2 支持 es6 module,那么你也可以配置 module 参数为 false 来禁用转换。