Skip to content
Search

Package.json

lastmod 2023-12-04created 2020-09-2311 edits

Categories

Example

Create a package.json file in the project ROOT directory.

{
    "name": "gitlab-webhook-server",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
        "gitlabhook": ">= 0.0.17"
    },
    "scripts": {
        "start": "node app.js"
    }
}

To install the packages, run the npm install command in that directory.

Properties

private
If set to true, npm will not publish the package.
scripts
A dictionary containing script commands that are run at various times in the package's life cycle.
version
Version information.
  • version: Exactly matching version
  • =version: Exactly matching version
  • >version: Greater version
  • >=version: Greater than or equal version
  • <version: Lesser version
  • <=version: Lesser than or equal version
  • ~version: A version range. ~0.2 means from 0.2 up to but not including 0.3
  • 1.2.x: x notation is also allowed
  • ^version: Updates within the range that guarantees backward compatibility, following the SemVer (Semantic Versioning) convention.
dependencies
List of dependencies, installed on npm install and npm install --production.
devDependencies
List of development dependencies, installed on npm install and npm install --dev.
exports
sub path module support

Specifying a local path in dependencies

"dependencies": {
    "express": "*",
    "somelocallib": "file:./somelocallib"
}

How to apply environment variables in scripts

To apply NODE_ENV=develop, do the following.

{
  "name": "answer",
  "scripts": {
    "serve": "NODE_ENV=develop vue-cli-service serve",
  }
}

How to use package.json properties in JavaScript

Mainly used to pull out things like the version number:

import {version} from './package.json';

However, a better approach than the one above is to use the environment variables with the npm_package_ prefix, such as process.env.npm_package_version. See the relevant documentation for details.

See also

Favorite site

author Zer0 sitemap