npm - GradedJestRisk/js-training GitHub Wiki
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
- (start is usually a script
- list all global modules
npm list -g --depth 0
- execute a scripts
npm run <SCRIPT>
- execute cli
npm exec / npx
doc
OS:
- windows
C:\Windows\System32\node_modules\
run npm init
and answers questions, all licence valueshere
run npm init --y
,that's all
Steps
- optional: create a .nvmrc file
- create your package.json following all these rules
- validate it here
- then run
npm init --yes
Al the very least, include:
- name
- version
- description
- main
- scripts/test
{ "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" }
Check installed size cost-of-modules
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
Check its size with npm-consider
For:
- production :
npm install --save-prod PACKAGE_NAME
ornpm i PACKAGE_NAME
- development only :
npm install --save-dev PACKAGE_NAME
ornpm i -D PACKAGE_NAME
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.
Steps:
- check for update
npm outdated
- update all packages
npm update
You need to update package.json
- do it manually
- use convenience package
sudo npm install -g npm-check-updates
- check for update
npm-check-updates (ncu)
- update package.json
npm-check-updates --upgrade (ncu -u)
- update module
npm install
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
A deprecated package is still available on npm, but is no longer maintained
npx check-is-deprecated --file ./package.json
sudo npm install -g npm
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
- to supplied version number:
npm version
doe the following under the hood
- create a git tag
- create a git commit with version number
- 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"
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
- npm install conventional-changelog
- npm install conventional-changelog-cli
- from start:
npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0
- last change only:
conventional-changelog -p angular -i CHANGELOG.md -s
Exclude repository (node_module)
Commit
- package.json
- package-lock.json
action | parameter |
---|---|
install (local) | i |
install global | i -g |
uninstall | un |
update | u |
test | t |
list installed | ls |
npm install --package-lock
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
- create form start:
Default to server.js in project root folder.
"scripts": { "start": "node <PATH-TO-SCRIPT>" }
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
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>