27

Using Babel in my NodeJSv4.1.1 code.

Got the require hook in:

require("babel-core/register");

$appRoot = __dirname;

module.exports = require("./lib/controllers/app");

In a subsequently lodaded .js file I am doing:

import { Strategy as LocalStrategy } from "passport-local";

However this is generating the following error in the CLI:

import { Strategy as LocalStrategy } from "passport-local";
^^^^^^

SyntaxError: Unexpected reserved word
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:413:25)
    at loader (/Users/*/Documents/Web/*/node_modules/babel-core/node_modules/babel-register/lib/node.js:128:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/*/Documents/Web/*/node_modules/babel-core/node_modules/babel-register/lib/node.js:138:7)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at module.exports (index.js:9:5)
    at Object.<anonymous> (app.js:102:39)
Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
rcjsdev
  • 753
  • 2
  • 8
  • 19
  • what version of Babel? – max Nov 14 '15 at 17:47
  • `"babel-core": "^6.1.21"` – rcjsdev Nov 14 '15 at 18:09
  • have you included any Babel plugins or presets? – max Nov 14 '15 at 18:54
  • nope, i tried es2015preset but this broke my app as well, i got an issue where it was saying the following was undefined: `$passport = require('passport');` – rcjsdev Nov 14 '15 at 19:19
  • Is this on line 1? The error is being reported by the babel module. – w00t Nov 14 '15 at 20:55
  • Please read the tag descriptions. `babel` is for questions for a *Python library* with said name. – Felix Kling Nov 15 '15 at 08:15
  • 1
    @Rob with the es2015 preset, if you change `$passport = require('passport');` to `var $passport = require('passport');`, do you still see that error? – max Nov 15 '15 at 15:03
  • 1
    This is a common error when running plain Node, which means you're likely seeing this because Babel isn't actually running. I would recommend switching over the the `babel-register` module as suggested by [the docs](https://babeljs.io/docs/usage/require/) since you're using Babel 6. Then make sure the file in question isn't getting ignored. See @pherris' comment below – ian Dec 21 '15 at 04:14
  • Also make sure you have correct `.babelrc`. That was the problem in my case. – Jayesh Mar 26 '16 at 03:28
  • @ian - the stack dump includes this line: `at loader (/Users/*/Documents/Web/*/node_modules/babel-core/node_modules/babel-register/lib/node.js:128:5)` which sure sounds like `babel-register` is being used to me... – Jules Jun 11 '17 at 02:05

3 Answers3

25

Sounds like you aren't using the right presets. As of babel 6, the core babel loader no longer includes the expected ES6 transforms by default (it's now a generic code transformer platform), instead you must use a preset:

require('babel-register')({
        "presets": ["es2015"]
});

You will also need to install the preset package:

npm install --save-dev babel-preset-es2015
Arkadiy Kukarkin
  • 2,034
  • 14
  • 23
  • 8
    where does this code belong? in a webpack config? in a babelconfig? in the code that I'm running? –  Mar 14 '16 at 04:06
  • 1
    @omouse you'd usually put this into whatever your main entrypoint is, typically index.js at the top level, before requiring the rest of your code – Arkadiy Kukarkin Mar 14 '16 at 18:34
  • As a little addition, I had to `npm install --save-dev babel-preset-es2015` since it didn't come by default with babel-register. But this answer worked for me! – ArtHare Mar 15 '16 at 17:27
  • I tried all that is mentioned here, but still I get this error. Very annoying. I got the top require("babel-core/register"); upgraded node/npm to 4.4/3.8.1. Have a file .babelrc with { "presets": ["es2015"] }. Installed babel-core, babel-register, babel-preset-2015. My node file has just the require above and import asap from "asap"; right under... – Fractalf Mar 16 '16 at 13:45
  • 2
    @Fractalf: the transpiler from babel-register does not apply to the file in which it is required, so you cannot use `import` in the same file. You must instead require another file which can then use full es2015 semantics – Arkadiy Kukarkin Mar 16 '16 at 18:23
  • 1
    @ArkadiyKukarkin: thanks, so I tried to just require the es6 module and use babel-core in only that module, but then there is the same error just complaining about export. I got it all working with babel-node, and then just building it, but man how hard is this for a first time user! Very bad documentation for those who didn't touch babel yet :( – Fractalf Mar 17 '16 at 07:38
  • Finally found something that made this work: https://leanpub.com/setting-up-es6/read#sec_nodejs-babel-dynamic https://github.com/rauschma/node-babel-dynamic-demo – Fractalf Mar 17 '16 at 08:16
  • it's working after i place on very top of line on my app file root, thanks – yussan Apr 21 '16 at 04:22
  • use `babel-preset-env` it covers babel-preset-2015-17 and its going to recommend it. – JREAM Aug 01 '18 at 10:36
  • I created a .babelrc in the root directory of the project and added `{ "presets": ["es2015"] }` to it – 2080 Sep 24 '18 at 18:59
3

It seems that this file is not being transpiled. Is this subsequently loaded .js file in the node_modules directory? If so, you need to:

require("babel-core/register")({
  // This will override `node_modules` ignoring - you can alternatively pass
  // an array of strings to be explicitly matched or a regex / glob
  ignore: false
});

By default all requires to node_modules will be ignored. You can override this by passing an ignore regex

https://babeljs.io/docs/usage/require/

pherris
  • 16,325
  • 8
  • 40
  • 54
0

I was hitting the problem when trying to run tests via mocha, and I solved it by putting this in my package.json file:

"babel": {
    "presets": [
      "es2015"
    ]
},

I'm not completely clear on how this works. I'm running tests like this:

mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive

Eventually, this will all make sense I suppose.