58

I don't understand what is wrong. I checked other forum talking about transpilation and babel. What do I have to do?

node -v
v5.5.0

my code:

import recast from 'recastai'

and the error

(function (exports, require, module, __filename, __dirname) { import recast from 'module1'
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:139:18)
    at node.js:999:3
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Stefdelec
  • 2,711
  • 3
  • 33
  • 40
  • Possible duplicate of [Can't run simple app with Koa v2](http://stackoverflow.com/questions/37529156/cant-run-simple-app-with-koa-v2) – gevorg Jun 04 '16 at 20:23
  • @Stefdelec check out this answer see if it answers this better. This needed a revisit https://stackoverflow.com/a/47880185/124486 Please updated the chosen answer if so. – Evan Carroll Dec 19 '17 at 04:48

6 Answers6

44

ES6 imports are a recently introduced feature and the current stable version of Node does not support them yet. Node.js issue tracker has an open issue for this - but until V8 and Node add support for this feature, you will need to use a transpiler (most popular one being babel) to be able to use imports.

For quickly trying out transpilation, babel provides a web based REPL. This one demonstrates your code being transpiled.

The babel project homepage points to the relevant resources for getting started with Babel and integrating it with your development workflow.

For the simplest setup, visit this setup page and select CLI in the Babel built-ins section.

This basically involves three simple steps:

  1. Install babel-cli : npm install --save-dev babel-cli babel-preset-es2015
  2. Create .babelrc configuration file: echo '{ "presets": ["es2015"] }' > .babelrc
  3. Use the installed module to transpile your source code: ./node_modules/.bin/babel src -d lib

The aforementioned setup page also illustrates how to add an npm script to simplify the last step. Alternatively you can integrate babel with your editor or build chain so that your files are automatically compiled on change.

lorefnon
  • 12,875
  • 6
  • 61
  • 93
10

In case you don't want to deal with babel. This one worked for me.

const calc = require('./my_calc');
let {add, multiply} = calc;
Mahendran
  • 2,719
  • 5
  • 28
  • 50
5

1) Install the latest presets

yarn add --dev babel-preset-latest

2) Create .babelrc and add the following

{
    "presets": ["latest"]
}

3) Run your script

npx babel-node yourscript.js

Or in your package.json file add

"scripts": {
  "start": "babel-node index.js"
}
Jonathan H
  • 7,591
  • 5
  • 47
  • 80
Jerome Anthony
  • 7,823
  • 2
  • 40
  • 31
2

Getting Started

First we'll install babel-cli.

$ npm install --save-dev babel-cli

Along with some presets.

$ npm install --save-dev babel-preset-es2015 babel-preset-stage-2

package.json:

  "scripts": {
    "start": "babel-node index.js --presets es2015,stage-2"
  }

run:

$ npm start

Watching file changes with nodemon:

We can improve our npm start script with nodemon.

$ npm install --save-dev nodemon

Then we can update our npm start script.

package.json:

 "scripts": {
   "start": "nodemon index.js --exec babel-node --presets es2015,stage-2"
  }

run:

$ npm start 

If you are using pm2, then follow these steps:

$ pm2 start app.js --interpreter babel-node
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
1

Its very simple to resolve this issue, import is ES6 syntax and Node has difficulty in supporting it, you need to add Babel as a transpiler, go to package.json and add the following

First add a script tag to use babel while running the JS code for transpiling.

"scripts": {
    "start": "nodemon ./app.js --exec babel-node -e js"
  }

And then add the following as the Babel devDependencies

"devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1"
  }

after this you also need to configure the babel presets file, therefore create .babelrc file at the root directory and define the presets as follows

{
  "presets": [
    "es2015",
    "stage-0"
  ]
}

Don't forget to do an npm install in the end

Naved Ahmad
  • 783
  • 8
  • 7
0

Thanks to a NodeJS enhancement proposal we have a path forward. You can use @standard-things/esm

Find the announcement here Simply run

npm i --save @std/esm

In your packaged today.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468