Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm 相关笔记 #74

Open
zhiwenxuan opened this issue Jun 18, 2021 · 0 comments
Open

npm 相关笔记 #74

zhiwenxuan opened this issue Jun 18, 2021 · 0 comments

Comments

@zhiwenxuan
Copy link
Owner

npm 相关笔记

一些有趣的命令

本地包调试 npm link

平时开发一些独立的npm 模块时,我们难免需要引用调试测试。比如我们开发的npm 模块叫 my-utils,需要引入的工程叫做 my-project

一些不太好做法:

  1. 发布一个beta 版本(麻烦)

  2. 直接使用相对路径安装

$ cd path/to/my-project
$ npm install path/to/my-utils

有修改需要反复install

  1. 使用软链
$ cd path/to/my-project/node_modules
$ ln -s path/to/my-utils my-utils

不同系统命令不一样

正解 npm link

官方文档 https://docs.npmjs.com/cli/v7/commands/npm-link

实现:

# 关联到全局
$ cd path/to/my-utils
$ npm link

# 关联模块
$ cd path/to/my-project
$ npm link my-utils

# 移除关联
$ npm unlink

此命令还可以用于 node-cli 的调试,比如脚手架是 my-cli

// package.json
{
  "bin": {
    "my-cli": "bin/my-cli"
  }
}
# 关联到全局
$ cd path/to/my-cli
$ npm link

这样就可以全局使用 my-cli 命令

参考

atian25/blog#17

npm root -g 查看全局包路径

Mac 默认路径是 /usr/local/lib/node_modules

npm ls -g 查看所有全局包

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant