Node process IDs

1 min read

Shoutout to Sriraghavan for suggesting the snippet and the debugging process

While debugging why a class was being constructed repeatedly at build time, I added this snippet

class ContentStore {
    static _instance: ContentStore;
    constructor() {
        if (ContentStore._instance) {
            return ContentStore._instance;
        }
        ContentStore._instance = this;
        console.log("tree", process.pid, process.uptime());
        // expensive initialisation
    }
}

Leading to this output. There were 4 processes, so this was going to be initialised a minimum of 4 times anyway.

It was unclear why the last process was repeating it though.

āžœ npm run build

> [email protected] build
> next build && next export && npm run rssGen

info  - Using webpack 5. Reason: future.webpack5 option enabled https://nextjs.org/docs/messages/webpack5
info  - Checking validity of types
info  - Creating an optimized production build
info  - Compiled successfully
info  - Collecting page data .tree 48586 0.646691458
tree 48587 0.64301475
tree 48585 0.647991917
info  - Collecting page data
[   =] info  - Generating static pages (0/42)tree 48581 9.745386625
tree 48581 9.746271083
tree 48581 9.752843167
[    ] info  - Generating static pages (0/42)tree 48581 9.770965042
tree 48581 9.779078292
tree 48581 9.802078208
tree 48581 9.825228583
info  - Generating static pages (42/42)
info  - Finalizing page optimization