Struggling to search for this online. New versions of nodejs have core modules like:
require('node:assert') /// vs require('assert')
and
require('node:net') /// vs require('net')
I cannot find out what this means vs the original core module paths. Is there an explanation for this change?
When a module supports being imported with a prefix and without a prefix, it functions identically no matter how it's imported.
For built-in modules that have already existed - like fs
- the old way of require('fs')
will continue to work. But some new experimental modules are prefix-only; to import them, you must prefix them with node:
. If you don't:
'node:test'
is the first core module that can only be imported using the'node:'
prefix. In order to use Node's new test runner, you mustimport 'node:test'
. If the'node:'
prefix is not included, Node.js will attempt to load a module named test from userland instead.
Why prefixes? From the same link:
As previously mentioned, the explicit distinction between Node core modules and userland modules is the biggest benefit of prefix-only modules. For the Node.js core project, this change also makes it significantly easier to introduce new modules. Because core modules take precedence over userland modules during module loading, introducing a new core module has historically been treated as a breaking change and sometimes involved reaching out to npm module authors to negotiate the use of a module name. Prefix-only core modules provide a clear delineation between core and userland, reducing much of the friction involved in adding a new core module.
Using
'node:'
as a namespacing mechanism also allows new core modules to be introduced with more appealing names. For example, the new test runner was able to claim the name'test'
instead of something longer like'test_runner'
because there was no chance of a naming conflict.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With