Node 包管理器速查(npm / npx / nvm / yarn)
四张独立的短速查表合并为一张:同一份「装包、跑脚本、锁版本、切 Node 版本」的参考,不必在四个页面之间来回跳。
NPM
Basic Syntax of Node Package Manager
| Command | Description |
|---|---|
npm init | Initialize a node.js project |
npm install npm i | Install package.json dependencies |
npm install --global [package] npm i -g [package] | install package globally |
npm install-latest-npm | Update npm |
npm run | List available scripts to run |
npm run bar | Runs the script named bar |
npm test | Runs the projects tests |
npm list | For Local packages |
npm un <package_name> npm uninstall <package_name> | Uninstall package from project |
npm -g uninstall <name> | Uninstall global package |
npm update -g <package_name> | Update individual global package(s) |
npm up <package_name> | Update npm package |
npm list -g --depth-0 | List globally installed packages |
npm-windows-upgrades | Upgrade npm on Windows |
npm outdated -g --depth-0 | Updated global packages |
npm alias default 16 | Make a node version default |
npm i <package> | Install a package |
npm u <package> | Update the package |
npm rm <package> | Remove a package |
npm audit | Scan and list all the vulnerabilities in the package |
npm audit fix | Fix found vulnerabilities |
npm edit | Edit an installed package |
npm publish | Publish a package |
npm rebuild | - Rebuild a package - This command runs the npm build command on the matched folders |
NPX
Basic Commands
| Command | Description |
|---|---|
npx create-react-app [name] | Create a react app |
npx npm-check -u | Update all project dependencies |
npx browserslist OR npx browserslist "last 2 versions, not dead, not < 2%" | Browsers list |
npx create-react-app {name} --template {template} | Templates can be added according to developer's need, like redux template, npx create-react-app my-app --template redux |
npx sirdeploy reactapp . | Deploy React App on Github |
NVM
Basic Syntax of Node Version Manager
| Command | Description |
|---|---|
nvm ls-remote | Lists all available versions of Node |
nvm ls | Lists all locally installed versions |
nvm install node | Installs the newest release of Node |
nvm install <version> | Installs the given release of Node |
nvm use <version> | Switches and uses the given Node release |
nvm which <version> | Shows path to the given Node version |
nvm current | Shows what is the currently used Node version |
nvm alias default <version> | Sets the default Node version to the given version |
nvm unalias <alias_name> | Deletes the alias named <alias_name> |
nvm --help | Shows NVM help documents. |
nvm exec <version> node app.js | Run Node app.js with the PATH pointing to given node version |
nvm set-colors cgYmW | Set text colors to cyan, green, bold yellow, magenta, and white |
nvm run <version> app.js | Run app.js using given Node version |
nvm install-latest-npm | Update your version of npm if you use Node installed through nvm |
nvm root <path> | Set the directory where nvm should store different versions of node.js. |
nvm proxy [url] | Set a proxy to use for downloads. Leave [url] blank to see the current proxy. Set [url] to "none" to remove the proxy. |
nvm -v | Check if nvm is installed |
nvm uninstall <version> | Un-Install a specific version |
nvm ls | Show versions available locally |
nvm uninstall --lts | Uninstall the latest long term support version |
Yarn
Basic Syntax of Yarn
| Command | Description |
|---|---|
yarn | Install dependencies |
yarn add [package] | Install package |
yarn add --dev [package] | Install dev package |
yarn autoclean | Frees up unnecessary files and folders from dependencies |
yarn cache clean | Remove the shared cache files |
yarn global add [package] | Global install package |
yarn global remove [package] | Global uninstall package |
yarn help | Accessing the list of commands |
yarn info | See information related to packages |
yarn init | Create a new package |
yarn link <destination> | Connect the local project to another one |
yarn npm login | Store new login info to access the npm registry |
yarn npm logout | Logout of the npm registry |
yarn npm publish | Publish the active workspace to the npm registry |
yarn pack | Generate a tarball from the active workspace |
yarn remove [package] | Uninstall package |
yarn remove [package] | Uninstall dev package |
yarn run <scriptName> | Run a script defined in the package.json |
yarn set version <version> | Lock the Yarn version used by the project |
yarn up [package] | Upgrade dependencies across the project |
yarn upgrade | Updated |
yarn upgrade [package] | Update package |