Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node error: SyntaxError: Unexpected token import

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 
like image 760
Stefdelec Avatar asked Jun 04 '16 19:06

Stefdelec


People also ask

How do I fix an unexpected token?

This error can occur for a number of reasons, but is typically caused by a typo or incorrect code. Luckily, the SyntaxError: Unexpected token error is relatively easy to fix. In most cases, the error can be resolved by checking the code for accuracy and correcting any mistakes.

What is Syntax error unexpected token?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

Can I use import in node?

NodeJS allows us to import CommonJS modules from ES Modules. If we would like to import our CommonJS class export example from above, our ES Module import would look like this: // index.

How do I fix SyntaxError unexpected token export?

To solve the "Uncaught SyntaxError Unexpected token 'export'", refactor your code to use the CommonJS syntax, e.g. module. exports = {num}; instead of export num = 10; . Copied!


2 Answers

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.

like image 91
lorefnon Avatar answered Sep 21 '22 08:09

lorefnon


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

const calc = require('./my_calc'); let {add, multiply} = calc; 
like image 30
Mahendran Avatar answered Sep 19 '22 08:09

Mahendran