Skip to content

npm 源

node version Manage:

sh
# .bashrc .zshrc config.fish
alias npp=pnpm

# 查看版本
pnpm view XXX versions

# 删除未被任何项目 引用的包
pnpm store prune
sh
pnpm get registry

pnpm config set registry https://registry.npmmirror.com

pnpm config set registry https://registry.npmjs.org/
sh
npm config get registry

npm config set registry https://registry.npmmirror.com

npm config set registry https://registry.npmjs.org/

npm install --registry https://registry.npmmirror.com
sh
yarn config get registry

yarn config set registry https://registry.npmmirror.com

yarn config set registry https://registry.yarnpkg.com

手动安装 nvm

https://zhuanlan.zhihu.com/p/58466443

sh
cd ~/ ; git clone https://ghp.ci/https://github.com/creationix/nvm.git .nvm

cd ~/.nvm; git checkout v0.40.0; . nvm.sh

vim ~/.bashrc

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

nvm-windows 淘宝镜像

打开安装目录,默认在 C:\Users\用户名\AppData\Roaming\nvm

打开 settings.txt 然后 添加 淘宝镜像

node_mirror: https://npmmirror.com/mirrors/node/
npm_mirror: https://npmmirror.com/mirrors/npm/

windows 脚本 问题

npm : 无法加载文件 C:\nvm4w\nodejs\npm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fw link/?LinkID=135170 中的 about_Execution_Policies。

管理员 打开 powershell

sh
set-executionpolicy remotesigned

选择 A

package.json

版本号通常遵循 MAJOR.MINOR.PATCH 的格式,其中:

  • MAJOR:主版本号,不兼容递增

  • MINOR:次要版本号,向后兼容递增,新功能

  • PATCH:补丁版本号,兼容递增,问题修正

  • ~ 匹配次要版本 MINOR,比如 ~1.15.2 会匹配所有 1.15.x 的版本,但不包括 1.16.0

  • ^ ^3.3.4 会匹配所有 3.x.x 的版本,但不会更新到 4.0.0

跳过脚本安装

sh
pnpm install --ignore-scripts

Electron

sh
# 设置 ELECTRON_MIRROR 环境变量
export ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"

# 然后重新安装
pnpm add electron@28.2.0

或者使用 .npmrc

electron_mirror="https://npmmirror.com/mirrors/electron/"
ELECTRON_BUILDER_BINARIES_MIRROR="https://npmmirror.com/mirrors/electron-builder-binaries/"

npm install git

sh
# http
npm install git+https://github.com/your-org/my-shared-types.git

# ssh
npm install git+ssh://git@github.com:your-org/my-shared-types.git

锁定版本

json
"dependencies": {
  "@your-org/shared-types": "git+https://github.com/your-org/my-shared-types.git#v1.0.0"
}

分支

sh
npm install git+https://github.com/your-org/my-shared-types.git#main

方案 1

sh
"pnpm": {
  "overrides": {
    "@your-org/shared": "link:../shared"
  }
}
js
# remove-overrides.mjs
import fs from 'fs'

const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8'))
delete pkg.pnpm?.overrides
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2))
sh
node remove-overrides.mjs
pnpm install

发布 npm 包

sh
npm login

# 添加 NPM账号
npm adduser

# 查看当前登录的账号
npm whoami

# 登出当前账号
npm logout
sh
npm publish

npm publish --access public
sh
# patch:补丁号,修复bug,小变动,如 v1.0.0->v1.0.1
npm version patch

# minor:次版本号,增加新功能,如 v1.0.0->v1.1.0
npm version minor

# major:主版本号,不兼容的修改,如 v1.0.0->v2.0.0
npm version major

.npmignore 用法与 .gitignore

一些问题记录

1

npm package 的 devDependencies ,是不会安装的。 举个例子:a devDependencies b,pojectH 使用了 a,但是在安装 a 的时候,不会安装 b。

如果这个 b 是 types,那么 pojectH 就会一直缺少 b 的 types,除非你自己安装。

2

使用 workspace 时候,需要注意一下。

3 error:0308010C:digital envelope routines::unsupported

设置参数,解决报错

在执行运行项目命令之前,先运行这个命令:

sh
# Linux & Mac OS:
export NODE_OPTIONS=--openssl-legacy-provider

# windows
set NODE_OPTIONS=--openssl-legacy-provider