NPM
npm: node package manager.
npm 由三部分组成:
- website https://www.npmjs.com
- CLI
- registry https://registry.npmjs.org
config:
// 修改registry
$ npm config set registry https://registry.npm.taobao.org
$ npm config get registry
// 修改默认全局安装路径
mkdir /path/npm_global
npm config set prefix /path/npm_global
echo 'export PATH=/path/npm_global/bin:$PATH' >> ~/.profile
source ~/.profile
// 查看配置
$ npm config ls -l
install:
// 根据当前目录package.json安装到当前目录node_modules
$ npm install
// 安装pkg到当前目录的 node_modules
$ npm install <pkg>
// 安装到全局的node_modules
$ npm install -g <name>
--save-dev // 安装并自动更新到package.json的devDependencies.
--no-save // 不保存到package.json 和 package-lock.json.
--no-package-lock // 不生成package-lock.json
list:
// 查看安装了哪些包
$ npm list --depth=0 --global
init:
// 创建package.json
$ npm init
test:
$ npm test
publish:
# 注册npm账号
# 添加账号
npm adduser
# 验证账号
npm whoami
# 发布
npm publish
发布好了可以去npm网站查看.
npm + shrinkwrap = yarn lock npm-shrinkwrap.json = yarn.lock
folders
nodejs加载的路径:
1. 当前目录
2. $HOME/.node_modules
$HOME/.node_libraries
$PREFIX/lib/node_modules
3. $NODE_PATH
// 查看prefix (也是-g 安装的目录)
$ npm config ls -l | grep prefix
npmrc
get token
curl -u<username>:<api-key> https://artifactory.canux.com/artifactory/api/npm/auth
“.npmrc” 配置private registry
registry=https://artifactory.canux.com/artifactory/api/npm/iep.npm-virtual
always-auth=true
email=<user-email>
//artifactory.arm.com/artifactory/api/npm/:_auth=<token>
npmignore
打包的时候忽略的文件和目录,优先级高于gitignore。
package.json
package.json
package-locks
package-lock.json
yarn
使用corepack安装yarn。
激活corepack
$ corepack enable
安装yarn
$ corepack prepare yarn@3.8.1 --activate
CLI:
// yarn config, yarn 会读npmrc
yarn config
cacheFolder: $HOME/.yarn/berry/cache
globalFolder: $HOME/.yarn/berry
// create package.json
$ yarn init
// 设置project的yarn version,自动下载.yarn/release/yarn-<version>.cjs
yarn set version berry
// remove all local archives
$ yarn cache clean
// add/remove/install dependencies
$ yarn add ...
$ yarn remove ...
$ yarn install --inline-builds
// nodejs
$ yarn node
// check npm package information
$ yarn npm info react
// run script defined in package.json
$ yarn run <scriptName>
// plugin management
$ yarn plugin ......
// workspace management
$ yarn workspaces ...
$ yarn workspaces list
$ yarn workspaces foreach // run command on all workspaces
// login to private registry
yarn npm login
yarn npm logout
yarn npm publish
npmRegistries:
"https://artifactory.arm.com/artifactory/api/npm/iep.npm-virtual":
npmAuthToken: <token>