node:
is a new protocol imports for loading Node.js built-in modules. Actually it has been there for a while since Node.js v12. However I just heard about this few days ago.😲 Let's find out together how to use this new node:
protocol.
Previously
import * as fs from 'fs/promises';
Now
import * as fs from 'node:fs/promises';
Benefits of node:
imports✨
It resolved the conflict of a module (npm-installed) in node_modules
overriding the built-in module. Besides that, it is also clear that a built-in Node.js module is imported.
Supported Node.js versions
- v16.0.0, v14.18.0 (ESM import and CommonJS require())
- v14.13.1, v12.20.0
- Supported in TypeScript by the latest versions of
@types/node
(>= v16.0.0).
That's it, if this is new to you too, hope you find this useful. Thank you for reading. Please check out the official documentation too
Top comments (4)
I just tried to use
import fs from 'node:fs'
with TypeScript but the TypeScript compiler tells me "TS2307: Cannot find module 'node:fs' or its corresponding type declarations.". Do you know what I am doing wrong?Hi , it supported in TypeScript by the latest versions of
@types/node
. However I haven't try in Typescript yet. Maybe you can double check on the@types/node
version you are using. You may refer the Github issue hereCool. I just tried it wih
@types/node
v16.11.20 and can confirm it works! I suggest adding it to your article. Thanks for showing me this! :)Great👏🎉. Thank you for the verify too. I already added into the article. 😊