Javascript Package - Package.json


Package.json is used by npm to store metadata for projects published as package.

The package.json 1) files:

  • serves as documentation for what packages your project depends on.
  • allows you to specify the versions of a package that your project can use using semantic versioning rules.
  • makes the build reproducible



  • “name”: all lowercase one word, no spaces dashes and underscores allowed
  • “version” in the form of x.x.x follows semver spec


  • main.js: the entry point when the package is used.
  • description. The description helps people find your package on npm search. If there is no description field in the package.json, npm uses the first line of the or README instead.


  "name": "my_package",
  "description": "",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "repository": {
    "type": "git",
    "url": ""
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": ""
  "homepage": ""
  "dependencies": {
    "my_dep": "^1.0.0"
  "devDependencies" : {
    "my_test_framework": "^3.1.0"



The type property 2) defines the type of module that the package contains

  • module: This package contains ECMAScript modules and may have an import or export statement.
  • commonjs: This package contains CommonJS modules and may have a require() statement.
  • jsnext:main: This package contains ECMAScript modules and is intended to be used with bundlers like Webpack 2+ and Rollup that support the jsnext:main entry point.
  • typings: This package contains TypeScript definition files (*.d.ts) and may have an import or export statement.


The types property is used in the resolution of the location of declaration files.


The “typings” field is synonymous with types 3)


main defines the main script


modules defines where to find the ECMAScript-style module 4)

Note that Node uses the type property with the value module.

"module": "src/index.js",  

if compiled via typescript

"module": "./dist/index.js",


browser defines the location of modules that are only available inside the browsers.

"browser": {
    "module-a": false,
    "./server/only.js": "./shims/server-only.js"


The import order of precedence is defined in resolve-mainfields for webpack

module.exports = {
  resolve: {
    mainFields: ['browser', 'module', 'main']


The script section of the package.json allows to start command. See What are npm scripts and how to use them ? The script property of Package.json






yarn init
# optionaly
npm init

Modify Config

npm set "[email protected]"
npm set "ag_dubs"
npm set init.license "MIT"
Your Name <[email protected]> (

Add dependency

To add dependency, use the save and save dev flag of npm install. See Javascript Package.

  • For dependencies
npm install <package_name> --save
  • For dev-dependencies (development and testing)
npm install <package_name> --save-dev

Pass the version to your library

How to pass the version to your library

myLib.VERSION = require('../package.json').version;

Documentation / Reference

Powered by ComboStrap