search

Home  >  Q&A  >  body text

es6 - node.js 8 how to configure babel?

.babelrc configuration file is as follows

{
  "presets": ["es2015"]
}

node index.js runs the following code:

import express from 'express';

Error message:

(function (exports, require, module, __filename, __dirname) { import express from 'express';
                                                              ^^^^^^

SyntaxError: Unexpected token import
女神的闺蜜爱上我女神的闺蜜爱上我2717 days ago839

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-06-20 10:08:53

    Use babel-node to run http://www.ruanyifeng.com/blo...

    reply
    0
  • PHP中文网

    PHP中文网2017-06-20 10:08:53

    There is a mistake in your usage method. There are many ways to use babel. The most common ones are .babelrc files, or the require('babel') method. You configured .babelrc but you did not use babel to compile you. index.js file, however the import syntax is not implemented in your existing version of node, so an error is reported

    reply
    0
  • 高洛峰

    高洛峰2017-06-20 10:08:53

    If node also wants to use the ES6 module specification, you need babel-register

    reply
    0
  • Cancelreply