NPM for beginners
Adolfo Moreno 2020 Made in Microsoft PowerPoint

NPM for beginners

No alt text provided for this image

I made this cheat sheet so that developers print out or save and can reach with ease.

What is npm?

NPM stands for Node Package Manager, same as PIP for Python.

It comes installed with Node.js and you use it to add node packages to your project such as React, Nodemon, etc.

It helps keep track of what you use in your projects and what versions.

How does it work?

To start a project you use the command "npm init" and you get a few questions:

  • package name (name you want to give to you project package)
  • version (ex 1.0.0)
  • description
  • entry point (the file that will be loaded when you run "npm start")
  • test command (the command to run your tests, don't worry about it for now)
  • git repository (the git repository of your project, if you have one)
  • keywords
  • license

After you answer all this, the package.json file is created. This file the focus of this tutorial, if you understand it you get npm.

Package.json

{
2  "name": "package.json-work",
3  "version": "1.0.0",
4  "description": "package.json file",
6  "main": "index.js",
7  "scripts": {
8    "start": "node index",
9    "dev": "nodemon index",
10    "test": "jest"
11  },
12  "repository": {
13    "type": "git",
14    "url": "git+https://github.com/Easybuoy/package.json-mastery.git"
15  },
16  "keywords": [
17    "node",
18    "javascript",
19    "npm",
20    "yarn"
21  ],
22  "author": "Adolfo Moreno",
23  "license": "MIT",
24  
32  "dependencies": {
33    "bcryptjs": "^2.4.3",
34    "cors": "^2.8.5",
35    "dotenv": "^6.1.0",
36    "express": "^4.16.4"
37  },
38  "devDependencies": {
39    "eslint": "^4.19.1",
40    "mocha": "^6.2.0",
41    "nodemon": "^1.19.1"
42  } }
50}

This is an example of a package.json file. If we were to download a git repository and go to the folder to run "npm install", we would be installing all the dependencies and devDependencies.

Dependencies are the libraries of the production environment aka "npm start" as you see in the scripts. "npm start" would run "node index" which is the same as running "node index.js".

devDependencies are the libraries of the development environment aka "npm dev" which would run "nodemon index".

This makes it very easy to download projects from the internet and install all the required libraries to run them.

You can install libraries as global and they are available for every project but it's a better practice to use dependecies instead.

All the necessary commands are in the cheat sheet.

Versioning

No alt text provided for this image
  • Major (going from 4 -> 5 affects API)
  • Minor (going from 2 -> 3 does not break usage but changes API)
  • Patch (bug fix)



要查看或添加评论,请登录

社区洞察

其他会员也浏览了