Installing global npm packages from a requirements file

Sometimes there is a need to install global npm packages from a list of packages. With npm, there is no command line flag to specify a requirements file, unlike with python's pip where a simple pip install --requirement <requirement> is all that is needed.

Here is how to install global npm packages using some command line utilities:

sed 's/#.*//' npm-requirements.txt | xargs npm install -g

The requirements file (e.g. npm-requirements.txt) can be formatted like this (the requirements file can include comments too!):

# Languages.

# Tools.
electron  # For GUIs.

    # It's also okay for comments to be preceeded by whitespace.

Notice that comments start with the # character and can either be inline or be on their own line.

How it works

sed 's/#.*//' <file> reads the file and outputs the contents of the file with all occurrences of things starting with the character # substituted with nothing, effectively removing the comments.

The text that has been processed with sed is the piped (i.e. fed) into xargs, which constructs the arguments for npm install -g using the text that was fed into xargs.

Hope this helps.