简体   繁体   English

Babel NodeJS ES6:SyntaxError:意外的令牌导出

[英]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.

相关问题 Mocha 6、Babel 7、ES6:SyntaxError:意外的令牌导出 - Mocha 6, Babel 7, ES6: SyntaxError: Unexpected token export nodejs mocha es6模块意外令牌导出没有babel - nodejs mocha es6 modules unexpected token export without babel NodeJS(ES6):SyntaxError:意外的令牌{ - NodeJS (ES6): SyntaxError: Unexpected token { Webpack / Babel ES6错误:未捕获语法错误:意外的令牌导入 - Webpack/Babel ES6 error: Uncaught SyntaxError: Unexpected token import Babel ES6导入错误,SyntaxError:意外的令牌导入 - Babel ES6 import error, SyntaxError: Unexpected token import jest + typescript + es6 modules(又一次,2019) - SyntaxError:意外的令牌导出 - jest + typescript + es6 modules (yet again, 2019) - SyntaxError: Unexpected token export Babel成功地转译了ES6代码(node.js),但是当“ npm start”执行时,它会抛出“ SyntaxError:意外的令牌导入” - Babel transpiled ES6 code (node.js) successfully, but when do “npm start” it throws “SyntaxError: Unexpected token import ” NodeJS 12 SyntaxError:意外的令牌“导出” - NodeJS 12 SyntaxError: Unexpected token 'export' Karma + Webpack(babel-loader)+ ES6“意外令牌导入” - Karma + Webpack (babel-loader) + ES6 “Unexpected token import” 带有vuejs import es6的开玩笑失败SyntaxError:意外令牌 - Jest with vuejs import es6 fails SyntaxError: Unexpected token <
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM