[英]Babel NodeJS ES6: SyntaxError: Unexpected token export
I'm trying to use babel to run my NodeJS program, which includes ES6 syntax and exports from the Colyseus library.我正在尝试使用 babel 来运行我的 NodeJS 程序,其中包括 ES6 语法和来自 Colyseus 库的导出。 However, when I run the command:但是,当我运行命令时:
babel-node server.js
The following error message appears:出现以下错误消息:
export class MyRoom extends colyseus.Room {
^^^^^^
SyntaxError: Unexpected token export
Below is my package.json file:下面是我的 package.json 文件:
{
"name": "app",
"version": "1.0.0",
"description": "a description",
"main": "server.js",
"scripts": {
"test": "babel-node server.js",
"build": "babel-node server.js"
},
"author": "henryzhu",
"license": "ISC",
"dependencies": {
"actionhero": "^19.1.2",
"colyseus": "^0.9.33",
"easytimer.js": "^2.3.0",
"express": "^4.16.3",
"socket.io": "^2.1.0",
"socketio": "^1.0.0",
"uniqid": "^5.0.3"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1"
}
}
Below is my server.js file:下面是我的 server.js 文件:
var colyseus = require("colyseus");
var http = require("http");
var express = require("express");
var port = process.env.port || 3000;
var app = express();
app.use(express.static("public", { dotfiles: 'allow' }));
var gameServer = new colyseus.Server({
server: http.createServer(app)
});
export class MyRoom extends colyseus.Room {
// When room is initialized
onInit (options) { }
}
gameServer.listen(port);
Add a config file with the following ( .babel.config.js
):添加具有以下内容的配置文件( .babel.config.js
):
module.exports = {
presets: [
'@babel/preset-env'
]
};
Then run:然后运行:
babel-node --config-file .babel.config.js server.js
babel-node is presumably expecting the node style module syntax: babel-node 大概期望节点样式模块语法:
module.exports = ...
instead of the es6 style:而不是 es6 风格:
export class ...
EDIT:编辑:
You might be able to fix it by specifying a .babelrc file like so:您可以通过指定一个 .babelrc 文件来修复它,如下所示:
{
"presets": ["env"]
}
with package babel-preset-env installed安装了包 babel-preset-env
Adding a config file (babel.config.js) below worked for me.在下面添加一个配置文件(babel.config.js)对我有用。 Also, the order is important.另外,顺序很重要。 presets should be before all the plugins.预设应该在所有插件之前。
module.exports = {
presets: [['@babel/preset-env',{targets: {node:
'current',},loose:true,},],],
plugins: [
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-syntax-import-meta',
[
'@babel/plugin-transform-runtime',
{
useESModules: true,
},
],
],
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.