npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nodejs-scope

v2.1.2

Published

For working with environmens on NodeJS (optionally with PM2)

Downloads

11

Readme

Readme

Very simple proxy object that holds information about the current NodeJS process. It's useful for working with environments on NodeJS, especially in combination with PM2.

This is for everyone who likes to do things like this:

import scope from "nodejs-scope"

if(scope.dev) {
    //foo
} elseif (scope.env === "my-custom-environment") {
    //baz
}

scope is a proxy object containing fallowing getters:

  • pid: ID of the process
  • apn: Name of the process ([PM2] environment variable name)
  • cpu: List auf CPUs available
  • core: Number of CPU cores. Could be used for PM2 to set exec_mode and instances dynamically based on cores count.
  • realm: Group name that the process belongs to ([PM2] environment variable namespace)
  • env: Environment name of the process (refers to NOVE_ENV)
  • prod: Returns true if env contains one of ["prd", "prod", "production"]
  • stg: Returns true if env contains one of ["stg", "stage", "staging"]
  • dev: Return true if env contains one of ["dev", "development"]
  • localhost: Returns true if hostname contains "local"
  • manager: Returns true if apn contains one of ["master", "primary", "manager", "lead", "main"]. If PM2 runs in 'cluster' mode then could be used to detect if the process is a master or a slave.
  • worker: Returns true if apn contains one of ["slave", "secondary", "worker", "replica"]
  • any: Returns always true

Example

import scope from "nodejs-scope"

const {pid, apn, env} = scope // unpack selected

console.log(scope)
console.log(pid, apn, env)

switch(env) {
    case "production: {
        // production environment
        break
    }
    case "development":
    default: {
        // environmant is "development" or something other
    }
}

if(scope.prod && scope.manager) {
    // production environment on leading application instance
}

NodeJS

Most of the properties mentioned above aren't very useful when working with NodeJS exclusevly, except for pid, cpu, core, env, prod, stg, dev. The other properties come in handy when working with PM2 or Doncron.

For example, if you'd run NODE_ENV=staging node ./server.js then your process would have an environmental variable 'NODE_ENV' which maps through the proxy object scope to the property scope.env.

If you'd run your server through PM2 you'd certainly have more environmental variables like apn, realm. Or, if you plan to generate your PM2 config dynamically then you'll appreciate getters like cpu, core.

Implimentation details can be found here.

PM2

When working with 'ecosystem.config.js' in PM2, I tend to fallow simple naming conventions.

The name property of every application contains the evironment and the type of the instance. For example, I name my app prod_master.

If I have many instances of the same application (as a fallback or for load ballancing) then I name instances like dev_master, dev_worker.

By having the name property contain the environment, I can call pm2 list and immediately tell the difference of application environments.

By having the name contain a type identifier like 'manager' I can immediately tell if the application is master or an instance of a bigger swarm of workers - which becomes handy for cronjobs (since a cronjob only needs to run once - on the master instance of the application - not its slaves). Later, I can use scope.manager and scope.worker to separate the applications apart.