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

前端 未结 1 622
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 05:21

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条回答
  •  醉梦人生
    2020-12-21 06:15

    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?

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