Is it ok to use Babel npm package for node.js serv

2019-01-28 08:13发布

问题:

As Node V6 already supports ~95% of ES6, why are people using Babel on the server side? What are the pros and cons of using Babel on the server side?

回答1:

There is one main reason: import / export

See:

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

There is even a special Babel preset for Node:

  • https://www.npmjs.com/package/babel-preset-node6

that doesn't transpile anything that Node already supports natively.

You can also use babel-preset-env which is "A Babel preset that can automatically determine the Babel plugins and polyfills you need based on your supported environments" - thanks to loganfsmyth for pinting it out in the comments. See:

  • https://www.npmjs.com/package/babel-preset-env

And Node doesn't support ES6 modules - see this answer for details on why:

  • javascript - Why is there a spec for sync and async modules?