NPM
Intro
- manager for Node.js packages (or modules)
- package contains all the files you need for a module, modules are JavaScript libraries to include in project
npm install package-name
, then var pn = require('package-name');
in app files
- NPM creates a folder named "node_modules", where the package will be placed, all future packages will be placed in this folder
- npmjs.org
- search.npmjs.org
- add private:true to package.json to prevent accidental publication of private repositories as well as suppressing any warnings generated when running npm install
- save a package as a devDependency by specifying a
--save-dev
flag, packages used for development purposes (running tests, transpiling code,...)
# search packages
npm search mp3
# view details
npm view express
# show all installed packages
npm list
npm list depth 1 # further dependencies depth level
npm list -g # Globally
npm list -g --depth=0
# installing Latest Module
npm install package-name
npm install package-name -g # Globally
# install packages as a developer dependency
npm install package-name --save-dev
# install Specific Module Version
npm install package-name@0.2.6
# install Specific Module Version Range
npm install package-name@0.2.x
# Install package.json Dependencies
npm install -d
# Uninstalling a Module
npm uninstall package-name
npm uninstall package-name -g # Globally
# Updating an Installed Module
npm update package-name
npm update package-name -g
# update all packages
npm update
# check the registry if installed packages are currently outdated
npm outdated
# aliases
npm i package-name # install local package
npm i -g package-name # install global package
npm un package-name # uninstall local package
npm up # npm update packages
npm t # run tests
npm ls # list installed modules
npm ll # print additional package information while listing modules
npm la
# install multiple packages at once
npm i express momemt lodash mongoose body-parser webpack
# folder as npm package, package.json is the project manifest file
npm init
npm init -y # quicker way
# it is recommended that you give your user account
# access to the /usr/local folder instead
# so that you can just issue normal commands in there
sudo chown -R $USER /usr/local
# --- OR change the Location of Global Packages
# npm global configuration
npm config list
# get config
npm config get prefix # is /usr for example
# set config
# install global packages in our home directory
cd ~ && mkdir .node_modules_global
npm config set prefix=$HOME/.node_modules_global
# and install npm again
npm install npm --g
# add .node_modules_global/bin to our $PATH environment variable,
# so that we can run global packages from the command line
# append the following line
# to your .profile, .bash_profile or .bashrc and restarting your terminal
export PATH="$HOME/.node_modules_global/bin:$PATH"
# now our .node_modules_global/bin will found first
# and the correct version of npm will be used
which npm
# npm keeps a copy of installed package
# copies are cached in the .npm directory in your home path
ls ~/.npm
# clean it up
npm cache clean
# purge all node_module folders from your workspace
# if you have multiple node projects
find . -name "node_modules" -type d -exec rm -rf '{}' +
# running project
npm test
npm run start # run "start" script specified in package.json
npm run test # run Jest test against all .test.ts or .spec.ts files ("test" script)
npm run build # run a production build generation ("build folder")
# server runs at http://localhost:3000
Semantic Versioning
pnpm
- fast, disk space efficient package manager
- up to 2x faster than npm
- files inside
node_modules
are cloned or hard linked from a single content-addressable storage
- built-in support for multiple packages in a repository
- creates a non-flat
node_modules
by default, so code has no access to arbitrary packages
- how
pnpm <command>
works, a shortcut where pnpm exec
and pnpm run
are explicit commands without fallback:
- IF
<command>
is a pnpm command (add, install,...), execute that command
- ELSE IF
<command>
is a script found in package.json
, execute pnpm run <command>
- ELSE execute
pnpm exec <command>
# installing with NPM, when Node.js already installed:
npm install -g pnpm
# with Node.js not installed:
npm install -g @pnpm/exe
# installing with Windows Powershell:
iwr https://get.pnpm.io/install.ps1 -useb | iex
# installing with Chocolatey:
choco install pnpm
# initialize a new project, get the default package.json in the current directory
pnpm init
# install project dependencies
pnpm install
pnpm i
# run a package.json script
pnpm run build
pnpm build
# add new dependencies to project
pnpm add <pkg>
# add new dev dependency, -D (or --save-dev) flag
pnpm add -D <pkg>
# install a package globally, -g flag:
pnpm add -g <pkg>
pnpm add <pkg>@<tag>
pnpm add <pkg>@<version>
pnpm add <pkg>@<version range>
pnpm add <git host>:<git user>/<repo name>
pnpm add <git repo url>
pnpm add <tarball file>
pnpm add <tarball url>
pnpm add <dir>
# remove dependencies from the project
pnpm remove <pkg>
pnpm rm <pkg>
pnpm rm -g <pkg> # remove a globally installed package
pnpm uninstall <pkg>
pnpm un <pkg>
# update packages to their latest versions based on the specified range in package.json
pnpm update
pnpm up
# update all dependencies to their latest versions, ignoring ranges specified in package.json
pnpm up --latest
# run 'scripts' > 'start' command, specified in the package
pnpm start
# run 'scripts' > 'test' command, if one was provided
pnpm test
pnpm t
# npx alternative,
# download specified package from the registry without installing it as a dependency,
# and run whatever default command binary it exposes.
pnpm dlx <pkg> <command> <value>
# executed command:
# 1 - single entry of 'bin' field of package.json.
# 2 - command name in the 'bin' field of package.json that matches the package name, ignoring the scope part if any.
# 3 - throws an error with a helpful error message.
pnpm --package=typescript dlx tsc --init
# setup tab-completion
pnpm install-completion
# display a tree-like view of the project dependencies
pnpm list
pnpm ls
# checks for known security issues
pnpm audit
# check licenses
pnpm licenses
# check for outdated packages
pnpm outdated
# overview of the content-addressable store status
pnpm store status
# optimize disk space usage, remove unreferenced packages from the store
pnpm store prune
# prints the path to the active store directory
store path
# checks for modified packages in the store
store status
# publish packages to registries, and share them with the community
pnpm publish
# execute commands specified by the 'bin' field in installed package package.json file.
# run executable of the corresponding file with the same name in project node_modules/.bin directory.
pnpm add -D typescript # installing typescript as a dev dependency
pnpm exec tsc --init # running the tsc command to create a tsconfig.json file
pnpm tsc # omit exec, when there is no conflicting 'tsc' script in package.json
# 'pnpm exec' prepends ./node_modules/.bin to the PATH (PATH=./node_modules/.bin:$PATH) and then executes command.
# has access to all commands resolved by the paths specified in PATH,
# access many system commands for example rm, ls, and so on.
# PNPM Workspaces - manage multiple packages within a monorepository
# define a single package.json file at the root of the repository
# and specify the individual packages as workspaces, PNPM treats them as interdependent projects.
# running 'pnpm install' at the root of the repository installs dependencies for all workspaces.
# pnpm-workspace.yaml
{
"name": "my-monorepo",
"private": true,
"workspaces": [
"packages/*"
]
}
# run a command for each project in the workspace
pnpm <command> --recursive
pnpm <command> --r
serve - static file serving and directory listing
npm install -g serve
serve -s build # serve static site on the port 5000
serve -s build -l 4000 # adjust port
http-server
npm install http-server -g
http-server -p 8080 -c-1 dist/project-name
http-server -p 8080 -c-1 dist/project-name/index.html
./node_modules/.bin/http-server -a localhost -p 8000
# COMMAND DESCRIPTION:
#
# http-server [path] [options]
#
# [path]
# defaults to ./public if the folder exists, and ./ otherwise
#
# [options]
# -a - address to use (defaults to 0.0.0.0)
# -p - port to use (defaults to 8080)
# -d - show directory listings (defaults to 'True')
# -i - display autoIndex (defaults to 'True')
# -g or --gzip - when enabled (defaults to 'False')
# will serve ./public/some-file.js.gz
# in place of ./public/some-file.js
# when a such file exists and request accepts gzip encoding
# -e or --ext - default file extension if none supplied (defaults to 'html')
# -s or --silent - suppress log messages from output
# --cors - enable CORS via the Access-Control-Allow-Origin header
# -o - open browser window after starting the server
# -c - set cache time (in seconds) for cache-control max-age header
# -c10 for 10 seconds (defaults to '3600'), to disable caching, use -c-1
# -U or --utc - use UTC time format in log messages
# -P or --proxy - proxies all requests which cant be resolved
# locally to the given url. e.g.: -P http://someurl.com
# -S or --ssl - enable https
# -C or --cert - path to ssl cert file (default: cert.pem)
# -K or --key - path to ssl key file (default: key.pem)
# -r or --robots - provide a /robots.txt (defaults to 'User-agent: *\nDisallow: /')
# -h or --help - print this list and exit
Back to Main Page