DEV Community

Cover image for New Node.js protocol imports
Jordan Soo Yen Yih
Jordan Soo Yen Yih

Posted on • Edited on

New Node.js protocol imports

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';
Enter fullscreen mode Exit fullscreen mode

Now

import * as fs from 'node:fs/promises';
Enter fullscreen mode Exit fullscreen mode

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

See_Ya

Top comments (4)

Collapse
 
bennycode profile image
Benny Code

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?

Collapse
 
yenyih profile image
Jordan Soo Yen Yih • Edited

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 here

Collapse
 
bennycode profile image
Benny Code

Cool. 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! :)

Thread Thread
 
yenyih profile image
Jordan Soo Yen Yih

Great👏🎉. Thank you for the verify too. I already added into the article. 😊