Node Package Manager Begin To Advanced - BEHIND JAVA

Node Package Manager Begin To Advanced

Share This

The main use of Node Package Manager is resolve node dependencies. It having same use as the dependency management in maven. By default the NPM is comes with node installation. The npm command is used for managing the dependency. There are two possible ways to install node packages using npm and they are

Local Mode

Here the node packages are installed on a folder called node_module and which is placed inside the parent folder of your project.

Global Mode

In Global mode node dependencies are installed on node configured global folder. So In this scenario if you have multiple project which contain same node packages dependency.The dependency packages are not separately download for each project the dependency will map to the global package folder location.

You can run npm list -g to see where global libraries are installed.

On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally. If you set the NODE_PATH environment variable to this path, the modules can be found by node.

Windows XP - %USERPROFILE%\AppData\npm\node_modules
Windows 7, 8 and 10 - %USERPROFILE%\AppData\Roaming\npm\node_modules

package.json

The package.json file is the heart of Node.js system. It is the manifest file of any Node.js project and contains the metadata of the project.

The metadata information in package.json file can be categorized into below categories:

1. Identifying metadata properties: It basically consist of the properties to identify the module/project such as the name of the project, current version of the module, license, author of the project, description about the project etc.

2. Functional metadata properties: As the name suggests, it consists of the functional values/properties of the project/module such as the entry/starting point of the module, dependencies in project, scripts being used, repository links of Node project etc.

Creating a package.json file:

A package.json file can be created in two ways:

1. Using npm init : Running this command, system expects user to fill the vital information required as discussed above. It provides users with default values which are editable by the user.

2. Writing directly to file : One can directly write into file with all the required information and can include it in the Node project.

Here is an example of package.json file

Here is the explanation of each tags on the package.json

name: The name of the application/project.
version: The version of application. The version should follow semantic versioning rules.
description: The description about the application, purpose of the application, technology used like React, MongoDB, etc.
main: This is the entry/starting point of the app. It specifies the main file of the application that triggers when the application starts. Application can be started using npm start.
scripts: The scripts which needs to be included in the application to run properly.
engines: The versions of the node and npm used. These versions are specified in case the application is deployed on cloud like heroku or google-cloud.
keywords: It specifies the array of strings that characterizes the application.
author: It consist of the information about the author like name, email and other author related information.
license: The license to which the application confirms are mentioned in this key-value pair.
dependencies: The third party package or modules installed using npm are specified in this segment.
devDependencies: The dependencies that are used only in the development part of the application are specified in this segment. These dependencies do not get rolled out when the application is in production stage.
repository: It contain the information about the type and url of the repository where the code of the application lives is mentioned here in this segment.
bugs: The url and email where the bugs in the application should be reported are mentioned in this segment.

Note: Here, “body-parser”, “express”, “express-validator”, “mongoose” and “nodemon” are the modules/packages installed using npm (Node Package Manager).

Some useful npm commands

Update npm itself

npm install -g npm
# Downgrade to a specific version
npm install -g npm@2

Check npm version

npm --version

Install a package

# Local 
npm install package-name

# Local + make an entry in package.json as dependency
npm install package-name --save

# Install specific version of a package
npm install package-name@1.2.3

# Global
npm install -g package-name

Un-install a package

# Local
npm uninstall package-name

# Global
npm uninstall package-name -g

Get package info

# Home page
npm home package_name
# Github repo
npm repo package_name

Check for outdated packages in package.json

# Local
npm outdated

# Global
npm outdated -g

# Production only
npm outdated --prod

List installed packages

# Local with tree
npm ls

# Local - only parent
npm ls --depth=0

# Global - only parent
npm ls -g --depth=0

# List production packages only
npm ls --prod

Remove un-used packages from node_modules folder

npm prune
# Remove all devDependencies from node_modules 
npm prune --production

Update all packages listed in package.json

npm update

Update a single package

npm update package_name

Remove duplicate packages from node_modules

npm dedupe

List packages in cache

npm cache ls
Clean npm cache
npm cache clean -f

Bump version number in package.json and create a git tag automatically

npm version 1.2.3

Lockdown package versions for production

npm shrinkwrap
# Also include devDependencies
npm shrinkwrap --dev

Run npm in production (will not download devDependencies)

npm install --only=production
Install a package from github
npm install git://github.com/user-name/package-name.git#v0.1.0
# OR
npm install user/repo#v1.0.1

Install a package from local cache

npm install --cache-min 999999 package-name

View package info from its package.json file

npm view package_name property_in_json

npm install -g without sudo

-
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
chown -r user_name '~/.npm-global'

Some npm global configs

npm config set save-prefix ~
npm config set save-exact true
npm config set engine-strict true
npm config set ignore-scripts
npm config set init.author.name your_name  
npm config set init.author.email your_email  

No comments:

Post a Comment

Pages