npm 中文文档 npm 中文文档
指南
npmjs.com (opens new window)
指南
npmjs.com (opens new window)
  • 快速入门

    • npm 是什么?
    • npm 安装和更新
    • npm 防止权限错误
    • npm package.json 文件
    • npm 安装包
    • npm 更新包
    • npm 卸载包
    • npm 创建 Node.js 模块
    • npm 发布和更新包
    • npm 使用语义化版本
    • npm 使用 Dist-tags 标记包
    • npm 包和模块的了解
  • 命令行
  • 配置 npm

pacote    


NOTE: This repo has moved to https://github.com/npm/pacote and only exists for archival purposes.

pacote is a Node.js library for downloading npm -compatible packages. It supports all package specifier syntax that npm install and its ilk support. It transparently caches anything needed to reduce excess operations, using cacache.

Install


$ npm install --save pacote

Table of Contents


Example
Features
Contributing
API
manifest
packument
extract
tarball
tarball.stream
tarball.toFile
prefetch * (deprecated)
clearMemoized
options

Example


  1. ``` js
  2. const pacote = require('pacote')

  3. pacote.manifest('pacote@^1').then(pkg => {
  4.   console.log('package manifest for registry pkg:', pkg)
  5.   // { "name": "pacote", "version": "1.0.0", ... }
  6. })

  7. pacote.extract('http://hi.com/pkg.tgz', './here').then(() => {
  8.   console.log('remote tarball contents extracted to ./here')
  9. })
  10. ```

Features


Handles all package types npm does
high-performance, reliable, verified local cache
offline mode
authentication support (private git, private npm registries, etc)
github, gitlab, and bitbucket-aware
semver range support for git dependencies

Contributing


The pacote team enthusiastically welcomes contributions and project participation! There's a bunch of things you can do if you want to contribute! The Contributor Guide has all the information you need for everything from reporting bugs to contributing entire new features. Please don't hesitate to jump in if you'd like to, or even ask us questions if something isn't clear.

API


> pacote.manifest(spec, [opts])


Fetches the manifestfor a package. Manifest objects are similar and based on the package.json for that package, but with pre-processed and limited fields. The object has the following shape:

  1. ``` js
  2. {
  3.   "name": PkgName,
  4.   "version": SemverString,
  5.   "dependencies": { PkgName: SemverString },
  6.   "optionalDependencies": { PkgName: SemverString },
  7.   "devDependencies": { PkgName: SemverString },
  8.   "peerDependencies": { PkgName: SemverString },
  9.   "bundleDependencies": false || [PkgName],
  10.   "bin": { BinName: Path },
  11.   "_resolved": TarballSource, // different for each package type
  12.   "_integrity": SubresourceIntegrityHash,
  13.   "_shrinkwrap": null || ShrinkwrapJsonObj
  14. }
  15. ```

Note that depending on the spec type, some additional fields might be present. For example, packages from registry.npmjs.org have additional metadata appended by the registry.

Example

  1. ``` js
  2. pacote.manifest('pacote@1.0.0').then(pkgJson => {
  3.   // fetched `package.json` data from the registry
  4. })
  5. ```

> pacote.packument(spec, [opts])


Fetches the packumentfor a package. Packument objects are general metadata about a project corresponding to registry metadata, and include version and dist-tag information about a package's available versions, rather than a specific version. It may include additional metadata not usually available through the individual package metadata objects.

It generally looks something like this:

  1. ``` js
  2. {
  3.   "name": PkgName,
  4.   "dist-tags": {
  5.     'latest': VersionString,
  6.     [TagName]: VersionString,
  7.     ...
  8.   },
  9.   "versions": {
  10.     [VersionString]: Manifest,
  11.     ...
  12.   }
  13. }
  14. ```

Note that depending on the spec type, some additional fields might be present. For example, packages from registry.npmjs.org have additional metadata appended by the registry.

Example

  1. ``` js
  2. pacote.packument('pacote').then(pkgJson => {
  3.   // fetched package versions metadata from the registry
  4. })
  5. ```

> pacote.extract(spec, destination, [opts])


Extracts package data identified by <spec> into a directory named <destination>, which will be created if it does not already exist.

If opts.digest is provided and the data it identifies is present in the cache, extract will bypass most of its operations and go straight to extracting the tarball.

Example

  1. ``` js
  2. pacote.extract('pacote@1.0.0', './woot', {
  3.   digest: 'deadbeef'
  4. }).then(() => {
  5.   // Succeeds as long as `pacote@1.0.0` still exists somewhere. Network and
  6.   // other operations are bypassed entirely if `digest` is present in the cache.
  7. })
  8. ```

> pacote.tarball(spec, [opts])


Fetches package data identified by <spec> and returns the data as a buffer.

This API has two variants:

pacote.tarball.stream(spec, [opts]) - Same as pacote.tarball, except it returns a stream instead of a Promise.
pacote.tarball.toFile(spec, dest, [opts]) - Instead of returning data directly, data will be written directly to dest, and create any required directories along the way.

Example

  1. ``` js
  2. pacote.tarball('pacote@1.0.0', { cache: './my-cache' }).then(data => {
  3.   // data is the tarball data for pacote@1.0.0
  4. })
  5. ```

> pacote.tarball.stream(spec, [opts])


Same as pacote.tarball, except it returns a stream instead of a Promise.

Example

  1. ``` js
  2. pacote.tarball.stream('pacote@1.0.0')
  3. .pipe(fs.createWriteStream('./pacote-1.0.0.tgz'))
  4. ```

> pacote.tarball.toFile(spec, dest, [opts])


Like pacote.tarball, but instead of returning data directly, data will be written directly to dest, and create any required directories along the way.

Example

  1. ``` js
  2. pacote.tarball.toFile('pacote@1.0.0', './pacote-1.0.0.tgz')
  3. .then(() => /* pacote tarball written directly to ./pacote-1.0.0.tgz */)
  4. ```

> pacote.prefetch(spec, [opts])


THIS API IS DEPRECATED. USE pacote.tarball() INSTEAD

Fetches package data identified by <spec>, usually for the purpose of warming up the local package cache (with opts.cache ). It does not return anything.

Example

  1. ``` js
  2. pacote.prefetch('pacote@1.0.0', { cache: './my-cache' }).then(() => {
  3.   // ./my-cache now has both the manifest and tarball for `pacote@1.0.0`.
  4. })
  5. ```

> pacote.clearMemoized()


This utility function can be used to force pacote to release its references to any memoized data in its various internal caches. It might help free some memory.

  1. ``` js
  2. pacote.manifest(...).then(() => pacote.clearMemoized)
  3. ```

> options


pacote accepts the options fornpm-registry-fetch as-is, with a couple of additional pacote-specific ones:

opts.dirPacker

Type: Function
Default: Uses npm-packlist and tar to make a tarball.

Expects a function that takes a single argument, dir, and returns a ReadableStream that outputs packaged tarball data. Used when creating tarballs for package specs that are not already packaged, such as git and directory dependencies. The default opts.dirPacker does not execute prepare scripts, even though npm itself does.

opts.enjoy-by

Alias: opts.enjoyBy, opts.before
Type: Date-able
Default: undefined

If passed in, will be used while resolving to filter the versions for registry dependenciessuch that versions published afteropts.enjoy-by are not considered -- as if they'd never been published.

opts.include-deprecated

Alias: opts.includeDeprecated
Type: Boolean
Default: false

If false, deprecated versions will be skipped when selecting from registry range specifiers. If true, deprecations do not affect version selection.

opts.full-metadata

Type: Boolean
Default: false

If true, the full packument will be fetched when doing metadata requests. By defaul, pacote only fetches the summarized packuments, also called "corgis".

opts.tag

Alias: opts.defaultTag
Type: String
Default: 'latest'

Package version resolution tag. When processing registry spec ranges, this option is used to determine what dist-tag to treat as "latest". For more details about how pacote selects versions and how tag is involved, see thedocumentation for npm-pick-manifest.

opts.resolved

Type: String
Default: null

When fetching tarballs, this option can be passed in to skip registry metadata lookups when downloading tarballs. If the string is a file: URL, pacote will try to read the referenced local file before attempting to do any further lookups. This option does not bypass integrity checks when opts.integrity is passed in.

opts.where

Type: String
Default: null

Passed as an argument to npm-package-arg when resolving spec arguments. Used to determine what path to resolve local path specs relatively from.
Last Updated: 2023-05-15 10:22:02