Package.json practical side clarified (2 ed.)
Published: 2021-12-08 19:04:59

### <u>Intro</u>

As for beginning of this article let me cite the most important [[docs.npmjs.com](https://docs.npmjs.com/creating-a-package-json-file)] :

>  **A package.json file must contain "name" and "version" fields :** <br>
- The "name" field contains your package's name, and must be lowercase and one word, and may contain hyphens and underscores ;  <br>
- The "version" field must be in the form x.x.x and follow the (semantic versioning). <br>
[semantic versioning](https://docs.npmjs.com/about-semantic-versioning)

---

### <u>Deps saving demystified</u>

Consider the following information within you **package.json** :

```json
{
"name": "abc",
"main": "lib/some-entry-file.js",
...
}
```

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]](https://stackoverflow.com/questions/22512992/how-to-use-the-main-parameter-in-package-json)

---

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

```json
{
"name": "abc",
"main": "./some-long-path/src/server/some-entry-file.js",
...
}
```

Using with **node**mon 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 :

```json
{
"name": "abc",
"main": "./some-long-path/src/server/some-entry-file.js",
"scripts": {
    "start" : "node ."
  },
...
}
```

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]](https://stackoverflow.com/questions/22512992/how-to-use-the-main-parameter-in-package-json)

---

### <u>Deps saving demystified</u>

> Deps stands for dependencies, where as..: <br>
- Dev – development <br>
- Prod – production <br>
 
| 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](https://docs.npmjs.com/cli/v8/commands/npm-install)]

All credits to [voithos [stack overflow]](https://stackoverflow.com/questions/19578796/what-is-the-save-option-for-npm-install)

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

`npm --help` <br>
`npm command_name -h` <br>

---

### <u>Useful references</u>

- Toptal guide [link](https://www.toptal.com/javascript/a-guide-to-npm-the-node-package-manager)
- Nodesource guide [link](https://nodesource.com/blog/an-absolute-beginners-guide-to-using-npm)
- Scripts guide [link on youtube](https://youtu.be/-SaZiADGLHs?t=388)