Skip to content

Instantly share code, notes, and snippets.

@tapyu
Last active May 27, 2024 18:14
Show Gist options
  • Save tapyu/ef8092ff720e4c10788d3094dc6a9a33 to your computer and use it in GitHub Desktop.
Save tapyu/ef8092ff720e4c10788d3094dc6a9a33 to your computer and use it in GitHub Desktop.
npm cheat sheet

npm (Node Package Manager) cheat sheet

I am pretty stupid when it comes to JavaScript and Node.js. That is a very elementary cheat sheet to minimaly handle npm packages on my Linux system.

Packages and cache

Command Description
npm list [--all] List installed packages locally [and its the dependences].
-g [--all] List installed packages globally instead [and its the dependences].
npm install [-g] pkg Install pkg locally [install it globally instead (you must sudo perimssions)].
npm cache [--force] clean Clear ~/.npm/_cache/ [forcibly].
{npx|npm exec} clear-npx-cache Clear ~/.npm/_npx/ (you need to install clear-npx-cache before with sudo npm install -g clear-npx-cache).

Executing

{npm exec|npx} pkg Execute command from a local or remote npm package. npx first looks in the local ./node_modules/.bin directory of your current working directory. If it finds the executable, it uses that version. If the executable is not found locally, npx checks the global (npm root -g) directory. If it finds the executable there (which typically happens if you have installed the package globally), it uses that version. If the executable is not found locally or globally, npx will download the package from the npm registry (such as registry.npmjs.org), if it is not already cached, and execute it. For each set of commands, arguments, and environments, npx download a set of `package.json`, `package-lock.json`, and `node_modules`, and put them into different directories under ~/.npm/_npx/, where each directory name is a 17-character hash that uniquely identifies each invocation. Each unique invocation of a command with npx may result in a different hash, leading to a separate directory in ~/.npm/_npx. Each directory in ~/.npm/_npx represents a separate instance of an npx invocation, and it serves as a cache for the binaries and related files needed to execute that specific command. Instead of re-downloading packages every time you run an already-downloaded instance, npm uses the ~/.npm/_npx/hash/ associated to that instance. After finding or downloading it, npx executes the command using the located binary within that environment.

Paths

npm root [-g] Print the npm path to the effective node_modules/ directory locally [Print globally instead].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment