├── img
├── npm-logo.png
├── npm-config.png
├── npm-no-symbol.png
├── npm-npmrc-file.png
├── bin-shebang-line.png
├── npm-caret-symbol.png
├── npm-start-output.png
├── npm-start-script.png
├── npm-tilde-symbol.png
├── npm-asterisk-symbol.png
├── npm-colors-package.png
├── npm-custom-script.png
├── npm-lodash-version.png
├── npm-edit-package-colors.png
├── npm-lodash-version-all.png
├── npm-prestart-script-test.png
├── npm-semantic versioning.png
├── npm-start-script-custom.png
├── npm-run-all-script-serial.png
├── npm-run-all-package-install.png
├── npm-run-all-package-json-edit.png
├── npm-run-all-project-directory.png
├── npm-script-project-directory.png
├── npm-start-command-hello-sunil.png
├── prestart-js-ourput-red-color.png
├── npm-project-directory-custom-js.png
├── npm-project-folder-npm-prestart.png
├── prestart-js-edit-for-red-color.png
├── npm-package.json-package-lock.json.png
├── npm-run-all-script-serial-output.png
├── npm-run-all-script-a-script-b-script-c.png
└── edit-package-json-file-for-custom-script.png
├── LICENSE
└── README.md
/img/npm-logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-logo.png
--------------------------------------------------------------------------------
/img/npm-config.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-config.png
--------------------------------------------------------------------------------
/img/npm-no-symbol.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-no-symbol.png
--------------------------------------------------------------------------------
/img/npm-npmrc-file.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-npmrc-file.png
--------------------------------------------------------------------------------
/img/bin-shebang-line.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/bin-shebang-line.png
--------------------------------------------------------------------------------
/img/npm-caret-symbol.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-caret-symbol.png
--------------------------------------------------------------------------------
/img/npm-start-output.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-start-output.png
--------------------------------------------------------------------------------
/img/npm-start-script.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-start-script.png
--------------------------------------------------------------------------------
/img/npm-tilde-symbol.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-tilde-symbol.png
--------------------------------------------------------------------------------
/img/npm-asterisk-symbol.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-asterisk-symbol.png
--------------------------------------------------------------------------------
/img/npm-colors-package.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-colors-package.png
--------------------------------------------------------------------------------
/img/npm-custom-script.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-custom-script.png
--------------------------------------------------------------------------------
/img/npm-lodash-version.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-lodash-version.png
--------------------------------------------------------------------------------
/img/npm-edit-package-colors.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-edit-package-colors.png
--------------------------------------------------------------------------------
/img/npm-lodash-version-all.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-lodash-version-all.png
--------------------------------------------------------------------------------
/img/npm-prestart-script-test.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-prestart-script-test.png
--------------------------------------------------------------------------------
/img/npm-semantic versioning.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-semantic versioning.png
--------------------------------------------------------------------------------
/img/npm-start-script-custom.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-start-script-custom.png
--------------------------------------------------------------------------------
/img/npm-run-all-script-serial.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-run-all-script-serial.png
--------------------------------------------------------------------------------
/img/npm-run-all-package-install.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-run-all-package-install.png
--------------------------------------------------------------------------------
/img/npm-run-all-package-json-edit.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-run-all-package-json-edit.png
--------------------------------------------------------------------------------
/img/npm-run-all-project-directory.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-run-all-project-directory.png
--------------------------------------------------------------------------------
/img/npm-script-project-directory.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-script-project-directory.png
--------------------------------------------------------------------------------
/img/npm-start-command-hello-sunil.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-start-command-hello-sunil.png
--------------------------------------------------------------------------------
/img/prestart-js-ourput-red-color.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/prestart-js-ourput-red-color.png
--------------------------------------------------------------------------------
/img/npm-project-directory-custom-js.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-project-directory-custom-js.png
--------------------------------------------------------------------------------
/img/npm-project-folder-npm-prestart.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-project-folder-npm-prestart.png
--------------------------------------------------------------------------------
/img/prestart-js-edit-for-red-color.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/prestart-js-edit-for-red-color.png
--------------------------------------------------------------------------------
/img/npm-package.json-package-lock.json.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-package.json-package-lock.json.png
--------------------------------------------------------------------------------
/img/npm-run-all-script-serial-output.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-run-all-script-serial-output.png
--------------------------------------------------------------------------------
/img/npm-run-all-script-a-script-b-script-c.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/npm-run-all-script-a-script-b-script-c.png
--------------------------------------------------------------------------------
/img/edit-package-json-file-for-custom-script.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sunil-Pradhan/npm-cheatsheet/HEAD/img/edit-package-json-file-for-custom-script.png
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Sunil Pradhan
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | npm Cheat Sheet
6 |
7 |
8 | A list of terminal commands and flags to help use npm.
9 |
10 |
11 | ## Table of Contents
12 |
13 | - [Basic npm commands](#basic-command)
14 | - [Package creation](#Pakg-cr)
15 | - [Package management](#Pakg-magt)
16 | - [List packages](#list-packg)
17 | - [npm versioning](#npm-ver)
18 | - [Updating Packages](#update-packg)
19 | - [NPM Prune](#npm-prune)
20 | - [Dependencies and Development Dependencies](#npm-dep-dev)
21 | - [Exploring package information and versions](#npm-pkg-ver)
22 | - [Configure NPM](#npm-config)
23 | - [NPM Script](#npm-script)
24 | - [Custom NPM scripts](#npm-custom-script)
25 | - [Resources](#npm-resources)
26 |
27 |
28 |
29 | ## Basic npm commands
30 |
31 | Check version number of npm:
32 |
33 | ```
34 | npm -version
35 | ```
36 |
37 | Or use shorthand:
38 |
39 | ```
40 | npm -v
41 | ```
42 |
43 | Get help on npm:
44 |
45 | ```
46 | npm help
47 | ```
48 |
49 | Quick help for specific command:
50 |
51 | ```
52 | npm -h
53 | ```
54 |
55 | Example:
56 |
57 | ```
58 | npm install -h
59 | ```
60 |
61 | Search npm documentation and return the result:
62 |
63 | ```
64 | npm help -search
65 | ```
66 |
67 | Example:
68 |
69 | ```
70 | npm help -search install
71 | ```
72 |
73 | Read documentation of any specific package online (official):
74 |
75 | ```
76 | npm docs
77 | ```
78 |
79 | Example:
80 |
81 | ```
82 | npm docs lodash
83 | ```
84 |
85 | Update npm to latest version:
86 |
87 | ```
88 | npm install npm@latest -g
89 | ```
90 |
91 |
92 |
93 |
94 |
95 | ## Package creation
96 |
97 | Initialize project and create package.json file:
98 |
99 | ```
100 | npm init
101 | ```
102 |
103 | :bulb: **Note:** Follow the wizard dilligently.
104 |
105 | Initialize project and create package.json file with default values:
106 |
107 | ```
108 | npm init --yes
109 | ```
110 |
111 | Or use shorthand:
112 |
113 | ```
114 | npm init --y
115 | ```
116 |
117 | Set default author name for package.json:
118 |
119 | ```
120 | npm config set init -author-name ‘your-name’
121 | ```
122 |
123 | Set default license for package.json:
124 |
125 | ```
126 | npm set init -license ‘MIT’
127 | ```
128 |
129 | Show default author name:
130 |
131 | ```
132 | npm config get init -author-name
133 | ```
134 |
135 | Show default license:
136 |
137 | ```
138 | npm get init -license
139 | ```
140 |
141 | Delete default author name:
142 |
143 | ```
144 | npm config delete init -author-name
145 | ```
146 |
147 | Delete default license:
148 |
149 | ```
150 | npm config delete init -license
151 | ```
152 |
153 | **package.json file:**
154 |
155 | **Package name -** If you plan to publish your package, the most important things in your package.json are the name and version fields as they will be required. The name and version together form an identifier that is assumed to be completely unique.
156 |
157 | Changes to the package should come along with changes to the version. If you do not plan to publish your package, the name and version fields are optional.
158 |
159 | **Rules:**
160 |
161 | - The name must be less than or equal to 214 characters. This includes the scope for scoped packages.
162 | - The name can not start with a dot or an underscore.
163 | - New packages must not have uppercase letters in the name
164 | - The name ends up being part of a URL, an argument on the command line, and a folder name. Therefore, the name can’t contain any non-URL-safe characters.
165 |
166 | **Version -** Version number of your package.
167 |
168 | **Description -** It’s a string. This helps people discover your package, as it’s listed in npm search.
169 |
170 | **Entry point -** We write a file which includes the main logic of the package/module.
171 |
172 | **Test command -** Write test tool name.
173 |
174 | **Git repository -** Address of git repo.
175 |
176 | **Keywords -** It’s an array of strings. This helps people discover your package as it’s listed in npm search.
177 |
178 | **Author -** Name of Author.
179 |
180 | **License -** You should specify a license for your package so that people know how they are permitted to use it, and any restrictions you are placing on it.
181 |
182 |
183 |
184 |
185 |
186 | ## Package management
187 |
188 | Install everything in package.json:
189 |
190 | ```
191 | npm install
192 | ```
193 |
194 | Or use shorthand:
195 |
196 | ```
197 | npm i
198 | ```
199 |
200 | Install a package:
201 |
202 | ```
203 | npm install
204 | ```
205 |
206 | Or use shorthand:
207 |
208 | ```
209 | npm i
210 | ```
211 |
212 | Example:
213 |
214 | ```
215 | npm install lodash
216 | ```
217 |
218 | Install a package as devDependency:
219 |
220 | ```
221 | npm install --save-dev
222 | ```
223 |
224 | Or use shorthand:
225 |
226 | ```
227 | npm install -D
228 | ```
229 |
230 | Example:
231 |
232 | ```
233 | npm install lodash --save-dev
234 | ```
235 |
236 | Uninstalling local packages:
237 |
238 | ```
239 | npm uninstall --save
240 | ```
241 |
242 | Or
243 |
244 | ```
245 | npm un --save
246 | ```
247 |
248 | Or
249 |
250 | ```
251 | npm remove --save
252 | ```
253 |
254 | Or
255 |
256 | ```
257 | npm rm --save
258 | ```
259 |
260 | Example:
261 |
262 | ```
263 | npm rm lodash --save
264 | ```
265 |
266 | Install global packages:
267 |
268 | ```
269 | npm install g
270 | ```
271 |
272 | Uninstall global packages:
273 |
274 | ```
275 | npm uninstall g
276 | ```
277 |
278 | Or
279 |
280 | ```
281 | npm un g
282 | ```
283 |
284 | Or
285 |
286 | ```
287 | npm remove g
288 | ```
289 |
290 | Or
291 |
292 | ```
293 | npm rm g
294 | ```
295 |
296 | Example:
297 |
298 | ```
299 | npm rm lodash g
300 | ```
301 |
302 | Install a package based on its tag:
303 |
304 | ```
305 | npm install @ --save-dev
306 | ```
307 |
308 | Example:
309 |
310 | ```
311 | npm install lodash@latest --save-dev
312 | ```
313 |
314 | Install a package to specific version:
315 |
316 | ```
317 | npm install @ --save-dev
318 | ```
319 |
320 | Example:
321 |
322 | ```
323 | npm install lodash@2.1.1 --save-dev
324 | ```
325 |
326 | Install a package to exact specific version:
327 |
328 | ```
329 | npm install @ --save-exact
330 | ```
331 |
332 | Example:
333 |
334 | ```
335 | npm install lodash@2.4.1 --save-exact
336 | ```
337 |
338 | **Flags:**
339 |
340 | **-P :** Production (--save-prod)
341 |
342 | **-D :** devDependencies (--save-dev)
343 |
344 | **-O :** Optional (--save-optional)
345 |
346 | **-E :** Save exact
347 |
348 | **-g :** Global
349 |
350 | **-S :** Save
351 |
352 | **--no-save:** Prevents saving to dependencies
353 |
354 |
355 |
356 |
357 |
358 | ## List packages
359 |
360 | List locally installed packages (includes all dependencies):
361 |
362 | ```
363 | npm list
364 | ```
365 |
366 | Or use shorthand:
367 |
368 | ```
369 | npm ls
370 | ```
371 |
372 | Restrict the depth of the tree by 1:
373 |
374 | ```
375 | npm list --depth 1
376 | ```
377 |
378 | :bulb: **NOTE:** _It gives you only immediate dependencies of the packages._
379 |
380 |
381 | Restrict the depth of the tree by 0:
382 |
383 | ```
384 | npm list --depth 0
385 | ```
386 |
387 | :bulb: **NOTE:** _It gives you the list of all the packages and no dependencies for that particular package._
388 |
389 |
390 | List globally installed packages (includes all dependencies):
391 |
392 | ```
393 | npm list --global
394 | ```
395 |
396 | Or
397 |
398 | ```
399 | npm list -g
400 | ```
401 |
402 | Restrict the depth of the tree by 0 (global packages):
403 |
404 | ```
405 | npm list -g --depth 0
406 | ```
407 |
408 | Or
409 |
410 | ```
411 | npm list --global true --depth 0
412 | ```
413 |
414 | Lists the dependencies which are outdated:
415 |
416 | ```
417 | npm outdated
418 | ```
419 |
420 |
421 |
422 |
423 |
424 | ## NPM Versioning
425 |
426 | There is a special agreement to set versions of the packages and it's called **semantic versioning**. It is a specification where a version is represented by three numbers that mean the same thing for every developer.
427 |
428 | 
429 |
430 | - **Patch:** It is used for bug fixes
431 | - **Minor:** New features but compatible with previous version
432 | - **Major:** Introduce non-compatible features or change your project completely
433 |
434 | **Versioning Table**
435 |
436 | | Parameter | Release Version |
437 | | ------------------- | --------------- |
438 | | Exact version | 5.21.17 |
439 | | Greater than | >5.21.1 |
440 | | Compatible changes | ^5.21.8 |
441 | | Minor-level changes | ~5.21.8 |
442 |
443 | **Version specification in package.json file (helpful when using `npm install` command)**
444 |
445 | **Caret (^) symbol:** Stick to the given major version number but when run `npm install` retrieve the latest minor and patch version.
446 |
447 | Example:
448 |
449 | 
450 |
451 | **Tilde (~) symbol:** It means, leave the major version and minor version as it is but retrieve the latest version of patch number when run `npm install` command.
452 |
453 | Example:
454 |
455 | 
456 |
457 | **Asterisk (\*) symbol:** Let's say even if there is a change in the major version number of a package but you want the latest one. In this case you will use an asterisk symbol.
458 |
459 | So, when you specify an asterisk symbol it is going to retrieve the latest version of the package irrespective of the major minor or patch version.
460 |
461 | **Example:**
462 |
463 | 
464 |
465 | **No symbol:** You can just have the specific version number in this case npm doesn't install a newer version even though it exists when you run `npm install`.
466 |
467 | Example:
468 |
469 | 
470 |
471 | Install specific version of a package:
472 |
473 | ```
474 | npm install
475 | ```
476 |
477 | Example:
478 |
479 | ```
480 | npm install lodash@3.3.0 --save
481 | ```
482 |
483 | Restrict major and minor version but use patch version to latest in a package:
484 |
485 | ```
486 | npm install
487 | ```
488 |
489 | Example:
490 |
491 | ```
492 | npm install lodash@4.14 --save
493 | ```
494 |
495 | Install package only with latest major version number:
496 |
497 | ```
498 | npm install
499 | ```
500 |
501 | Example:
502 |
503 | ```
504 | npm install lodash@4 --save
505 | ```
506 |
507 | **What is the difference between package.json and package-lock.json?**
508 |
509 | 
510 |
511 | When you install any package in your project by executing the command
512 |
513 | ```
514 | npm install --save
515 | ```
516 |
517 | Then it will install the exact latest version of that package in your project and save the dependency in package.json with a carat (^) sign.
518 |
519 | Like, if the current version of a package is 5.2.3 then the installed version will be 5.2.3 and the saved dependency will be ^5.2.3.
520 |
521 | Carat (^) means it will support any higher version with major version 5 like 5.3.1 and so on. **Here, package-lock.json is created for locking the dependency with the installed version.**
522 |
523 | **Purpose of package-lock.json:**
524 |
525 | To avoid differences in installed dependencies on different environments and to generate the same results on every environment you should use the package-lock.json file to install dependencies.
526 |
527 | Ideally, this file should be on your source control with the package.json file so when you or any other user will clone the project and run the command `npm i`, it will install the exact same version saved in package-lock.json file and you will able to generate the same results as you developed with that particular package.
528 |
529 | **Why should you commit package-lock.json with your project source code?**
530 |
531 | During deployment, when you again run `npm i` with the same package.json file without the package-lock.json, the installed package might have a higher version now from what you had intended.
532 |
533 | Now, what if you wanted to have that particular version for your dependency during deployment which you used at the time of development. This is the need of creating a package-lock.json file and keeping it with the source code. This file is created with the details of the specific version installed in your project.
534 |
535 |
536 |
537 |
538 |
539 | ## Updating Packages
540 |
541 | Updates all listed packages to the latest release version:
542 |
543 | ```
544 | npm update
545 | ```
546 |
547 | Update specific package:
548 |
549 | ```
550 | npm update --save
551 | ```
552 |
553 | Example:
554 |
555 | ```
556 | npm update lodash --save
557 | ```
558 |
559 | Update only dev dependencies:
560 |
561 | ```
562 | npm update --dev --save-dev
563 | ```
564 |
565 | Update all the packages globally:
566 |
567 | ```
568 | npm update -g
569 | ```
570 |
571 | Update specific package globally:
572 |
573 | ```
574 | npm update -g
575 | ```
576 |
577 | Example:
578 |
579 | ```
580 | npm update -g lodash
581 | ```
582 |
583 |
584 |
585 |
586 |
587 | ## NPM Prune
588 |
589 | Remove all unused packages(It remove modules not listed in package.json):
590 |
591 | ```
592 | npm prune
593 | ```
594 |
595 | More info: [@stackoverflow](https://stackoverflow.com/questions/21417014/npm-command-to-uninstall-or-prune-unused-packages-in-node-js)
596 |
597 |
598 |
599 |
600 |
601 | ## Dependencies and Development Dependencies
602 |
603 | **Dependencies:**
604 |
605 | - Installed using `npm install` or `npm install `
606 | - If package A is installed using `npm install ` and it has dependency on package B then package B will be also installed along with it’s dependencies.
607 |
608 | **Development Dependencies:**
609 |
610 | - Installed using `npm install`
611 | - Not installed using `npm install --production`
612 | - If package A is installed using `npm install ` and it has devDependency on package B than package B will NOT be installed.
613 |
614 | Install a package as devDependency:
615 |
616 | ```
617 | npm install --save-dev
618 | ```
619 |
620 | Or use shorthand:
621 |
622 | ```
623 | npm install -D
624 | ```
625 |
626 | Example:
627 |
628 | ```
629 | npm install lodash --save-dev
630 | ```
631 |
632 | package.json file contains dependencies and development dependencies. If you execute the `npm install` command then all those dependencies will be installed in case node_modules folder is missing.
633 |
634 | On the other hand, if you install with the `production` flag then it will only install dependencies.
635 |
636 | ```
637 | npm install --production
638 | ```
639 |
640 | But if the package has its own development dependencies they will be never installed in your project.
641 |
642 | :bulb: **TIP:** **Do you add packages as dependency or devDependancy?**
643 |
644 | The answer is it depends: It depends on the kind of application you are developing.
645 |
646 | If you build just a standalone application for the browser you should add all packages as development dependencies. Because browser does not understand dependencies or devDependencies.
647 |
648 | But if you build a public package and other packages depend on your package and if you will use some functions from external packages in your compiled code then you will need to add those external packages as dependencies.
649 |
650 | All other packages need to go into development dependencies in your project.
651 |
652 |
653 |
654 |
655 |
656 | ## Exploring package information and versions
657 |
658 | Print information about package:
659 |
660 | ```
661 | npm view
662 | ```
663 |
664 | Example:
665 |
666 | ```
667 | npm view lodash
668 | ```
669 |
670 | 
671 |
672 | Print information about package (all version):
673 |
674 | ```
675 | npm view versions
676 | ```
677 |
678 | Example:
679 |
680 | ```
681 | npm view lodash versions
682 | ```
683 |
684 | 
685 |
686 |
687 |
688 |
689 |
690 | ## Configure NPM
691 |
692 | To configure npm:
693 |
694 | ```
695 | npm config
696 | ```
697 |
698 | 
699 |
700 | _npm gets its config settings from the command line, environment variables, npmrc files, and in some cases, the package.json file._
701 |
702 | Display basic configuration list:
703 |
704 | ```
705 | npm config list
706 | ```
707 |
708 | Display complete configuration list:
709 |
710 | ```
711 | npm config list -l
712 | ```
713 |
714 | Sets the config key to the value:
715 |
716 | ```
717 | npm config set
718 | ```
719 |
720 | _If value is omitted, then it sets it to **true**_
721 |
722 | **Example:**
723 |
724 | Set default author name for all new package creation:
725 |
726 | ```
727 | npm config set init.author.name ‘your-name’
728 | ```
729 |
730 | Verify by: `npm init -y`
731 |
732 | Get default configuration value:
733 |
734 | ```
735 | npm config get
736 | ```
737 |
738 | **Example:**
739 |
740 | Get value of default author name:
741 |
742 | ```
743 | npm config get init.author.name
744 | ```
745 |
746 | Edit configuration file:
747 |
748 | ```
749 | npm config edit
750 | ```
751 |
752 | It will opens up configuration file;
753 |
754 | 
755 |
756 | If you see carefully all new edit which you have made it reflects on top of the list.If you want to go with npm inbuilt default values then remove it and save the file.
757 |
758 | Edit global configuration file with global values:
759 |
760 | ```
761 | npm config edit --global
762 | ```
763 |
764 | Delete configuration value:
765 |
766 | ```
767 | npm config delete
768 | ```
769 |
770 | Example:
771 |
772 | ```
773 | npm config delete init.author.name
774 | ```
775 |
776 |
777 |
778 |
779 |
780 | ## NPM Script
781 |
782 | NPM scripts allow you easily perform certain operations to execute commands or execute files using npm command.
783 |
784 | Popular npm script command:
785 |
786 | - `npm start`
787 | - `npm run`
788 | - `npm built`
789 |
790 | **npm start:**
791 |
792 | **npm start -** This runs an arbitrary command specified in the package’s **“Start”** property of its **“scripts”** object. If no **“start”** property is specified on the **“script”** object, it will run node server.js.
793 |
794 | Project directory:
795 |
796 | 
797 |
798 | Basically what you would do is to execute node and run index.js
799 |
800 | 
801 |
802 | So let's write a script that is going to do this for us. Remove the following line from package.json file:
803 |
804 | 
805 |
806 | Now add the following line:
807 |
808 | 
809 |
810 | Now run `npm start` command:
811 |
812 | 
813 |
814 | This comes in handy when you are developing a project. Let's say the project entry point is index.js.
815 |
816 | So by having a start command the user doesn't have to worry about what command to run. So someone downloads your project from github and they run - `npm start`.
817 |
818 | Your project will automatically execute as you have intended.
819 |
820 | Other popular npm inbuilt script command are:
821 |
822 | - `npm stop`
823 | - `npm restart`
824 | - `npm test`
825 | - `npm prestart`
826 | - `npm poststart`
827 |
828 | **npm prestart:**
829 |
830 | Project directory:
831 |
832 | 
833 |
834 | Install colors package as devDependency:
835 |
836 | ```
837 | npm install colors --save-dev
838 | ```
839 |
840 | 
841 |
842 | Edit package.json file for prestart:
843 |
844 | 
845 |
846 | Now test prestart script by typing: `npm start`:
847 |
848 | 
849 |
850 | Edit prestart.js file to see output in red color:
851 |
852 | 
853 |
854 | Now see the final output by typing `npm start`:
855 |
856 | 
857 |
858 | **NPM .bin folder with executable scripts:**
859 |
860 | During npm package installation bin scripts are copied to the .bin folder.
861 |
862 | 
863 |
864 | :bulb: **Note:** #!/usr/bin/env - Shebang line
865 |
866 | It determines which interpreter should be used for file execution on the Unix-like operating systems. (first line in the executable plain text files)
867 |
868 |
869 |
870 |
871 |
872 | ## Custom NPM scripts
873 |
874 | Executes certain script:
875 |
876 | ```
877 | npm run