NPM(3)

πŸ“¦ version 1.2.1, in nixos 26.05, nixpkgs unstable

πŸ’– NAME πŸ’– β€” NPM(3), the javascript package manager API

  • npm β€” javascript package manager πŸŽ€

okay so this is NPM(3), bestie β€” the API manual, not the CLI manual. for the command line client docs, see npm help npm. πŸ’…


✨ SYNOPSIS ✨

  var npm = require("npm")
  npm.load([configObject, ]function (er, npm) {
    // use the npm object, now that it's loaded.

    npm.config.set(key, val)
    val = npm.config.get(key)

    console.log("prefix = %s", npm.prefix)

    npm.commands.install(["package"], cb)
  })

πŸ“ VERSION πŸ“

2.15.12 β€” that's the version this page is serving, no cap. 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so this is the API documentation for npm. to find documentation of the command line client, see npm help npm. πŸŽ€

prior to using npm's commands, npm.load() must be called. literally must. if you provide configObject as an object map of top-level configs, those override the values stored in the various config locations. in the npm command line client, this set of configs is parsed from the command line options. additional configuration params are loaded from two configuration files. see npm help npm-config, npm help 7 npm-config, and npm help 5 npmrc for more information. iykyk. ✨

after that, each of the functions is accessible in the commands object: npm.commands.<cmd>. see npm help 7 npm-index for a list of all possible commands. πŸ‘‘

all commands on the command object take an array of positional argument strings. the last argument to any function is a callback. some commands take other optional arguments. periodt.

configs cannot currently be set on a per function basis, bestie 😭 β€” as each call to npm.config.set will change the value for all npm commands in that process. no cap.

to find API documentation for a specific command, run the npm apihelp command. slay. 🌸


πŸ‘‘ METHODS AND PROPERTIES πŸ‘‘

here's the whole cast β€” main character energy only. πŸ’–

  • npm.load(configs, cb) β€” load the configuration params, and call the cb function once the globalconfig and userconfig files have been loaded as well, or on nextTick if they've already been loaded. πŸŽ€
  • npm.config β€” an object for accessing npm configuration parameters. the config plug, basically. πŸ”Œ
  • npm.config.get(key) β€” the get call, signature literally npm.config.get(key). πŸ’…
  • npm.config.set(key, val) β€” the set call, signature literally npm.config.set(key, val). ✨
  • npm.config.del(key) β€” the delete call, signature literally npm.config.del(key). 🫢
  • npm.dir or npm.root β€” the node_modules directory where npm will operate. πŸ“
  • npm.prefix β€” the prefix where npm is operating. (most often the current working directory.) πŸ“
  • npm.cache β€” the place where npm keeps JSON and tarballs it fetches from the registry (or uploads to the registry). πŸ—ƒοΈ
  • npm.tmp β€” npm's temporary working directory. 🧚
  • npm.deref β€” get the "real" name for a command that has either an alias or abbreviation. πŸ”

πŸ”₯ MAGIC πŸ”₯

for each of the methods in the npm.commands object, a method is added to the npm object, which takes a set of positional string arguments rather than an array and a callback. it's giving built-in convenience. ✨

if the last argument is a callback, then it will use the supplied callback. however, if no callback is provided, then it will print out the error or results. πŸ’…

for example, this would work in a node repl:

> npm = require("npm")
> npm.load()  // wait a sec...
> npm.install("dnode", "express")

note that that won't work in a node program, since the install method will get called before the configuration load is completed. 😭


🌈 ABBREVS 🌈

in order to support npm ins foo instead of npm install foo, the npm.commands object has a set of abbreviations as well as the full method names. use the npm.deref method to find the real name. πŸ•΅οΈβ€β™€οΈ

for example:

var cmd = npm.deref("unp") // cmd === "unpublish"

that's the whole NPM(3) API tea, bestie β˜•πŸ’– β€” source/version: NPM(3), version 2.15.12, dated March 2017. periodt. 🫢

πŸ“¦ where this page comes from