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