import/export statements in JavaScript not working

Hello everyone,

I'm a newbie to WebStrom. I'm working with JavaScript(ES6) and using a modular approach in my Project.

But for me, import/export statements are not working. It is giving me an error...

Unexpected token import

But if I try the same thing with require/module.exports it works fine.

N.B. - I've already changed the JS version to ES6 from Languages and Frameworks.

0
2 comments

This is not WebStorm but Node.js that fails. While import is a part of ES6, native support for ES6 modules in Node.js is very limited and requires special setup - see https://nodejs.org/api/esm.html#esm_enabling. So, you have to compile your code with Babel first. Usually transpiling is a part of build process (using Gulp, Grunt, WebPack, etc.). Or, you can transpile your code on-the-fly by passing -r babel-register to Node.js. Of course, you would still need to have all required modules installed

1
Avatar
Permanently deleted user

Thanks, Elena!

0

Please sign in to leave a comment.