Configure Babel with WebStorm to use ES6 with WebStorm Node.js project

前端 未结 2 1508
轻奢々
轻奢々 2020-12-21 09:43

I\'m using WebStorm latest version to work with Node.js (express.js) frame work. I have set my Babel so that I can use ES6 syntax, for example:

import expres         


        
相关标签:
2条回答
  • 2020-12-21 10:17

    same as @lena but I'm using "@babel/register", I added --require @babel/register

    0 讨论(0)
  • 2020-12-21 10:20

    To make things clear: you issue has absolutely nothing to do with WebStorm, error comes from Node.js interpreter that runs your code. Node.js still doesn't support ES6 modules natively (actually, no JavaScript runtime currently supports them - ECMAScript does not define a "Loader" specification which determines how Modules are inserted into the runtime. The Loader spec is being defined by WHATWG, but is not yet finalized). So, to get ES6 imports/exports accepted, you need using transpilers. Current industry standard is Babel

    To make things work, try the following:

    • install babel in your project using npm install --save-dev babel-cli babel-preset-env
    • create a .babelrc file in project root dir:

      { "presets": ["env"] }

    • in your Node.js Run configuration, pass -r babel-register to Node:

    0 讨论(0)
提交回复
热议问题