w3resource

Comprehensive Guide to npm-build, npm-cache, and npm-bundle Commands


In our previous tutorial we looked at the audit, bin and bugs cli commands, in this tutorial we will examine how the npm-build, npm-bundle and npm-cache work.

Npm-build

The npm build is used to build a package, the synopsis is given in the next section.

Synopsis

npm build [<package-folder>]

where <package-folder> is a folder that contains a package.json in its root.

Description

This is the plumbing command that is called by npm link and npm install.

Generally, this command is called during installation, however, if you need to run it directly, you can run:

npm run-script build

Npm-bundle

The npm-bundle command has been removed. Npm removed the command as from v1.0, the reason behind the removal is that the default behavior now is to install packages into the local space.

Hence, you should just use the npm install to do what the npm bundle used to do.

Npm-cache

The npm cache is used to manipulate the cache of packages.

Synopsis

npm cache add <tarball file>
npm cache add <folder>
npm cache add <tarball url>
npm cache add <name>@<version>
npm cache clean [<path>]
aliases: npm cache clear, npm cache rm

npm cache verify

Description

The npm-cache cli command is used to add, list, or clean the npm cache folder.

  • add: You use this to add the specified package to the local cache. This command is primarily intended to be used by npm internally, but it can equally provide a way to add data to the local installation cache explicitly.
  • clean: it can also be used to delete all data out of the cache folder.
  • verify: Finally, it can verify the contents of the cache folder, garbage collecting any data that is not needed, and verifies the integrity of the cache index and all the cached data.

DETAILS

npm will store cache data in an opaque directory within the configured cache, that is named _cacache. This directory is a cacache-based content-addressable cache that will store all http request data as well as other package-related data. The primary way to access this directory is through the pacote, which is the library responsible for all package fetching as of npm@5.

All the data that passes through the cache is fully verified for integrity on both insertion and extraction. Cache corruption either triggers an error, or signals to pacote that the data has to be refetched, which it does automatically. This is why it is expected you never have to clear the cache for any reason aside reclaiming disk space, this explains why clean now requires --force to run.

Currently there is no method exposed through npm to inspect or directly manage the contents of this cache. If you have to access it, you have to used cacache directly.

npm does not remove data by itself: the cache grows as new packages are installed.

A NOTE ON THE CACHE'S DESIGN

The npm cache is a cache and nothing more: You should not rely on it as a persistent and reliable data store for package data. npm cannot guarantee that a previously-cached piece of data will be available later, also, npm automatically deletes corrupted contents. The primary guarantee that the cache makes is that, when it does return data, that data will be exactly the data that was inserted into it.

If you want to run an offline verification of existing cache contents, you should use npm cache verify.

Configuration

cache

This is the root cache folder.

Default: ~/.npm on Posix, or %AppData%/npm-cache on Windows.

Previous: Comprehensive Guide to npm-audit, npm-bin, and npm-bugs Commands.
Next: Comprehensive Guide to npm-ci, npm-completion, and npm-config Commands.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/npm/build-bundle-and-cache-commands.php