Home > other >  What is the benefit of importing process in node?
What is the benefit of importing process in node?

Time:10-31

I am used to simply using process.cwd() directly inside of node applications, however, I have recently read the node docs and saw that it recommends importing it from node:process.

So what is the difference from just calling it directly?

process.cwd()

vs importing it and calling it:

import {cwd} from 'node:process'

cwd()

I am currently building a CLI application in case that makes a difference.

  • Is there a difference?

  • Which one should I be using?

CodePudding user response:

importing it from node:process guarentees that you get the built-in process module, not some global that some other code in the project has configured or overridden.

This may be done for safety reasons or just for robustness reasons so other modules in the project can't hack on a global process object before you get to use it.

It also may not normally be needed, but is considered good project hygiene as it deters certain types of hacking.

  • Related