Introduction of npm - HeroLiu183/MyWiki GitHub Wiki

What is npm?

  • npm can be considered as a package service, which provides programmers(in JavaScript) to share codes to others or use the existing codes from others.

  • npm is consisted by following three components

     1. the website: where you can search packages and register npm account.
     2. the command interface (CLI): use it to operate packages for projects and download or push packages.
     3. the registry: it stores all the packages for users to download.
    

Install npm

  1. Install Node.js: npm is written in Node.js, before getting start npm, you need to install Node.js first. you can download Node.js from it's official website, please select LTS version which is tested by npm. You can test your installation by following commands in CLI(ex:CMD, powerShell), that will return the numbers of current Node.js and npm versions.

     node -v
     npm -v
    
  2. To update npm with following command

     npm install npm@latest -g
    

Install npm package locally

There's two ways to install npm packages for your porject

  1. Install package directly for you project: run npm install ${package_name} command in your project directory. after install npm will create a new folder /node_moules, and it will contain loadash folder shows as following

     cd C:/Lab/my_project
     npm install loadash
    

  2. Manage packages with package.json file:

    • you need create package.json and add project name and version in two ways

      1. Just create a file then named package.json init it wiht project name and version.
      2. Run a CLI command, then it will help you create package.json file.

      npm init

    • then run npm install ${package_name} --save, will crate package.json file, which record the package dependency of project.

    • then it will update package.json file.

    • the benefit use pagckage.json manage your package dependency is that, others can reproduce your project on their own environments by npm install.

more detail(npm website)

Reference

npm official site