• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1---
2title: package.json
3section: 5
4description: Specifics of npm's package.json handling
5---
6
7### Description
8
9This document is all you need to know about what's required in your
10package.json file.  It must be actual JSON, not just a JavaScript object
11literal.
12
13A lot of the behavior described in this document is affected by the config
14settings described in [`config`](/using-npm/config).
15
16### name
17
18If you plan to publish your package, the *most* important things in your
19package.json are the name and version fields as they will be required. The
20name and version together form an identifier that is assumed to be
21completely unique.  Changes to the package should come along with changes
22to the version. If you don't plan to publish your package, the name and
23version fields are optional.
24
25The name is what your thing is called.
26
27Some rules:
28
29* The name must be less than or equal to 214 characters. This includes the
30  scope for scoped packages.
31* The names of scoped packages can begin with a dot or an underscore. This
32  is not permitted without a scope.
33* New packages must not have uppercase letters in the name.
34* The name ends up being part of a URL, an argument on the command line,
35  and a folder name. Therefore, the name can't contain any non-URL-safe
36  characters.
37
38Some tips:
39
40* Don't use the same name as a core Node module.
41* Don't put "js" or "node" in the name.  It's assumed that it's js, since
42  you're writing a package.json file, and you can specify the engine using
43  the "engines" field.  (See below.)
44* The name will probably be passed as an argument to require(), so it
45  should be something short, but also reasonably descriptive.
46* You may want to check the npm registry to see if there's something by
47  that name already, before you get too attached to it.
48  <https://www.npmjs.com/>
49
50A name can be optionally prefixed by a scope, e.g. `@myorg/mypackage`. See
51[`scope`](/using-npm/scope) for more detail.
52
53### version
54
55If you plan to publish your package, the *most* important things in your
56package.json are the name and version fields as they will be required. The
57name and version together form an identifier that is assumed to be
58completely unique.  Changes to the package should come along with changes
59to the version. If you don't plan to publish your package, the name and
60version fields are optional.
61
62Version must be parseable by
63[node-semver](https://github.com/npm/node-semver), which is bundled with
64npm as a dependency.  (`npm install semver` to use it yourself.)
65
66### description
67
68Put a description in it.  It's a string.  This helps people discover your
69package, as it's listed in `npm search`.
70
71### keywords
72
73Put keywords in it.  It's an array of strings.  This helps people discover
74your package as it's listed in `npm search`.
75
76### homepage
77
78The url to the project homepage.
79
80Example:
81
82```json
83"homepage": "https://github.com/owner/project#readme"
84```
85
86### bugs
87
88The url to your project's issue tracker and / or the email address to which
89issues should be reported. These are helpful for people who encounter
90issues with your package.
91
92It should look like this:
93
94```json
95{
96  "url" : "https://github.com/owner/project/issues",
97  "email" : "project@hostname.com"
98}
99```
100
101You can specify either one or both values. If you want to provide only a
102url, you can specify the value for "bugs" as a simple string instead of an
103object.
104
105If a url is provided, it will be used by the `npm bugs` command.
106
107### license
108
109You should specify a license for your package so that people know how they
110are permitted to use it, and any restrictions you're placing on it.
111
112If you're using a common license such as BSD-2-Clause or MIT, add a current
113SPDX license identifier for the license you're using, like this:
114
115```json
116{
117  "license" : "BSD-3-Clause"
118}
119```
120
121You can check [the full list of SPDX license
122IDs](https://spdx.org/licenses/).  Ideally you should pick one that is
123[OSI](https://opensource.org/licenses/) approved.
124
125If your package is licensed under multiple common licenses, use an [SPDX
126license expression syntax version 2.0
127string](https://spdx.dev/specifications/), like this:
128
129```json
130{
131  "license" : "(ISC OR GPL-3.0)"
132}
133```
134If you are using a license that hasn't been assigned an SPDX identifier, or if
135you are using a custom license, use a string value like this one:
136
137```json
138{
139  "license" : "SEE LICENSE IN <filename>"
140}
141```
142Then include a file named `<filename>` at the top level of the package.
143
144Some old packages used license objects or a "licenses" property containing
145an array of license objects:
146
147```json
148// Not valid metadata
149{
150  "license" : {
151    "type" : "ISC",
152    "url" : "https://opensource.org/licenses/ISC"
153  }
154}
155
156// Not valid metadata
157{
158  "licenses" : [
159    {
160      "type": "MIT",
161      "url": "https://www.opensource.org/licenses/mit-license.php"
162    },
163    {
164      "type": "Apache-2.0",
165      "url": "https://opensource.org/licenses/apache2.0.php"
166    }
167  ]
168}
169```
170
171Those styles are now deprecated. Instead, use SPDX expressions, like this:
172
173```json
174{
175  "license": "ISC"
176}
177```
178
179```json
180{
181  "license": "(MIT OR Apache-2.0)"
182}
183```
184
185Finally, if you do not wish to grant others the right to use a private or
186unpublished package under any terms:
187
188```json
189{
190  "license": "UNLICENSED"
191}
192```
193
194Consider also setting `"private": true` to prevent accidental publication.
195
196### people fields: author, contributors
197
198The "author" is one person.  "contributors" is an array of people.  A
199"person" is an object with a "name" field and optionally "url" and "email",
200like this:
201
202```json
203{
204  "name" : "Barney Rubble",
205  "email" : "b@rubble.com",
206  "url" : "http://barnyrubble.tumblr.com/"
207}
208```
209
210Or you can shorten that all into a single string, and npm will parse it for
211you:
212
213```json
214{
215  "author": "Barney Rubble <b@rubble.com> (http://barnyrubble.tumblr.com/)"
216}
217```
218
219Both email and url are optional either way.
220
221npm also sets a top-level "maintainers" field with your npm user info.
222
223### funding
224
225You can specify an object containing a URL that provides up-to-date
226information about ways to help fund development of your package, or a
227string URL, or an array of these:
228
229```json
230{
231  "funding": {
232    "type" : "individual",
233    "url" : "http://example.com/donate"
234  },
235
236  "funding": {
237    "type" : "patreon",
238    "url" : "https://www.patreon.com/my-account"
239  },
240
241  "funding": "http://example.com/donate",
242
243  "funding": [
244    {
245      "type" : "individual",
246      "url" : "http://example.com/donate"
247    },
248    "http://example.com/donateAlso",
249    {
250      "type" : "patreon",
251      "url" : "https://www.patreon.com/my-account"
252    }
253  ]
254}
255```
256
257Users can use the `npm fund` subcommand to list the `funding` URLs of all
258dependencies of their project, direct and indirect. A shortcut to visit
259each funding url is also available when providing the project name such as:
260`npm fund <projectname>` (when there are multiple URLs, the first one will
261be visited)
262
263### files
264
265The optional `files` field is an array of file patterns that describes the
266entries to be included when your package is installed as a dependency. File
267patterns follow a similar syntax to `.gitignore`, but reversed: including a
268file, directory, or glob pattern (`*`, `**/*`, and such) will make it so
269that file is included in the tarball when it's packed. Omitting the field
270will make it default to `["*"]`, which means it will include all files.
271
272Some special files and directories are also included or excluded regardless
273of whether they exist in the `files` array (see below).
274
275You can also provide a `.npmignore` file in the root of your package or in
276subdirectories, which will keep files from being included. At the root of
277your package it will not override the "files" field, but in subdirectories
278it will. The `.npmignore` file works just like a `.gitignore`. If there is
279a `.gitignore` file, and `.npmignore` is missing, `.gitignore`'s contents
280will be used instead.
281
282Certain files are always included, regardless of settings:
283
284* `package.json`
285* `README`
286* `LICENSE` / `LICENCE`
287* The file in the "main" field
288
289`README` & `LICENSE` can have any case and extension.
290
291Conversely, some files are always ignored:
292
293* `.git`
294* `CVS`
295* `.svn`
296* `.hg`
297* `.lock-wscript`
298* `.wafpickle-N`
299* `.*.swp`
300* `.DS_Store`
301* `._*`
302* `npm-debug.log`
303* `.npmrc`
304* `node_modules`
305* `config.gypi`
306* `*.orig`
307* `package-lock.json` (use
308  [`npm-shrinkwrap.json`](/configuring-npm/npm-shrinkwrap-json) if you wish
309  it to be published)
310
311### main
312
313The main field is a module ID that is the primary entry point to your
314program.  That is, if your package is named `foo`, and a user installs it,
315and then does `require("foo")`, then your main module's exports object will
316be returned.
317
318This should be a module relative to the root of your package folder.
319
320For most modules, it makes the most sense to have a main script and often
321not much else.
322
323If `main` is not set, it defaults to `index.js` in the package's root folder.
324
325### browser
326
327If your module is meant to be used client-side the browser field should be
328used instead of the main field. This is helpful to hint users that it might
329rely on primitives that aren't available in Node.js modules. (e.g.
330`window`)
331
332### bin
333
334A lot of packages have one or more executable files that they'd like to
335install into the PATH. npm makes this pretty easy (in fact, it uses this
336feature to install the "npm" executable.)
337
338To use this, supply a `bin` field in your package.json which is a map of
339command name to local file name. When this package is installed globally,
340that file will be either linked inside the global bins directory or
341a cmd (Windows Command File) will be created which executes the specified
342file in the `bin` field, so it is available to run by `name` or `name.cmd` (on
343Windows PowerShell). When this package is installed as a dependency in another
344package, the file will be linked where it will be available to that package
345either directly by `npm exec` or by name in other scripts when invoking them
346via `npm run-script`.
347
348
349For example, myapp could have this:
350
351```json
352{
353  "bin": {
354    "myapp": "./cli.js"
355  }
356}
357```
358
359So, when you install myapp, in case of unix-like OS it'll create a symlink
360from the `cli.js` script to `/usr/local/bin/myapp` and in case of windows it
361will create a cmd file usually at `C:\Users\{Username}\AppData\Roaming\npm\myapp.cmd`
362which runs the `cli.js` script.
363
364If you have a single executable, and its name should be the name of the
365package, then you can just supply it as a string.  For example:
366
367```json
368{
369  "name": "my-program",
370  "version": "1.2.5",
371  "bin": "./path/to/program"
372}
373```
374
375would be the same as this:
376
377```json
378{
379  "name": "my-program",
380  "version": "1.2.5",
381  "bin": {
382    "my-program": "./path/to/program"
383  }
384}
385```
386
387Please make sure that your file(s) referenced in `bin` starts with
388`#!/usr/bin/env node`, otherwise the scripts are started without the node
389executable!
390
391Note that you can also set the executable files using [directories.bin](#directoriesbin).
392
393See [folders](/configuring-npm/folders#executables) for more info on
394executables.
395
396### man
397
398Specify either a single file or an array of filenames to put in place for
399the `man` program to find.
400
401If only a single file is provided, then it's installed such that it is the
402result from `man <pkgname>`, regardless of its actual filename.  For
403example:
404
405```json
406{
407  "name": "foo",
408  "version": "1.2.3",
409  "description": "A packaged foo fooer for fooing foos",
410  "main": "foo.js",
411  "man": "./man/doc.1"
412}
413```
414
415would link the `./man/doc.1` file in such that it is the target for `man
416foo`
417
418If the filename doesn't start with the package name, then it's prefixed.
419So, this:
420
421```json
422{
423  "name": "foo",
424  "version": "1.2.3",
425  "description": "A packaged foo fooer for fooing foos",
426  "main": "foo.js",
427  "man": [
428    "./man/foo.1",
429    "./man/bar.1"
430  ]
431}
432```
433
434will create files to do `man foo` and `man foo-bar`.
435
436Man files must end with a number, and optionally a `.gz` suffix if they are
437compressed.  The number dictates which man section the file is installed
438into.
439
440```json
441{
442  "name": "foo",
443  "version": "1.2.3",
444  "description": "A packaged foo fooer for fooing foos",
445  "main": "foo.js",
446  "man": [
447    "./man/foo.1",
448    "./man/foo.2"
449  ]
450}
451```
452
453will create entries for `man foo` and `man 2 foo`
454
455### directories
456
457The CommonJS [Packages](http://wiki.commonjs.org/wiki/Packages/1.0) spec
458details a few ways that you can indicate the structure of your package
459using a `directories` object. If you look at [npm's
460package.json](https://registry.npmjs.org/npm/latest), you'll see that it
461has directories for doc, lib, and man.
462
463In the future, this information may be used in other creative ways.
464
465#### directories.bin
466
467If you specify a `bin` directory in `directories.bin`, all the files in
468that folder will be added.
469
470Because of the way the `bin` directive works, specifying both a `bin` path
471and setting `directories.bin` is an error. If you want to specify
472individual files, use `bin`, and for all the files in an existing `bin`
473directory, use `directories.bin`.
474
475#### directories.man
476
477A folder that is full of man pages.  Sugar to generate a "man" array by
478walking the folder.
479
480### repository
481
482Specify the place where your code lives. This is helpful for people who
483want to contribute.  If the git repo is on GitHub, then the `npm docs`
484command will be able to find you.
485
486Do it like this:
487
488```json
489{
490  "repository": {
491    "type": "git",
492    "url": "https://github.com/npm/cli.git"
493  }
494}
495```
496
497The URL should be a publicly available (perhaps read-only) url that can be
498handed directly to a VCS program without any modification.  It should not
499be a url to an html project page that you put in your browser.  It's for
500computers.
501
502For GitHub, GitHub gist, Bitbucket, or GitLab repositories you can use the
503same shortcut syntax you use for `npm install`:
504
505```json
506{
507  "repository": "npm/npm",
508
509  "repository": "github:user/repo",
510
511  "repository": "gist:11081aaa281",
512
513  "repository": "bitbucket:user/repo",
514
515  "repository": "gitlab:user/repo"
516}
517```
518
519If the `package.json` for your package is not in the root directory (for
520example if it is part of a monorepo), you can specify the directory in
521which it lives:
522
523```json
524{
525  "repository": {
526    "type": "git",
527    "url": "https://github.com/facebook/react.git",
528    "directory": "packages/react-dom"
529  }
530}
531```
532
533### scripts
534
535The "scripts" property is a dictionary containing script commands that are
536run at various times in the lifecycle of your package.  The key is the
537lifecycle event, and the value is the command to run at that point.
538
539See [`scripts`](/using-npm/scripts) to find out more about writing package
540scripts.
541
542### config
543
544A "config" object can be used to set configuration parameters used in
545package scripts that persist across upgrades.  For instance, if a package
546had the following:
547
548```json
549{
550  "name": "foo",
551  "config": {
552    "port": "8080"
553  }
554}
555```
556
557It could also have a "start" command that referenced the
558`npm_package_config_port` environment variable.
559
560### dependencies
561
562Dependencies are specified in a simple object that maps a package name to a
563version range. The version range is a string which has one or more
564space-separated descriptors.  Dependencies can also be identified with a
565tarball or git URL.
566
567**Please do not put test harnesses or transpilers or other "development"
568time tools in your `dependencies` object.**  See `devDependencies`, below.
569
570See [semver](https://github.com/npm/node-semver#versions) for more details about specifying version ranges.
571
572* `version` Must match `version` exactly
573* `>version` Must be greater than `version`
574* `>=version` etc
575* `<version`
576* `<=version`
577* `~version` "Approximately equivalent to version"  See
578  [semver](https://github.com/npm/node-semver#versions)
579* `^version` "Compatible with version"  See [semver](https://github.com/npm/node-semver#versions)
580* `1.2.x` 1.2.0, 1.2.1, etc., but not 1.3.0
581* `http://...` See 'URLs as Dependencies' below
582* `*` Matches any version
583* `""` (just an empty string) Same as `*`
584* `version1 - version2` Same as `>=version1 <=version2`.
585* `range1 || range2` Passes if either range1 or range2 are satisfied.
586* `git...` See 'Git URLs as Dependencies' below
587* `user/repo` See 'GitHub URLs' below
588* `tag` A specific version tagged and published as `tag`  See [`npm
589  dist-tag`](/commands/npm-dist-tag)
590* `path/path/path` See [Local Paths](#local-paths) below
591
592For example, these are all valid:
593
594```json
595{
596  "dependencies": {
597    "foo": "1.0.0 - 2.9999.9999",
598    "bar": ">=1.0.2 <2.1.2",
599    "baz": ">1.0.2 <=2.3.4",
600    "boo": "2.0.1",
601    "qux": "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0",
602    "asd": "http://asdf.com/asdf.tar.gz",
603    "til": "~1.2",
604    "elf": "~1.2.3",
605    "two": "2.x",
606    "thr": "3.3.x",
607    "lat": "latest",
608    "dyl": "file:../dyl"
609  }
610}
611```
612
613#### URLs as Dependencies
614
615You may specify a tarball URL in place of a version range.
616
617This tarball will be downloaded and installed locally to your package at
618install time.
619
620#### Git URLs as Dependencies
621
622Git urls are of the form:
623
624```bash
625<protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]
626```
627
628`<protocol>` is one of `git`, `git+ssh`, `git+http`, `git+https`, or
629`git+file`.
630
631If `#<commit-ish>` is provided, it will be used to clone exactly that
632commit. If the commit-ish has the format `#semver:<semver>`, `<semver>` can
633be any valid semver range or exact version, and npm will look for any tags
634or refs matching that range in the remote repository, much as it would for
635a registry dependency. If neither `#<commit-ish>` or `#semver:<semver>` is
636specified, then the default branch is used.
637
638Examples:
639
640```bash
641git+ssh://git@github.com:npm/cli.git#v1.0.27
642git+ssh://git@github.com:npm/cli#semver:^5.0
643git+https://isaacs@github.com/npm/cli.git
644git://github.com/npm/cli.git#v1.0.27
645```
646
647When installing from a `git` repository, the presence of certain fields in the
648`package.json` will cause npm to believe it needs to perform a build. To do so
649your repository will be cloned into a temporary directory, all of its deps
650installed, relevant scripts run, and the resulting directory packed and
651installed.
652
653This flow will occur if your git dependency uses `workspaces`, or if any of the
654following scripts are present:
655
656* `build`
657* `prepare`
658* `prepack`
659* `preinstall`
660* `install`
661* `postinstall`
662
663If your git repository includes pre-built artifacts, you will likely want to
664make sure that none of the above scripts are defined, or your dependency
665will be rebuilt for every installation.
666
667#### GitHub URLs
668
669As of version 1.1.65, you can refer to GitHub urls as just "foo":
670"user/foo-project".  Just as with git URLs, a `commit-ish` suffix can be
671included.  For example:
672
673```json
674{
675  "name": "foo",
676  "version": "0.0.0",
677  "dependencies": {
678    "express": "expressjs/express",
679    "mocha": "mochajs/mocha#4727d357ea",
680    "module": "user/repo#feature\/branch"
681  }
682}
683```
684
685#### Local Paths
686
687As of version 2.0.0 you can provide a path to a local directory that
688contains a package. Local paths can be saved using `npm install -S` or `npm
689install --save`, using any of these forms:
690
691```bash
692../foo/bar
693~/foo/bar
694./foo/bar
695/foo/bar
696```
697
698in which case they will be normalized to a relative path and added to your
699`package.json`. For example:
700
701```json
702{
703  "name": "baz",
704  "dependencies": {
705    "bar": "file:../foo/bar"
706  }
707}
708```
709
710This feature is helpful for local offline development and creating tests
711that require npm installing where you don't want to hit an external server,
712but should not be used when publishing packages to the public registry.
713
714*note*: Packages linked by local path will not have their own
715dependencies installed when `npm install` is ran in this case.  You must
716run `npm install` from inside the local path itself.
717
718### devDependencies
719
720If someone is planning on downloading and using your module in their
721program, then they probably don't want or need to download and build the
722external test or documentation framework that you use.
723
724In this case, it's best to map these additional items in a
725`devDependencies` object.
726
727These things will be installed when doing `npm link` or `npm install` from
728the root of a package, and can be managed like any other npm configuration
729param.  See [`config`](/using-npm/config) for more on the topic.
730
731For build steps that are not platform-specific, such as compiling
732CoffeeScript or other languages to JavaScript, use the `prepare` script to
733do this, and make the required package a devDependency.
734
735For example:
736
737```json
738{
739  "name": "ethopia-waza",
740  "description": "a delightfully fruity coffee varietal",
741  "version": "1.2.3",
742  "devDependencies": {
743    "coffee-script": "~1.6.3"
744  },
745  "scripts": {
746    "prepare": "coffee -o lib/ -c src/waza.coffee"
747  },
748  "main": "lib/waza.js"
749}
750```
751
752The `prepare` script will be run before publishing, so that users can
753consume the functionality without requiring them to compile it themselves.
754In dev mode (ie, locally running `npm install`), it'll run this script as
755well, so that you can test it easily.
756
757### peerDependencies
758
759In some cases, you want to express the compatibility of your package with a
760host tool or library, while not necessarily doing a `require` of this host.
761This is usually referred to as a *plugin*. Notably, your module may be
762exposing a specific interface, expected and specified by the host
763documentation.
764
765For example:
766
767```json
768{
769  "name": "tea-latte",
770  "version": "1.3.5",
771  "peerDependencies": {
772    "tea": "2.x"
773  }
774}
775```
776
777This ensures your package `tea-latte` can be installed *along* with the
778second major version of the host package `tea` only. `npm install
779tea-latte` could possibly yield the following dependency graph:
780
781```bash
782├── tea-latte@1.3.5
783└── tea@2.2.0
784```
785
786In npm versions 3 through 6, `peerDependencies` were not automatically
787installed, and would raise a warning if an invalid version of the peer
788dependency was found in the tree.  As of npm v7, peerDependencies _are_
789installed by default.
790
791Trying to install another plugin with a conflicting requirement may cause
792an error if the tree cannot be resolved correctly. For this reason, make
793sure your plugin requirement is as broad as possible, and not to lock it
794down to specific patch versions.
795
796Assuming the host complies with [semver](https://semver.org/), only changes
797in the host package's major version will break your plugin. Thus, if you've
798worked with every 1.x version of the host package, use `"^1.0"` or `"1.x"`
799to express this. If you depend on features introduced in 1.5.2, use
800`"^1.5.2"`.
801
802### peerDependenciesMeta
803
804When a user installs your package, npm will emit warnings if packages
805specified in `peerDependencies` are not already installed. The
806`peerDependenciesMeta` field serves to provide npm more information on how
807your peer dependencies are to be used. Specifically, it allows peer
808dependencies to be marked as optional.
809
810For example:
811
812```json
813{
814  "name": "tea-latte",
815  "version": "1.3.5",
816  "peerDependencies": {
817    "tea": "2.x",
818    "soy-milk": "1.2"
819  },
820  "peerDependenciesMeta": {
821    "soy-milk": {
822      "optional": true
823    }
824  }
825}
826```
827
828Marking a peer dependency as optional ensures npm will not emit a warning
829if the `soy-milk` package is not installed on the host. This allows you to
830integrate and interact with a variety of host packages without requiring
831all of them to be installed.
832
833### bundleDependencies
834
835This defines an array of package names that will be bundled when publishing
836the package.
837
838In cases where you need to preserve npm packages locally or have them
839available through a single file download, you can bundle the packages in a
840tarball file by specifying the package names in the `bundleDependencies`
841array and executing `npm pack`.
842
843For example:
844
845If we define a package.json like this:
846
847```json
848{
849  "name": "awesome-web-framework",
850  "version": "1.0.0",
851  "bundleDependencies": [
852    "renderized",
853    "super-streams"
854  ]
855}
856```
857
858we can obtain `awesome-web-framework-1.0.0.tgz` file by running `npm pack`.
859This file contains the dependencies `renderized` and `super-streams` which
860can be installed in a new project by executing `npm install
861awesome-web-framework-1.0.0.tgz`.  Note that the package names do not
862include any versions, as that information is specified in `dependencies`.
863
864If this is spelled `"bundledDependencies"`, then that is also honored.
865
866Alternatively, `"bundleDependencies"` can be defined as a boolean value. A
867value of `true` will bundle all dependencies, a value of `false` will bundle
868none.
869
870### optionalDependencies
871
872If a dependency can be used, but you would like npm to proceed if it cannot
873be found or fails to install, then you may put it in the
874`optionalDependencies` object.  This is a map of package name to version or
875url, just like the `dependencies` object.  The difference is that build
876failures do not cause installation to fail.  Running `npm install
877--omit=optional` will prevent these dependencies from being installed.
878
879It is still your program's responsibility to handle the lack of the
880dependency.  For example, something like this:
881
882```js
883try {
884  var foo = require('foo')
885  var fooVersion = require('foo/package.json').version
886} catch (er) {
887  foo = null
888}
889if ( notGoodFooVersion(fooVersion) ) {
890  foo = null
891}
892
893// .. then later in your program ..
894
895if (foo) {
896  foo.doFooThings()
897}
898```
899
900Entries in `optionalDependencies` will override entries of the same name in
901`dependencies`, so it's usually best to only put in one place.
902
903### overrides
904
905If you need to make specific changes to dependencies of your dependencies, for
906example replacing the version of a dependency with a known security issue,
907replacing an existing dependency with a fork, or making sure that the same
908version of a package is used everywhere, then you may add an override.
909
910Overrides provide a way to replace a package in your dependency tree with
911another version, or another package entirely. These changes can be scoped as
912specific or as vague as desired.
913
914To make sure the package `foo` is always installed as version `1.0.0` no matter
915what version your dependencies rely on:
916
917```json
918{
919  "overrides": {
920    "foo": "1.0.0"
921  }
922}
923```
924
925The above is a short hand notation, the full object form can be used to allow
926overriding a package itself as well as a child of the package. This will cause
927`foo` to always be `1.0.0` while also making `bar` at any depth beyond `foo`
928also `1.0.0`:
929
930```json
931{
932  "overrides": {
933    "foo": {
934      ".": "1.0.0",
935      "bar": "1.0.0"
936    }
937  }
938}
939```
940
941To only override `foo` to be `1.0.0` when it's a child (or grandchild, or great
942grandchild, etc) of the package `bar`:
943
944```json
945{
946  "overrides": {
947    "bar": {
948      "foo": "1.0.0"
949    }
950  }
951}
952```
953
954Keys can be nested to any arbitrary length. To override `foo` only when it's a
955child of `bar` and only when `bar` is a child of `baz`:
956
957```json
958{
959  "overrides": {
960    "baz": {
961      "bar": {
962        "foo": "1.0.0"
963      }
964    }
965  }
966}
967```
968
969The key of an override can also include a version, or range of versions.
970To override `foo` to `1.0.0`, but only when it's a child of `bar@2.0.0`:
971
972```json
973{
974  "overrides": {
975    "bar@2.0.0": {
976      "foo": "1.0.0"
977    }
978  }
979}
980```
981
982You may not set an override for a package that you directly depend on unless
983both the dependency and the override itself share the exact same spec. To make
984this limitation easier to deal with, overrides may also be defined as a
985reference to a spec for a direct dependency by prefixing the name of the
986package you wish the version to match with a `$`.
987
988```json
989{
990  "dependencies": {
991    "foo": "^1.0.0"
992  },
993  "overrides": {
994    // BAD, will throw an EOVERRIDE error
995    // "foo": "^2.0.0"
996    // GOOD, specs match so override is allowed
997    // "foo": "^1.0.0"
998    // BEST, the override is defined as a reference to the dependency
999    "foo": "$foo",
1000    // the referenced package does not need to match the overridden one
1001    "bar": "$foo"
1002  }
1003}
1004```
1005
1006### engines
1007
1008You can specify the version of node that your stuff works on:
1009
1010```json
1011{
1012  "engines": {
1013    "node": ">=0.10.3 <15"
1014  }
1015}
1016```
1017
1018And, like with dependencies, if you don't specify the version (or if you
1019specify "\*" as the version), then any version of node will do.
1020
1021You can also use the "engines" field to specify which versions of npm are
1022capable of properly installing your program.  For example:
1023
1024```json
1025{
1026  "engines": {
1027    "npm": "~1.0.20"
1028  }
1029}
1030```
1031
1032Unless the user has set the
1033[`engine-strict` config](/using-npm/config#engine-strict) flag, this field is
1034advisory only and will only produce warnings when your package is installed as a
1035dependency.
1036
1037### os
1038
1039You can specify which operating systems your
1040module will run on:
1041
1042```json
1043{
1044  "os": [
1045    "darwin",
1046    "linux"
1047  ]
1048}
1049```
1050
1051You can also block instead of allowing operating systems, just prepend the
1052blocked os with a '!':
1053
1054```json
1055{
1056  "os": [
1057    "!win32"
1058  ]
1059}
1060```
1061
1062The host operating system is determined by `process.platform`
1063
1064It is allowed to both block and allow an item, although there isn't any
1065good reason to do this.
1066
1067### cpu
1068
1069If your code only runs on certain cpu architectures,
1070you can specify which ones.
1071
1072```json
1073{
1074  "cpu": [
1075    "x64",
1076    "ia32"
1077  ]
1078}
1079```
1080
1081Like the `os` option, you can also block architectures:
1082
1083```json
1084{
1085  "cpu": [
1086    "!arm",
1087    "!mips"
1088  ]
1089}
1090```
1091
1092The host architecture is determined by `process.arch`
1093
1094### private
1095
1096If you set `"private": true` in your package.json, then npm will refuse to
1097publish it.
1098
1099This is a way to prevent accidental publication of private repositories.
1100If you would like to ensure that a given package is only ever published to
1101a specific registry (for example, an internal registry), then use the
1102`publishConfig` dictionary described below to override the `registry`
1103config param at publish-time.
1104
1105### publishConfig
1106
1107This is a set of config values that will be used at publish-time. It's
1108especially handy if you want to set the tag, registry or access, so that
1109you can ensure that a given package is not tagged with "latest", published
1110to the global public registry or that a scoped module is private by
1111default.
1112
1113See [`config`](/using-npm/config) to see the list of config options that
1114can be overridden.
1115
1116### workspaces
1117
1118The optional `workspaces` field is an array of file patterns that describes
1119locations within the local file system that the install client should look
1120up to find each [workspace](/using-npm/workspaces) that needs to be
1121symlinked to the top level `node_modules` folder.
1122
1123It can describe either the direct paths of the folders to be used as
1124workspaces or it can define globs that will resolve to these same folders.
1125
1126In the following example, all folders located inside the folder
1127`./packages` will be treated as workspaces as long as they have valid
1128`package.json` files inside them:
1129
1130```json
1131{
1132  "name": "workspace-example",
1133  "workspaces": [
1134    "./packages/*"
1135  ]
1136}
1137```
1138
1139See [`workspaces`](/using-npm/workspaces) for more examples.
1140
1141### DEFAULT VALUES
1142
1143npm will default some values based on package contents.
1144
1145* `"scripts": {"start": "node server.js"}`
1146
1147  If there is a `server.js` file in the root of your package, then npm will
1148  default the `start` command to `node server.js`.
1149
1150* `"scripts":{"install": "node-gyp rebuild"}`
1151
1152  If there is a `binding.gyp` file in the root of your package and you have
1153  not defined an `install` or `preinstall` script, npm will default the
1154  `install` command to compile using node-gyp.
1155
1156* `"contributors": [...]`
1157
1158  If there is an `AUTHORS` file in the root of your package, npm will treat
1159  each line as a `Name <email> (url)` format, where email and url are
1160  optional.  Lines which start with a `#` or are blank, will be ignored.
1161
1162### SEE ALSO
1163
1164* [semver](https://github.com/npm/node-semver#versions)
1165* [workspaces](/using-npm/workspaces)
1166* [npm init](/commands/npm-init)
1167* [npm version](/commands/npm-version)
1168* [npm config](/commands/npm-config)
1169* [npm help](/commands/npm-help)
1170* [npm install](/commands/npm-install)
1171* [npm publish](/commands/npm-publish)
1172* [npm uninstall](/commands/npm-uninstall)
1173