Skip to content

Instantly share code, notes, and snippets.

@hochun836
Last active October 25, 2024 03:44
Show Gist options
  • Select an option

  • Save hochun836/9497c2c9112186c00ec203c1dd828ad4 to your computer and use it in GitHub Desktop.

Select an option

Save hochun836/9497c2c9112186c00ec203c1dd828ad4 to your computer and use it in GitHub Desktop.
# base
package // 套件/包 (a folder which contains package.json)
module // 模塊 (a file)
version
x.y.z
~x.y.z // x.y._
^x.y.z // x._._
# module spec
CommonJS (CJS)
module.exports // <=> exports
require // see module.exports, not exports
ESModule (ESM)
export
import
# nvm
nvm -h
nvm list
nvm use <version>
nvm install <version>
=> ref: https://github.com/nvm-sh/nvm
=> ref: https://github.com/coreybutler/nvm-windows
# node
node // enter interaction mode (ps. leave by .exit)
node -h // -h <=> --help
node -v // -v <=> --version
# npm cli
npm -h // node package manager
npm -v
npm -l // display full usage info
npm config -h
npm config get <key> [<key> ...]
npm config get userconfig // C:\Users\<user>\.npmrc
npm config get globalconfig // C:\Program Files\nodejs\etc\npmrc
npm config get registry
npm config get prefix
npm config get init-module // C:\Users\<user>\.npm-init.js
npm config list
npm config list -l // -l <=> --long, show all defaults
npm config list --json
npm config set <key>=<value> [<key>=<value> ...] [-L <global|user|project>] // -L <=> --location, default is 'user'
npm config set xxx=999
npm config set aaa=111 -L project
npm config set bbb=222 -L user
npm config set ccc=333 -L global
npm config delete <key> [<key> ...] [-L <global|user|project>] // -L <=> --location, default is 'user'
npm config delete xxx
npm config delete aaa -L project
npm config delete bbb -L user
npm config delete ccc -L global
npm config edit [-L <global|user|project>] // -L <=> --location, default is 'user'
npm config edit
npm config edit -L project
npm config edit -L user
npm config edit -L global
npm prefix -h
npm prefix
npm prefix -g
npm init -h
npm init // create package.json
npm init -y // -y <=> --yes
npm init --scope=@<scope>
npm i -h // i <=> install
npm i <package>
npm i <package>@<tag>
npm i <package>@<version>
npm i <package> --save // --save <=> -P
npm i <package> --save-dev // --save-dev <=> -D
npm un -h // un <=> uninstall, unlink
npm un <package>
npm un <package>@<tag>
npm un <package>@<version>
npm update -h
npm update <package>
npm ls -h // ls <=> list
npm ls // list local installed packages
npm ls --link // filter only linked packages
npm ls <package> // filter only <package>
npm ls -g // list global installed packages
npm ls -g --link
npm ls -g <package>
npm cache -h
npm cache clear
npm run <script> // script is defined in package.json
npm whoami -h
npm whoami
npm login // <=> npm adduser
npm logout
npm v -h // view registry info, v <=> info, show
npm v // view current package registry info
npm v <package> // view specified package registry info
npm v <package> <field>
npm v vue-loader
npm v vue-loader version
npm v vue-loader dependencies
npm version -h
npm version // show the current package version (version in package.json)
npm version <version> // update the current package version, and version format: major.minor.patch
npm version major // major++
npm version minor // minor++
npm version patch // patch++
npm publish -h
npm publish
npm publish --access public
npm publish --access restricted
npm unpublish -h
npm unpublish @<scope>/<package> -f
npm ln -h // ln <=> link
npm ln // create a symlink ({prefix}/lib/node_modules/<package> -> the package where the npm link command was executed)
npm ln <package> // create a symlink (node_modules/<package> -> {prefix}/lib/node_modules/<package>)
npm un -h // un <=> uninstall, unlink
npm un
npm un <package>
npm explore <package> // open a subshell which working directory is .\node_modules\<package> (ps. the <package> must be installed before)
npm explore <package> -- <command> // the command is run in the subshell, which then immediately terminates
npm explore @hochun836/mini-package
npm explore @hochun836/mini-package -- cd , // for windows
npm explore @hochun836/mini-package -- pwd // for linux
npm help -h
npm help <term>
npm help i
npm help ls
=> ref: https://docs.npmjs.com/cli/v8/commands
NOTE: npm init
when "npm init", it can set up a new or existing package.json
when "npm init <initializer>", it is converted to "npm exec create-<initializer>"
=> ref: https://elijahmanor.com/blog/npm-init-initializer
=> ref: https://docs.npmjs.com/cli/v8/commands/npm-init
NOTE: customize npm init
way1. npm config set init-<xxx> <value> -g // <xxx> can be found by command: npm config ls -l | findstr init
way2. write a .npm-init.js // this file location can be found by command: npm config get init-module
=> ref: https://blog.greenroots.info/tips-to-customize-npm-init-to-make-it-your-own
NOTE: npm i
according to package.json, package-lock.json
in node_modules directory
added x packages, removed y packages
ex.
mkdir demo_0630 && cd demo_0630
npm init -y
mkdir node_modules\a1
mkdir node_modules\a2
mkdir node_modules\a3
dir node_modules
npm i @hochun836/mini-package
dir node_modules
NOTE: npm link
library_0624 (used) : npm ln
project_0624 (use) : npm ln library_0624
=> ref: https://www.bilibili.com/video/BV1wb4y1C76w
=> ref: https://docs.npmjs.com/cli/v8/commands/npm-link
NOTE: list all linked packages
npm ls --link // local
npm ls -g --link // global
NOTE: npm version <new-version | major | minor | patch>
if use a git repository, then
npm version <new-version | major | minor | patch> // it will create a version commit and tag
npm version <new-version | major | minor | patch> -m "<commit-message>"
# npm scripts
user defined scripts
pre & post scripts
special life cycle scripts
=> ref: https://docs.npmjs.com/cli/v8/using-npm/scripts
# npx
npx <command> // command is in node_modules/bin
# nrm
npm i -g nrm
nrm list
nrm use <registry>
# [note] .npmrc (npm running cnfiguration)
local: <project>\.npmrc
global: %USERPROFILE%\.npmrc
# [note] --save vs. --save-dev
--save: package will appear in "dependencies" // --save <=> --save-prod, -P (default)
--save-dev: package will appear in "devDependencies" // --save-dev <=> -D
--save-optional: package will appear in "optionalDependencies" // --save-optional <=> -O
=> ref: https://matthung0807.blogspot.com/2021/07/npm-install-save-save-dev-difference.html
NOTE: get help about 'npm i'
npm help i
# [note] node_modules + package-lock.json
node_modules: place installed packages
package-lock.json: record installed packages version (and other information)
IMPORTANT: don't edit node_modules and package-lock.json manually
# [note] load module
- built-in module
Node.js has several modules compiled into the binary.
The core modules are defined within the Node.js source and are located in the lib/ folder.
Core modules are always preferentially loaded if their identifier is passed to require().
=> ref: https://nodejs.org/api/modules.html#core-modules
=> ref: https://github.com/nodejs/node
- customize module
If the exact filename is not found, then Node.js will attempt to load the required filename with the added extensions: .js, .json, and finally .node.
=> ref: https://nodejs.org/api/modules.html#file-modules
- folder (aka. package) as module
It is convenient to organize programs and libraries into self-contained directories, and then provide a single entry point to those directories.
There are three ways in which a folder may be passed to require() as an argument.
The first is to create a package.json file in the root of the folder, which specifies a main module.
If there is no package.json file present in the directory, or if the "main" entry is missing or cannot be resolved,
then Node.js will attempt to load an index.js or index.node file out of that directory.
=> ref: https://nodejs.org/api/modules.html#folders-as-modules
- third module
If the module identifier passed to require() is not a core module, and does not begin with '/', '../', or './',
then Node.js starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location.
Node.js will not append node_modules to a path already ending in node_modules.
If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.
=> ref: https://nodejs.org/api/modules.html#loading-from-node_modules-folders
# [note] 'browser', 'module', 'main' in package.json
=> ref: https://github.com/SunshowerC/blog/issues/8
=> ref: https://loveky.github.io/2018/02/26/tree-shaking-and-pkg.module/
=> ref: https://nodejs.org/api/packages.html#determining-module-system
# [note] write your own package and publish to the registry
=> ref: https://github.com/hochun836/mini-package
=> ref: https://docs.npmjs.com/packages-and-modules/contributing-packages-to-the-registry
=> ref: https://pjchender.dev/devops/devops-publish-npm-1
# [note] useful packages
- dayjs (https://www.npmjs.com/package/dayjs)
- uuid (https://www.npmjs.com/package/uuid)
- typedoc (https://www.npmjs.com/package/typedoc)
// ==========
// a.js
// ==========
console.log(module.exports);
console.log(exports);
console.log(module.exports === exports);
module.exports = {a:1};
// module.exports.a = 1;
console.log(module.exports);
console.log(exports);
console.log(module.exports === exports);
// ==========
// b.js
// ==========
const a = require('./a'); // see module.exports, not exports
console.log('----------------');
console.log(a);
console.log('----------------');
console.log(module);
{
"name": "demo1", // required
"version": "1.0.0", // required
"description": "",
"main": "index.js", // required
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "hochun836",
"license": "MIT",
"files": [],
"repository": {
"type": "git",
"url": "https://github.com/hochun836/demo1.git"
},
"bugs": {
"url": "https://github.com/hochun836/demo1/issues"
},
"homepage": "https://github.com/hochun836/demo1"
}
/*
IMPORTANT: "main" field specifies a file (a module) used in
case 1. node demo1
case 2. require('demo1')
=> ref: https://docs.npmjs.com/cli/v8/configuring-npm/package-json
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment