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?