Search code examples
node.jsecmascript-6webstormbabeljses6-module-loader

Webstorm Unexpected Token export


I am having an "Unexpected token export" issue in Webstorm that has not been solved by the other StackOverflow posts. Essentially I am trying to use the import/export module functionality with the package.json and bar.js code below. I am using Node.js 5x, Babel 6, and I have a File Watcher setup to do the Babel transforms on the fly.

The code should speak for itself, and I appreciate any thoughts on how to resolve it. Again, I have tried the other StackOverflow suggestions with no luck at this point.

//bar.js

'use strict';

export class Bar{
    constructor(){
        this.tempish = 'allo';
    }
}

//bar-compiled.js

'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Bar = exports.Bar = function Bar() {
    _classCallCheck(this, Bar);

    this.tempish = 'allo';
};

//# sourceMappingURL=bar-compiled.js.map

//index.js

'use strict';

import {Bar} from './bar'

console.log('This is a test.');

//index-compiled.js

'use strict';

var _bar = require('./bar');

console.log('This is a test.');

//# sourceMappingURL=index-compiled.js.map

//package.json

{
  "name": "npt-test",
  "version": "1.0.0",
  "description": "",
  "main": "index-compiled.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel": "^6.3.26",
    "babel-cli": "^6.4.5",
    "babel-core": "^6.4.5",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-stage-0": "^6.3.13"
  }
}

//.babelrc

{
    "presets": ["es2015", "stage-0", "stage-1"],
    "plugins": ["babel-plugin-transform-decorators-legacy"]
}

//Error on debug, I am running against the index-compiled.js during debug

C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe"
"C:\Program Files\nodejs\node.exe" --debug-brk=45287 --nolazy index-compiled.js
Debugger listening on port 45287
[MYPROJECTDIR]\bar.js:3
export class Bar{
^^^^^^

SyntaxError: Unexpected token export
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:404:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> ([MYPROJECTDIR]\index-compiled.js:3:12)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)

Process finished with exit code 1


Solution

  • what does your index-compiled.js code look like? seems it requires original bar.js rather than bar-compiled.js. Node.js can't natively execute ES6 code, thus the errors.

    I'd recommend configuring the compiler to output transpiled code into a separate directory to avoid using 'compiled' postfix. Please see the following link for instructions on setting up Babel 6 with WebStorm: http://mcculloughwebservices.com/2015/12/10/webstorm-babel-6-plugin/