why node uses require not import?

后端 未结 3 1339
难免孤独
难免孤独 2021-02-19 20:08

I\'m learning node.js and am wondering why it uses the require syntax rather than the import syntax which React uses.

i.e.

const

3条回答
  •  醉话见心
    2021-02-19 20:29

    the import and default are newer ES6 features, not yet used by node. Node is actually already implementing the new features as experiment though: with the --experimental-modules flag and only for files saved with .mjs extension.

    Transpilers like babel make it possible to write modern, spec approved and /or experimental ECMAScript. In an ecosystem of bundlers like Webpack with transpilers like babel, it becomes easy to write maintainable, future-proof javascript, while the code remains widely suported because it's transformed to commonjs (the format you see recognizable byrequire (the old-school import) and module.exports (the old-school export).

提交回复
热议问题