跳到主要内容

Node 包管理器速查(npm / npx / nvm / yarn)

四张独立的短速查表合并为一张:同一份「装包、跑脚本、锁版本、切 Node 版本」的参考,不必在四个页面之间来回跳。

NPM​

Basic Syntax of Node Package Manager​

CommandDescription
npm initInitialize 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-npmUpdate npm
npm runList available scripts to run
npm run barRuns the script named bar
npm testRuns the projects tests
npm listFor 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-0List globally installed packages
npm-windows-upgradesUpgrade npm on Windows
npm outdated -g --depth-0Updated global packages
npm alias default 16Make a node version default
npm i <package>Install a package
npm u <package>Update the package
npm rm <package>Remove a package
npm auditScan and list all the vulnerabilities in the package
npm audit fixFix found vulnerabilities
npm editEdit an installed package
npm publishPublish a package
npm rebuild- Rebuild a package
- This command runs the npm build command on the matched folders

NPX​

Basic Commands​

CommandDescription
npx create-react-app [name]Create a react app
npx npm-check -uUpdate 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​

CommandDescription
nvm ls-remoteLists all available versions of Node
nvm lsLists all locally installed versions
nvm install nodeInstalls 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 currentShows 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 --helpShows NVM help documents.
nvm exec <version> node app.jsRun Node app.js with the PATH pointing to given node version
nvm set-colors cgYmWSet text colors to cyan, green, bold yellow, magenta, and white
nvm run <version> app.jsRun app.js using given Node version
nvm install-latest-npmUpdate 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 -vCheck if nvm is installed
nvm uninstall <version>Un-Install a specific version
nvm lsShow versions available locally
nvm uninstall --ltsUninstall the latest long term support version

Yarn​

Basic Syntax of Yarn​

CommandDescription
yarnInstall dependencies
yarn add [package]Install package
yarn add --dev [package]Install dev package
yarn autocleanFrees up unnecessary files and folders from dependencies
yarn cache cleanRemove the shared cache files
yarn global add [package]Global install package
yarn global remove [package]Global uninstall package
yarn helpAccessing the list of commands
yarn infoSee information related to packages
yarn initCreate a new package
yarn link <destination>Connect the local project to another one
yarn npm loginStore new login info to access the npm registry
yarn npm logoutLogout of the npm registry
yarn npm publishPublish the active workspace to the npm registry
yarn packGenerate 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 upgradeUpdated
yarn upgrade [package]Update package