• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# pacote [![npm version](https://img.shields.io/npm/v/pacote.svg)](https://npm.im/pacote) [![license](https://img.shields.io/npm/l/pacote.svg)](https://npm.im/pacote) [![Travis](https://img.shields.io/travis/npm/pacote.svg)](https://travis-ci.org/npm/pacote) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/npm/pacote?svg=true)](https://ci.appveyor.com/project/npm/pacote) [![Coverage Status](https://coveralls.io/repos/github/npm/pacote/badge.svg?branch=latest)](https://coveralls.io/github/npm/pacote?branch=latest)
2
3[`pacote`](https://github.com/npm/pacote) is a Node.js library for downloading
4[npm](https://npmjs.org)-compatible packages. It supports all package specifier
5syntax that `npm install` and its ilk support. It transparently caches anything
6needed to reduce excess operations, using [`cacache`](https://npm.im/cacache).
7
8## Install
9
10`$ npm install --save pacote`
11
12## Table of Contents
13
14* [Example](#example)
15* [Features](#features)
16* [Contributing](#contributing)
17* [API](#api)
18  * [`manifest`](#manifest)
19  * [`packument`](#packument)
20  * [`extract`](#extract)
21  * [`tarball`](#tarball)
22  * [`tarball.stream`](#tarball-stream)
23  * [`tarball.toFile`](#tarball-to-file)
24  * ~~[`prefetch`](#prefetch)~~ (deprecated)
25  * [`clearMemoized`](#clearMemoized)
26  * [`options`](#options)
27
28### Example
29
30```javascript
31const pacote = require('pacote')
32
33pacote.manifest('pacote@^1').then(pkg => {
34  console.log('package manifest for registry pkg:', pkg)
35  // { "name": "pacote", "version": "1.0.0", ... }
36})
37
38pacote.extract('http://hi.com/pkg.tgz', './here').then(() => {
39  console.log('remote tarball contents extracted to ./here')
40})
41```
42
43### Features
44
45* Handles all package types [npm](https://npm.im/npm) does
46* [high-performance, reliable, verified local cache](https://npm.im/cacache)
47* offline mode
48* authentication support (private git, private npm registries, etc)
49* github, gitlab, and bitbucket-aware
50* semver range support for git dependencies
51
52### Contributing
53
54The 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](CONTRIBUTING.md) 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.
55
56### API
57
58#### <a name="manifest"></a> `> pacote.manifest(spec, [opts])`
59
60Fetches the *manifest* for a package. Manifest objects are similar and based
61on the `package.json` for that package, but with pre-processed and limited
62fields. The object has the following shape:
63
64```javascript
65{
66  "name": PkgName,
67  "version": SemverString,
68  "dependencies": { PkgName: SemverString },
69  "optionalDependencies": { PkgName: SemverString },
70  "devDependencies": { PkgName: SemverString },
71  "peerDependencies": { PkgName: SemverString },
72  "bundleDependencies": false || [PkgName],
73  "bin": { BinName: Path },
74  "_resolved": TarballSource, // different for each package type
75  "_integrity": SubresourceIntegrityHash,
76  "_shrinkwrap": null || ShrinkwrapJsonObj
77}
78```
79
80Note that depending on the spec type, some additional fields might be present.
81For example, packages from `registry.npmjs.org` have additional metadata
82appended by the registry.
83
84##### Example
85
86```javascript
87pacote.manifest('pacote@1.0.0').then(pkgJson => {
88  // fetched `package.json` data from the registry
89})
90```
91
92#### <a name="packument"></a> `> pacote.packument(spec, [opts])`
93
94Fetches the *packument* for a package. Packument objects are general metadata
95about a project corresponding to registry metadata, and include version and
96`dist-tag` information about a package's available versions, rather than a
97specific version. It may include additional metadata not usually available
98through the individual package metadata objects.
99
100It generally looks something like this:
101
102```javascript
103{
104  "name": PkgName,
105  "dist-tags": {
106    'latest': VersionString,
107    [TagName]: VersionString,
108    ...
109  },
110  "versions": {
111    [VersionString]: Manifest,
112    ...
113  }
114}
115```
116
117Note that depending on the spec type, some additional fields might be present.
118For example, packages from `registry.npmjs.org` have additional metadata
119appended by the registry.
120
121##### Example
122
123```javascript
124pacote.packument('pacote').then(pkgJson => {
125  // fetched package versions metadata from the registry
126})
127```
128
129#### <a name="extract"></a> `> pacote.extract(spec, destination, [opts])`
130
131Extracts package data identified by `<spec>` into a directory named
132`<destination>`, which will be created if it does not already exist.
133
134If `opts.digest` is provided and the data it identifies is present in the cache,
135`extract` will bypass most of its operations and go straight to extracting the
136tarball.
137
138##### Example
139
140```javascript
141pacote.extract('pacote@1.0.0', './woot', {
142  digest: 'deadbeef'
143}).then(() => {
144  // Succeeds as long as `pacote@1.0.0` still exists somewhere. Network and
145  // other operations are bypassed entirely if `digest` is present in the cache.
146})
147```
148
149#### <a name="tarball"></a> `> pacote.tarball(spec, [opts])`
150
151Fetches package data identified by `<spec>` and returns the data as a buffer.
152
153This API has two variants:
154
155* `pacote.tarball.stream(spec, [opts])` - Same as `pacote.tarball`, except it returns a stream instead of a Promise.
156* `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.
157
158##### Example
159
160```javascript
161pacote.tarball('pacote@1.0.0', { cache: './my-cache' }).then(data => {
162  // data is the tarball data for pacote@1.0.0
163})
164```
165
166#### <a name="tarball-stream"></a> `> pacote.tarball.stream(spec, [opts])`
167
168Same as `pacote.tarball`, except it returns a stream instead of a Promise.
169
170##### Example
171
172```javascript
173pacote.tarball.stream('pacote@1.0.0')
174.pipe(fs.createWriteStream('./pacote-1.0.0.tgz'))
175```
176
177#### <a name="tarball-to-file"></a> `> pacote.tarball.toFile(spec, dest, [opts])`
178
179Like `pacote.tarball`, but instead of returning data directly, data will be
180written directly to `dest`, and create any required directories along the way.
181
182##### Example
183
184```javascript
185pacote.tarball.toFile('pacote@1.0.0', './pacote-1.0.0.tgz')
186.then(() => /* pacote tarball written directly to ./pacote-1.0.0.tgz */)
187```
188
189#### <a name="prefetch"></a> `> pacote.prefetch(spec, [opts])`
190
191##### THIS API IS DEPRECATED. USE `pacote.tarball()` INSTEAD
192
193Fetches package data identified by `<spec>`, usually for the purpose of warming
194up the local package cache (with `opts.cache`). It does not return anything.
195
196##### Example
197
198```javascript
199pacote.prefetch('pacote@1.0.0', { cache: './my-cache' }).then(() => {
200  // ./my-cache now has both the manifest and tarball for `pacote@1.0.0`.
201})
202```
203
204#### <a name="clearMemoized"></a> `> pacote.clearMemoized()`
205
206This utility function can be used to force pacote to release its references
207to any memoized data in its various internal caches. It might help free
208some memory.
209
210```javascript
211pacote.manifest(...).then(() => pacote.clearMemoized)
212
213```
214
215#### <a name="options"></a> `> options`
216
217`pacote` accepts [the options for
218`npm-registry-fetch`](https://npm.im/npm-registry-fetch#fetch-options) as-is,
219with a couple of additional `pacote-specific` ones:
220
221##### <a name="dirPacker"></a> `opts.dirPacker`
222
223* Type: Function
224* Default: Uses [`npm-packlist`](https://npm.im/npm-packlist) and [`tar`](https://npm.im/tar) to make a tarball.
225
226Expects a function that takes a single argument, `dir`, and returns a
227`ReadableStream` that outputs packaged tarball data. Used when creating tarballs
228for package specs that are not already packaged, such as git and directory
229dependencies. The default `opts.dirPacker` does not execute `prepare` scripts,
230even though npm itself does.
231
232##### <a name="opts-enjoy-by"></a> `opts.enjoy-by`
233
234* Alias: `opts.enjoyBy`, `opts.before`
235* Type: Date-able
236* Default: undefined
237
238If passed in, will be used while resolving to filter the versions for **registry
239dependencies** such that versions published **after** `opts.enjoy-by` are not
240considered -- as if they'd never been published.
241
242##### <a name="opts-include-deprecated"></a> `opts.include-deprecated`
243
244* Alias: `opts.includeDeprecated`
245* Type: Boolean
246* Default: false
247
248If false, deprecated versions will be skipped when selecting from registry range
249specifiers. If true, deprecations do not affect version selection.
250
251##### <a name="opts-full-metadata"></a> `opts.full-metadata`
252
253* Type: Boolean
254* Default: false
255
256If `true`, the full packument will be fetched when doing metadata requests. By
257defaul, `pacote` only fetches the summarized packuments, also called "corgis".
258
259##### <a name="opts-tag"></a> `opts.tag`
260
261* Alias: `opts.defaultTag`
262* Type: String
263* Default: `'latest'`
264
265Package version resolution tag. When processing registry spec ranges, this
266option is used to determine what dist-tag to treat as "latest". For more details
267about how `pacote` selects versions and how `tag` is involved, see [the
268documentation for `npm-pick-manifest`](https://npm.im/npm-pick-manifest).
269
270##### <a name="opts-resolved"></a> `opts.resolved`
271
272* Type: String
273* Default: null
274
275When fetching tarballs, this option can be passed in to skip registry metadata
276lookups when downloading tarballs. If the string is a `file:` URL, pacote will
277try to read the referenced local file before attempting to do any further
278lookups. This option does not bypass integrity checks when `opts.integrity` is
279passed in.
280
281##### <a name="opts-where"></a> `opts.where`
282
283* Type: String
284* Default: null
285
286Passed as an argument to [`npm-package-arg`](https://npm.im/npm-package-arg)
287when resolving `spec` arguments. Used to determine what path to resolve local
288path specs relatively from.
289