npm, yarn,yalc常用命令&问题

源配置

cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
// 查看版本
cnpm -v
// 之后下载东西就用cnpm代替npm下载
npm 配置源(yarn 一样)
// 查看当前下载地址
npm config get registry
// 设置淘宝镜像的地址
npm config set registry http://registry.npm.taobao.org
// 查看当前的下载地址
npm config get registry
nrm源管理
// nrm安装
npm install -g nrm
// 查看可选源
nrm ls
npm ---------- https://registry.npmjs.org/
yarn --------- https://registry.yarnpkg.com/
tencent ------ https://mirrors.cloud.tencent.com/npm/
cnpm --------- https://r.cnpmjs.org/
taobao ------- https://registry.npmmirror.com/
npmMirror ---- https://skimdb.npmjs.com/registry/
// 切换为taobao源
nrm use taobao

下载/卸载

npm install [包名] 下载
npm install [包名]@1.0.0 下载指定版本
npm view [包名] versions 查看包有哪些版本
npm uninstall [包名] 卸载

清除缓存

npm cache clear --force 清除缓存
yarn cache dir 清除全局缓存目录
yarn cache clean --force 清除缓存

发布npm包

查看当前登录用户
npm who am i
注册用户
npm adduser --registry=https://example.com
之前已经注册过了,使用登录命令
npm login --registry=https://example.com
发布到公共仓库
npm publish
指定发布仓库源
npm publish --registry=https://example.com
或者项目package.json里面配置发布源

"publishConfig": {
    "registry": "https://example.com"
  }

指定前缀为@mylib 的文件,都从 http://example.com拉取
npm config set @mylib:registry=http://example.com

显示当前安装的所有全局包

npm list -g --dept 0 一级目录
npm list -g 多级目录

链接本地npm包,

npm link 是把包链接到全局npm下
npm unlink [包名]
如果unlink 取消链接失败,可以查找到全局包,移除掉
where npm
~/usr/.nvm/versions/node/v14.15.4/bin/npm(路径样例)

//npm路径是在bin下,我们要到lib目录下
cd ../ 
cd [npm路径]/lib/node_modules
rm -rf [package-name]

如果使用 npm link 或者 yarn link 后提示react ,react-dom 版本不一致,可用使用yalc

yalc

组件仓库构建
yarn build
组件仓库发布(注意包是发布到本地的)
yalc publish
后续更新build 之后推送
yalc push
项目仓库(使用组件的仓库)link组件仓库
yalc add [组件仓库名]
关联后会项目中添加.yalc文件,查看node_modules里组件文件一致(跟你组件库比较),如果没有更新,或者有问题清除node_modules 重新install 试一下,或者清除node_modules 下的.cache 文件,重新yarn start 项目,保证组件库代码已经更新到项目仓库
调试结束,移除组件库本地link
yalc remove --all

遇到的问题

Q:Found incompatible module
A:yarn config set ignore-engines true

Q:unable to resolve dependency tree
A:npm i --legacy-peer-deps

Q:Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app

A:这个一般发生在Link 调试时候,因为React ,React-dom 版本不一致发生的,可以使用yalc 解决。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容