问题描述
我正在学习node.js,想知道为什么它使用require
语法而不是React使用的import
语法.
I'm learning node.js and am wondering why it uses the require
syntax rather than the import
syntax which React uses.
即
const Validator = require("validator");
VS
import Validator from "validator";
我相信import
是es6,但是我不认为这解释了为什么它不在节点中使用.
I believed import
is es6 but I don't think that explains why it's not used in node.
推荐答案
import
和default
是较新的ES6功能,尚未被节点使用. 实际上,Node已经通过实验将新功能实现为实验:仅使用--experimental-modules
标志用于以.mjs
扩展名保存的文件.
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.
像 babel 这样的编译器使编写现代的,经过规格认可和/或实验性的 ECMAScript .在像打包机这样的打包器生态系统(如 Webpack )和诸如babel这样的编译器的情况下,编写可维护的,面向未来的javascript变得很容易,尽管代码仍然受到广泛支持,因为它已转换为commonjs
(您看到的格式可由require
(老式的import
)和module.exports
(老式的export
)识别.
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
).
这篇关于为什么节点使用不需要导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!