Aleix
Aleix

Reputation: 127

Do I need to import the process module of nodejs or leave it as such?

I've seen that the process modules can be used without importing it directly in your file as said in the documentation from 2011. https://nodejs.org/en/knowledge/getting-started/the-process-module/

The process module doesn't need to be required - it is somewhat literally a wrapper around the currently executing process, and many of the methods it exposes are actually wrappers around calls into core C libraries.

But in the documentation of Node.js v16.17.1 (the one I'm using) they actually import it without mentioning it. https://nodejs.org/docs/latest-v16.x/api/process.html#process-events

import process from 'node:process';

process.on('beforeExit', (code) => {
  console.log('Process beforeExit event with code: ', code);
});

Is there even a difference between the two ? If I use the import syntax will it be referencing the current process or a different instance?

Upvotes: 5

Views: 1763

Answers (1)

ibrahim tanyalcin
ibrahim tanyalcin

Reputation: 6491

process is available without export. For consistency it is re-exported as a module here

Two things to keep in mind:

  • Child processes (spawn/exec) return their own process object.
  • For Workers, within worker body, process returns the owner process, as these are just threads.

Upvotes: 7

Related Questions