Skip to content

Instantly share code, notes, and snippets.

@externvoid
Last active August 4, 2018 21:55
Show Gist options
  • Select an option

  • Save externvoid/adbadc0cb0d97dbde5b62fce19f8cd60 to your computer and use it in GitHub Desktop.

Select an option

Save externvoid/adbadc0cb0d97dbde5b62fce19f8cd60 to your computer and use it in GitHub Desktop.
npmコマンド、まとめ

インストール情報

npm config list

インストールフォルダ表示

npm config get prefix

インストールフォルダの変更

cd ~ && mkdir .node_modules_global
npm config set prefix=$HOME/.node_modules_global

インストール情報の確認

cat .npmrc

新規インストール

npm install npm --global
echo 'export PATH ="$HOME/.node_modules_global/bin:$PAHT"' >> .bashrc

グローバルモードでモジュールをインストール

npm install cheerio-httpcli --global

システムモジュール、グローバルにinstallされたモジュールの一覧

npm list -g --depth=0

グローバルにinstallしたコマンドはコマンドラインから実行可能

uglifyjs example.js -o example.min.js

ローカルモードでパッケージをインストール

npm init -y

ローカルモードでモジュールをinstall

npm install underscore

package.jsonのdependenciesセクションに項目が追加される

default動作は--saveと同じ

cat package.json

モジュールの稼働を確認

const _ = require('underscore')
console.log(_.range(5)) // => 0, 1, 2, 3, 4

モジュールの互換性問題解決のため古いバージョンをinstall

npm un underscore
npm i [email protected]
npm ls

モジュールの最新版の有無を確認

npm updated

node_modulesフォルダを削除してもpackage.jsonで環境が再現できる

rm -r node_modules
npm i
npm ls

NPMリポジトリのsearch

npm search mkdir

キャッシュの管理

ls ~/.npm
npm cache clean

全てのnode_modulesフォルダの削除

find . name "node_modules" -type d -exec rm -rf'{}' +
find . name "node_modules" | xargs rm -rf

豆知識

npmを使う人の大半は、フロントエンドでJavaScripを書くためのツールのinstallの為に使って要る。 Node.jsを書くためにnpmを使う人は少数。

グローバルモジュールをHOMEにinstall

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH="$HOME/.npm-global/bin:$PATH"
npm install -g npm
npm rebuild # rebuild C++ addon

後からデフォルト設定を変更

npm init -y
npm config set init.author.name foo
npm config set init.author.email [email protected]

便利サイト

npms, npm Discover, Packages by PageRank

Curated npm Lists

Awesome Node.js

モジュールのhomepage, repository, documents, bugnews

npm home package
npm repo package
npm docs package
npm bugs package

無関係モジュールの一斉削除

npm prune

モジュールのバージョン依存を変更

npm config set save-prefix="~" # x.y.zのyの変化を許さない、保守的設定
npm config set save-exact true # バージョン固定
npm shrinkwrap # save above

ローカルモジュールをグローバルモジュールへシンボリックリンク

npm link
npm outdated -g

ローカルプロジェクトで使っているモジュールを参照

"dependencies": {
  "myproject": "file:../myproject/"
  }

キャレットとチルダ

~1.1.2: 1.1.3はOK, 1.2.0はNG、厳しい ^1.1.2: 1.2.0はOK, 2.0.0はNG、ゆるい 許容される記載方法

{ "dependencies" :
  { "foo" : "1.0.0 - 2.9999.9999"
  , "bar" : ">=1.0.2 <2.1.2"
  , "baz" : ">1.0.2 <=2.3.4"
  , "boo" : "2.0.1"
  , "qux" : "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0"
  , "asd" : "http://asdf.com/asdf.tar.gz"
  , "til" : "~1.2"
  , "elf" : "~1.2.3"
  , "two" : "2.x"
  , "thr" : "3.3.x"
  , "lat" : "latest"
  , "dyl" : "file:../dyl"
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment