12/08/2018, 15:33

Understanding the contents of "package.json"

When the npm module was published in March 2017, the corresponding package.json needed to be edited, so I did a research to have a fully understanding about all the properties of the package.json/ There could be some lacking of properties, but i will add them in later on/ Name of the Module, ...

When the npm module was published in March 2017, the corresponding package.json needed to be edited, so I did a research to have a fully understanding about all the properties of the package.json/

There could be some lacking of properties, but i will add them in later on/

Name of the Module, this field is required It is used in module import by import and require () in source. The npm module is assumed to be unique in name and version, so do not duplicate names with other libraries.

{ "name": "react-native-card-media" }

Version of the Module, this field is required The npm module is assumed to be unique in name and version. When upgrading npm, do not forget to update the version number.

{ "version": "0.0.5" }

If this property is set to true, you can not publish the module. Be sure not to publish projects that will not be published by mistake.

{ "private" : true }

Module's explanation Since it is displayed in npm search, it helps people find and understand your package.

{ "description": "Card Media component for React Native. Also supports multiple image layout." }

Specify the script file that is called first in the module. For example, we name the module foo, install it by the user, and when we execute require ("foo") which is the exports object of the module specified by main is returned. You must specify a relative path from the package root.

{ "main": "index.js" }

You can define an alias command to execute arbitrary shell script.

{
  "scripts": {
    "test": "eslint *.js ./components/*.js ./example/*/*.js",
    "start":      "node app.js",
    "production": "NODE_ENV=production node app.js"
  }
}

The key is a name that can be used as an alias like npm test, npm start, and shell script is specified as one line for the value. However, when registering non-reserved words such as test and start as alias commands, you must executenpm run production. When describing a one-line shell script, the bin of the module in dependencies and devDependencies automatically enters PATH. And you do like this:

{
  "devDependencies": {
    "eslint": "^3.18.0"
  }
}

Not like this:

{
  "scripts": {
    "test": "node_modules/.bin/eslint *.js"
  }
}

but like this

{
  "scripts": {
    "test": "eslint *.js"
  }
}

Scripts seems to have many more functions, so I'd like to write on it in the following posts

Specify where the source code is managed.

{
  "repository": {
    "type": "git",
    "url": "git+ssh://git@github.com/dondoko-susumu/react-native-card-media.git"
  }
}

author specifies only one person, contributors specify array of people.

It seems common to write with such abbreviations.

{ "author": "Ken Kubota <kkbt2003@gmail> (https://github.com/dondoko-susumu/)" }

License information

{"license" : "MIT"}

URL and things like that which referring to the project problems and bug trackings are reported by email address If it is GitHub so it seems to be similar to the POST issue URL

{
  "bugs": {
    "url": "https://github.com/dondoko-susumu/react-native-card-media/issues",
    "email" : "project@hostname.com"
  }
}

Home page URL of the project

{
  "homepage": "https://github.com/dondoko-susumu/react-native-card-media#readme"
}

Describe dependent modules and versions. When execute npm installing in the directory where package.json is installed, the modules described in dependencies and (devDependencies) are installed in the node_module directory.

If any modules depend on installed modules, they are also installed. Modules that depend on dependencies are installed like strings.

Do not write something that automatically runs a test script or a trans-piler (a module for building a release module) in dependencies. Such items are described in devDependencies described later.

$ npm install --save co

With the --save option, it is registered in package.json without permission as follows.

{
  "dependencies": {
    "co": "^4.6.0"
  }
}

^ (Caret Ranges)

Version specification specified automatically by the --save option (it seems to call ^ (caret))

^1.2.3 := >=1.2.3 <2.0.0
^0.2.3 := >=0.2.3 <0.3.0
^0.0.3 := >=0.0.3 <0.0.4

Define what you need when developing a module to be published to npm.

The one described in devDependencies is not installed if you are executing npm install as an external module of another project yourself.

On the other hand, if you clone the project with Git and install it at npm install it will be installed. This library is necessary only for development, it is useless at run time, so it does not need to be included.

Test scripts, compilers, task runners, etc. Define what you do not need to load as modules.

By the way, when you execute npm install as an end user, both dependencies and devDependencies are installed.

You can write the setting of jest with the top level jest key of package.json.

For preset, specify the npm module exporting jest-preset.json at the top level.

{
  "jest": {
    "preset": "react-native"
  }
}

references URL:

jest configuration npm publish semver publish

0