I am confused about require
and import
in node.js.
Some modules use require
and some use import
can we use only one or the other for all modules?
I am confused about require
and import
in node.js.
Some modules use require
and some use import
can we use only one or the other for all modules?
Yes, you can exclusively use import
statements to load modules.
You must configure Node to use the ESM loader. The most common method of doing this is setting the "type"
field in package.json
to "module"
:
// package.json
{
"type": "module"
}
See Determining the module system in the docs.