gpt4 book ai didi

javascript - 巴别塔 NodeJS ES6 : SyntaxError: Unexpected token export

转载 作者:搜寻专家 更新时间:2023-11-01 00:03:42 25 4
gpt4 key购买 nike

我正在尝试使用 babel 来运行我的 NodeJS 程序,其中包括 ES6 语法和从 Colyseus 库导出的内容。但是,当我运行命令时:

babel-node server.js

出现以下错误信息:

export class MyRoom extends colyseus.Room {
^^^^^^

SyntaxError: Unexpected token export

下面是我的 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"
}
}

下面是我的 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);

最佳答案

添加一个包含以下内容的配置文件(.babel.config.js):

module.exports = {
presets: [
'@babel/preset-env'
]
};

然后运行:

babel-node --config-file .babel.config.js server.js

关于javascript - 巴别塔 NodeJS ES6 : SyntaxError: Unexpected token export,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55329589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com