DEV Community

LG
LG

Posted on • Edited on

Package.json practical side clarified (2 ed.)

Intro

As for beginning of this article let me cite the most important [docs.npmjs.com] :

A package.json file must contain "name" and "version" fields :

  • The "name" field contains your package's name, and must be lowercase and one word, and may contain hyphens and underscores ;
  • The "version" field must be in the form x.x.x and follow the (semantic versioning). semantic versioning

Deps saving demystified

Consider the following information within you package.json :

{
"name": "abc",
"main": "lib/some-entry-file.js",
...
}
Enter fullscreen mode Exit fullscreen mode

The require('abc') actually will do require lib/some-entry-file.js . Consider this requirement as some kind of alias for the entry point .

All credits to prosti [stack overflow]


Property of "main": "some-entry-point" is also useful when you have long path to the entry file , for an example :

{
"name": "abc",
"main": "./some-long-path/src/server/some-entry-file.js",
...
}
Enter fullscreen mode Exit fullscreen mode

Using with nodemon you would only need to use (dot) that denotes pwd i.e. root of your working directory , consider the following to conclude what I've just said above :

{
"name": "abc",
"main": "./some-long-path/src/server/some-entry-file.js",
"scripts": {
    "start" : "node ."
  },
...
}
Enter fullscreen mode Exit fullscreen mode

Now all you have to do is just run the following command of npm start

Either it would be node or nodemon or any other relevant and it's followed by dot (.) which denotes any path as a value assigned to "main" in package.json will always find so called entry-point to run things right without any error of ENOENT or similar thrown .

All credits to Aakash [stack overflow]


Deps saving demystified

Deps stands for dependencies, where as..:

  • Dev – development
  • Prod – production
Full command (a.k.a. --option) Shorthand (a.k.a. -flag) Action
--save-prod -P save deps for prod
--save [^default] -S shorter version of -P
--save-dev -D save deps for dev , not prod

^NOTE : Since NPM v5.0.0 and higher --save (-S) as alias of --save-prod (-P) are by default, need explicitly to define whilst installing packages : it means npm installs packages under Dependencies (for Prod) ;
Also worth to mention if not version specified , default installation is within @latest tag e.g. npm i express would be installed as if under npm i express@latest [more about]

All credits to voithos [stack overflow]

Useful way to get reference out-of-the-box is to run one (or both) of the following commands :

npm --help

npm command_name -h


Useful references

Top comments (0)