npm - GradedJestRisk/js-training GitHub Wiki

Beginner's guide

Table of Contents

Overview

Commands:

  • node module name contentions: lower case and usually dash-separated eg. gilded-rose
  • look for a package npm search DESCRIPTION
  • install package npm install PACKAGE_NAME@VERSION
  • get installed packages npm list -g --depth=0
  • reinstall project from package.json npm install (delete node_modules to check)
  • execute application tests npm run test
  • generate a version and execute it :
    • (start is usually a script node <ENTRY-POINT>.jqs
    • for dev, on a local server npm run start (shorthand: npm start )
    • for production npm run build
  • list all global modules npm list -g --depth 0
  • execute a scripts npm run <SCRIPT>
  • execute cli npm exec / npx doc

Repository

OS:

  • windows C:\Windows\System32\node_modules\

Start a project

Choose a name, kebab-case

interactive

run npm init and answers questions, all licence valueshere

all defaults

run npm init --y ,that's all

from text file

Steps

  • optional: create a .nvmrc file
  • create your package.json following all these rules
  • validate it here
  • then run npm init --yes

keywords

Al the very least, include:

  • name
  • version
  • description
  • main
  • scripts/test

sample

{
  "name": "Test project",
  "version": "1.0.0",
  "description": "Hello world from node",
  "main": "helloWorld.js",
  "scripts": {
    "test": "jest"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/GradedJestRisk/js-training.git"
  },
  "keywords": [
    "jest"
  ],
  "author": "GradedJestRisk",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/GradedJestRisk/js-training/issues"
  },
  "homepage": "https://github.com/GradedJestRisk/js-training#readme"
}

Dependencies

Check installed size cost-of-modules

limit transitive version to a single one

Look for existing js-yaml $ find node_modules.new -path '*/js-yaml/package.json'|xargs grep version node_modules.new/mocha/node_modules/js-yaml/package.json: "version": "4.0.0", node_modules.new/depcheck/node_modules/js-yaml/package.json: "version": "3.14.1", node_modules.new/@eslint/eslintrc/node_modules/js-yaml/package.json: "version": "3.14.1", node_modules.new/js-yaml/package.json: "version": "4.1.0",

So we have 3.14.1 and 4, let's choose 3.14.1

To do so npm install --save js-yaml@3 , then dedupe npm dedupe

add

Check its size with npm-consider

For:

  • production : npm install --save-prod PACKAGE_NAME or npm i PACKAGE_NAME
  • development only : npm install --save-dev PACKAGE_NAME or npm i -D PACKAGE_NAME

update

npm update the module using outdated/update, following rules in package.json, but it will NEVER update to major version, unless you explictly instruct him to do so using ncu/ncu -u.

minor

Steps:

  • check for update npm outdated
  • update all packages npm update

major

You need to update package.json

  • do it manually
  • use convenience package sudo npm install -g npm-check-updates
Steps:
  • check for update npm-check-updates (ncu)
  • update package.json npm-check-updates --upgrade (ncu -u)
  • update module npm install

single package

List:

  • update to version: : npm install --save-dev <PACKAGE_NAME>@<VERSION_NUMBER>
  • update to latest (including breaking change): npm install --save-dev <PACKAGE_NAME>@latest

look for deprecated package

A deprecated package is still available on npm, but is no longer maintained npx check-is-deprecated --file ./package.json

npm itself

sudo npm install -g npm

Manage the current module version

Version is stored in package.json

{
  "name": "vanilla-js",
  "version": "1.0.2"
}

npm command is version, API here

List:

  • get version: node -p -e "require('./package.json').version
  • change version
    • version number should use semver syntax
    • change version
      • to supplied version number: npm version <VERSION_NUMBER> using 1.2.0, 1.0.0 => 1.2.0
      • using semver (major, minor, patch): npm version <SEMVER_DELTA> using patch, 1.0.0 => 1.0.1
Note: as-is npm version doe the following under the hood
  • create a git tag
  • create a git commit with version number
If it doesn't fit you need, you can:
  • disable it: npm version --no-git-tag-version patch
  • keep it, but set your own commit message: npm version -m "Upgrade to version %s for so-and-so"

Changelog

Generate according to git commit history:

Make a sandbox

  • cd /tmp
  • git init version-sandbox
  • cd version-sandbox
  • npm init --yes
  • git commit -am "Initial version"
  • git commit --allow-empty -m "Fix bug 1"
  • npm version patch
  • git commit --allow-empty -m "Fix bug 2"
  • npm version patch
  • git commit --allow-empty -m "Add feature 1"
  • npm version minor
  • git commit --allow-empty -m "Add breaking change 1"
  • npm version major
  • git log
Install conventional chnagelog:
  • npm install conventional-changelog
  • npm install conventional-changelog-cli
Generate changelog:
  • from start: npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0
  • last change only: conventional-changelog -p angular -i CHANGELOG.md -s

VC integration

Exclude repository (node_module)

Commit

  • package.json
  • package-lock.json

Shortcuts

action parameter
install (local) i
install global i -g
uninstall un
update u
test t
list installed ls

Lock version

npm install --package-lock

Tasks

List:

  • start: run start
  • test: run test
  • check code: run lint
  • fix code : run format
  • DB:
    • create form start: run db:flush
    • migrate until last: run db:migrate

Starting point

Default to server.js in project root folder.

"scripts": {
    "start": "node <PATH-TO-SCRIPT>"
}

Clear cache

Source

npm stores cache data in an opaque directory within the configured cache, named _cacache. This directory is a cacache-based content-addressable cache that stores all http request data as well as other package-related data. All data that passes through the cache is fully verified for integrity on both insertion and extraction. Cache corruption will either trigger an error, or signal to pacote that the data must be refetched, which it will do automatically. For this reason, it should never be necessary to clear the cache for any reason other than reclaiming disk space, thus why clean now requires --force to run.

List:

  • see cache files: ls -la /.npm/_cacache/_cacache
  • reclaim space : npm cache verify
  • clear all cache (in emergency): npm cache clear --force

Run CLI

npx stands for npm execute

List:

  • run a package without installing it : npx <PACKAGE_NAME> <ARGUMENTS>
  • run a (non-globally-installed) package without downloading it : npx --no-install <PACKAGE_NAME> <ARGUMENTS>
  • run a package in a nested directory (several package.json): node ./node_modules/<PACKAGE_NAME> <ARGUMENTS>


              
⚠️ **GitHub.com Fallback** ⚠️