Learn TypeScript 3 by Building Web Applications
上QQ阅读APP看书,第一时间看更新

package.json

When you work on a project, usually, you simply don't want to create a long document explaining how to download/install each and every dependency. Instead, you want to have a simple way to install everything at once.

As explained in the official documentation (https://docs.npmjs.com/getting-started/using-a-package.json), a package.json file does the following:

  • Lists the packages that your project depends on
  • Allows you to specify the versions of all dependencies required by the project using semantic versioning rules (that is, major.minor.patch)
  • Makes your build reproducible, and therefore much easier to share with other developers
If you want to know more about semantic versioning (also known as semver), check out the following website: https://semver.org.

We'll add to that list; the file also allows you to do the following:

  • Define scripts. (We will use that feature soon to improve our application's build, that is, the set of scripts that will construct our application, by the way.)
  • Document metadata about your project in a central location (for example, name, version, description, author, contributors, bug tracker, repository, and much more​).
  • Store the configuration for specific tools.

As you can see, with the package.json file, you can do much more than just manage your dependencies.

Regarding dependencies, those are mainly defined in two different sections, depending on the needs:

  • dependencies: For all the things that your program requires at runtime.
  • devDependencies: For all the rest; for example, transpilers, testing tools, and so on​.
There's also an optionalDependencies section that can sometimes prove useful:  http://npm.github.io/using-pkgs-docs/package-json/types/optionaldependencies.html.

You can find everything there is to know about the package.json file here: https://docs.npmjs.com/files/package.json.