12

I'm trying to use babel to run my NodeJS program, which includes ES6 syntax and exports from the Colyseus library. 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:

{
  "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:

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);
1
  • what does your babel config look like? Commented Mar 24, 2019 at 23:31

3 Answers 3

3

Add a config file with the following (.babel.config.js):

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

Then run:

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

Sign up to request clarification or add additional context in comments.

4 Comments

That doesn't seem to work with "@babel/cli": "^7.2.3", "@babel/core": "^7.4.0", "@babel/node": "^7.2.2", "@babel/preset-env": "^7.0.0". I always fail to understand why there are so many ways for doing one such simple thing as transpiling some code in JS.
@cglacet, that is a different major version of Babel, have you checked their docs on this? I'm guessing a CLI flag changed between versions.
I think I found the problem. If you have your config in your root/ directory and call a script that is in say root/folderA and that script imports something from root/folderB then the js file from folderB doesn't seem to be transpiled correctly (ignored?). That look like a bug to me but maybe I don't understand the responsibility of the config file (I had thought of it as a "project configuration" but apparently that's not how it is supposed to be understood).
@cglacet I'm glad you consider JS transpiling "a simple thing". At this point, the whole community is waiting for you to write your personal transpiler. I am really curious to see it at work.
1

babel-node is presumably expecting the node style module syntax:

module.exports = ...

instead of the es6 style:

export class ...

EDIT:

You might be able to fix it by specifying a .babelrc file like so:

{
    "presets": ["env"]
}

with package babel-preset-env installed

Comments

0

Adding a config file (babel.config.js) below worked for me. 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,
        },
     ],
    ],
  };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.