13
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

npmモジュール開発時の動作確認に便利な npm link

Posted at

npmにpublishされているモジュールの開発時に動作確認する時どうするんだろうと思って調べてみたら npm link という便利なコマンドがあった

使い方

開発するモジュール(例:redux)のディレクトリ配下で npm link と叩くと prepublish が走った後グローバルな
node_modules にシンボリックリンクが作成される

~/sandbox/github.com/reactjs/redux (master ✔) ᐅ npm link

> redux@3.6.0 prepublish /Users/yamamoto/sandbox/github.com/reactjs/redux
> yarn run clean && yarn run lint && yarn test && yarn run build && check-es3-syntax lib/ dist/ --kill --print
# 略 (npm script)
/Users/yamamoto/.nodenv/versions/6.10.1/lib/node_modules/redux -> /Users/yamamoto/sandbox/github.com/reactjs/redux

動作確認したいディレクトリで npm link redux と叩く

~/sandbox/github.com/reactjs/redux/examples/todos-flow (master ✔) ᐅ npm link redux
/Users/yamamoto/sandbox/github.com/reactjs/redux/examples/todos-flow/node_modules/redux -> /Users/yamamoto/.nodenv/versions/6.10.1/lib/node_modules/redux -> 
/Users/yamamoto/sandbox/github.com/reactjs/redux

node_modules にシンボリックリンクが作成される

~/sandbox/github.com/reactjs/redux/examples/todos-flow (master ✔) ᐅ ll node_modules | grep redux
drwxr-xr-x   11 yamamoto  staff   374B  4 21 17:17 react-redux
lrwxr-xr-x    1 yamamoto  staff    67B  4 21 19:12 redux -> ../../../../../../../.nodenv/versions/6.10.1/lib/node_modules/redux

yarnの場合

yarn の場合もっと良くて、単に yarn link したディレクトリへのシンボリックリンクになる(相対パス)

~/sandbox/github.com/reactjs/redux (master ✔) ᐅ yarn link
yarn link v0.22.0
success Registered "redux".
info You can now run `yarn link "redux"` in the projects where you want to use this module and it will be used instead.
✨  Done in 0.16s.
~/sandbox/github.com/reactjs/redux/examples/todos-flow (master ✔) ᐅ yarn link redux
yarn link v0.22.0
success Registered "redux".
✨  Done in 0.17s.

~/sandbox/github.com/reactjs/redux/examples/todos-flow (master ✔) ᐅ ll node_modules | grep redux
drwxr-xr-x   11 yamamoto  staff   374B  4 21 17:17 react-redux
lrwxr-xr-x    1 yamamoto  staff     8B  4 21 19:33 redux -> ../../..

これでnpmモジュール開発が捗りますね。

13
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?