Skip to content

NVM https://heynode.com/tutorial/install-nodejs-locally-nvm/

NPM Fundamentals Link

  • Npm stands for Node Package Manager

  • package manager for the Node JavaScript platform.

Node Package Manager provides two main functionalities

  1. Provides online repositories for node.js packages/modules, which are searchable on search.nodejs.org

  2. Provides command-line utility to install Node.js packages and also manages Node.js versions and dependencies

package.json

  • every npm project has a file called package.json located in the root directory.

  • To maintain versions of libs being installed in a project we use package.json and package-lock.json so that there is no issue in porting that app to a different environment.

  • To create the package.json file

    `npm init --yes`
    
  • The package.json file is the heart of a Node.js system. This file holds the metadata for a particular project.

  • Once the installation is completed, you will see a new directory called /node_modules created under the root of the project. All the new modules that you install will be placed in this directory.

  • If you expand the /node_modules directory, you will see that npm installed not only express but also the dependencies of express, and dependencies of these dependencies, and so on.

  • If you open the package.json file in the root of the project, you will also find that the dependencies section is updated, which includes the express package

  • used exclusively with node.js as package manager or as build tool for front end

Setting up a sample project

 npm init --yes

Setting up a sample project

 npm i <Package-name>

uninstall Package

 npm un <Package>

Update package

 npm up <Package>

Instantly initialize project with available package.json file

npm install

intall package as development dependency

  • sometimes, you may want to install a package that runs only on the development environment.

  • This command will download and install the morgan package. In addition, it adds a new section to the package.json file called devDependencies

  • devDependencies are a collection of the dependencies used during the development of your application: the modules that you need to use to build it but don't need when it's running. This could include testing tools, a local server to speed up your development, and more.

npm install <Morgan> --dev

list installed packages and their dependencies

  • npm list / npm ls

  • npm la or npm ll command, the output will also include extended information.

Listing packages as a tree with a specified depth

  • npm list --depth=0

Listing packages in dependencies

  • npm list --prod / production

Listing packages in devDependencies

  • npm list --dev / --development

Listing packages in the global packages

  • npm list --global

Formatting installed packages in JSON format

  • npm list --depth=0 --json

information on a package:

  • npm view

--save

  • Before npm 5, when you ran npm install to install a module, it was only added to the node_modules directory.

  • Thus, if you want to add it to the project's dependencies in the package.json, you must add the optional flag --save (or -S) to the command.

  • Nowadays, since this is the default behavior, no flag is needed (although it's kept for compatibility purposes); however, if for some reason you want to go back to the old usage (i.e., install only to the node_modules folder but not add it to the package.json dependencies section) the --no-save flag is what you're looking for.

Upgrade npm on Windows

  • npm-windows-upgrade

have multiple versions of Node.js installed

  • nvm use 10.19.0

Searches the npm registry for packages matching the search terms

  • NPM search

Location of npm Global Packages

  • npm config get prefix

Tip: you can also add private: true to package.json to prevent accidental publication of private repositories, as well as suppressing any warnings generated when running npm install.

Manage npm's Cache

  • When npm installs a package, it keeps a copy, so the next time you want to install that package, it doesn’t need to hit the network. The copies are cached in the .npm directory in your home path:

  • This directory will get cluttered with old packages over time, so it’s useful to clean it up occasionally: - $ npm cache clean --force

  • Explain the difference between local and global npm packages installation

  • local packages are installed in the directory where you run npm install <package-name>, and they are put in the node_modules folder under this directory

  • global packages are all put in a single place in your system (exactly where depends on your setup), regardless of where you run npm install -g <package-name>

Package-lock.json

  • package-lock.json is automatically generated for any operations where npm modifies either the node_modules tree, or package.json. It describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates.

  • Describe a single representation of a dependency tree such that teammates, deployments, and continuous integration are guaranteed to install exactly the same dependencies. d