Get Started with npm

common npm commands to know

Get Started with npm

Introduction

I have written the blog post to retain my knowledge after learning about npm. And also for my future reference.

npm is the standard package manager with node run-time. When working on a large project, one cannot do all the low-level things, and cannot implement everything from scratch, so we depend on other people's code. Here is where a Package Manager comes in, it provides a way to use already well written packages in ones project.

Node Package Manager (npm) is one such registry where all the all packages are listed, and can be imported to any project. Npm contains a minifed code of the package, the complete package can be seen on github, as they are all open source. npm is technically a cli tool to install, remove and manage the packages.

npm vs yarn : yarn is a similar package manager, which started out as alternative to npm and provided a better speed in its early. But at this point of time, both are pretty much the same. But still there are some differences.

commands

  1. Firstly to get help for the command : npm help <command> , this provides long documentation.
    • there a shorter version npm <command> -h , which opens inline help.
    • to search a term in help documentation npm help-search <search-term>.
    • eg: npm install -h opens short help for install command.
  2. To start a package.json file : npm init , this asks a series for questions to the create the file
    • we can skip the questions and get default values ( which can later be edited ) but using npm init --yes or shortly npm init -y.
    • we can set some default values by the command: npm config set init-author-name "youName"
    • or a shorter command npm set init-license "MIT", to set default license as MIT.
    • npm config delete init-license will delete and set back the default values.
  3. We can install a local package with : `npm install
    • npm install date-fns to install a popular Dates package.
    • npm i lodash to install a useful utility package.
  4. To list all the installed packages npm list
  5. To uninstall a package npm uninstall date-fns
  6. We can also install the packages globally by npm install dotenv -g
    • to remove global package add the -g / --global flag
    • to find the path in which global packages are installed npm root -g
  7. When we install a package from npm install a latest version is installed
    •   "dependencies": {
      "date-fns": "^2.29.1"
      }
      
      Here 2 denotes a Current Major version, 29 denotes a minor version and 1 denoted the patch /bug fix version.
    • this way of versioning the release is called Semantic Versioning .
  8. npm install date-fns@3.3.0 will install as specific version

    • npm install date-fns@4.4 will install the 4.4 version but with the latest patch.
    • npm install axios --save-dev will install the package only for development and testing.
  9. Inside the version inside the package.json file, installation in other system is based upon the symbol specified.

    • Caret sign ^ : ^6.3.1 sticks to the major version 6 ,but looks for latest minor and patch versions.
    • Tidle sign ~: ~6.3.1 sticks to same major and minor version i.e., 6.3 but gets the latest patch.
    • Star sign * : * is going to fetch the latest major,minor and patch version
    • No sign : 6.3.1 gets the exact version.
  10. We can update a package by : npm update <package-name>

    • npm update will update all the local packages, add -g for global
  11. If you remove(if its' no longer needed) the project dependency from package.json, it would still be in node_modules folder

    • run npm ls to see the removed project marked as extraneous
    • These can be removed by npm prune command, completely.
  12. To run a script : npm run <script-name>

    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
    },
    
    • a start script will run the app.js file with node

Those are some most used commands to get started, more can be found in npm documentation.