量産メモ帳

忘れっぽいのでメモを残しています。浅く適当に書きます。

npmでとりあえず覚えておけば良さそうなコマンド

スポンサーリンク

npmは仕事では殆ど使う機会がなくて、一度使ったコマンドも忘れてしまいそうなので、メモしておきます。

npmコマンド

npm help --- コマンドの一覧を表示する。

まずはhelpですね。

npm help

Usage: npm

where is one of:
access, adduser, audit, bin, bugs, c, cache, ci, cit,
clean-install, clean-install-test, completion, config,
create, ddp, dedupe, deprecate, dist-tag, docs, doctor,
edit, explore, get, help, help-search, hook, i, init,
install, install-ci-test, install-test, it, link, list, ln,
login, logout, ls, org, outdated, owner, pack, ping, prefix,
profile, prune, publish, rb, rebuild, repo, restart, root,
run, run-script, s, se, search, set, shrinkwrap, star,
stars, start, stop, t, team, test, token, tst, un,
uninstall, unpublish, unstar, up, update, v, version, view,
whoami

npm -h quick help on
npm -l display full usage info
npm help search for help on
npm help npm involved overview

Specify configs in the ini-formatted file:
~/.npmrc
or on the command line via: npm --key value
Config info can be viewed via: npm help config

npm@6.9.0 ~/.nodebrew/node/v10.15.1/lib/node_modules/npm

npm install -g npm --- npm自身をアップデートする。

古いのが嫌な人はとりあえず最新化しておけば良いでしょう。

npm install -g npm

~/.nodebrew/node/v10.15.1/bin/npm -> ~/.nodebrew/node/v10.15.1/lib/node_modules/npm/bin/npm-cli.js
~/.nodebrew/node/v10.15.1/bin/npx -> ~/.nodebrew/node/v10.15.1/lib/node_modules/npm/bin/npx-cli.js

  1. npm@6.9.0

updated 6 packages in 5.384s

npm version --- npmのバージョンを知る。

依存パッケージのバージョンも一緒に出してくれます。

npm version

{ npm: '6.9.0',
ares: '1.15.0',
cldr: '33.1',
http_parser: '2.8.0',
icu: '62.1',
modules: '64',
napi: '3',
nghttp2: '1.34.0',
node: '10.15.1',
openssl: '1.1.0j',
tz: '2018e',
unicode: '11.0',
uv: '1.23.2',
v8: '6.8.275.32-node.12',
zlib: '1.2.11' }

npm init --- package.jsonファイルを生成する。

新しくNode.jsプロジェクトを始めたりする時に必要なんじゃないでしょうか。

npm init

npm install --- package-lock.jsonファイルやnode_modulesディレクトリを更新する。

実行したら、元々あったpackage-lock.jsonファイルの中身が書き換わり、依存パッケージのバージョンが上がっていました。
node_modulesディレクトリも作成されて、依存物が配置されていました。
バージョンを上げたくない時は更にオプションが必要なんでしょうかね?

npm install

> fsevents@1.2.4 install ~/github.com/luin/medis/node_modules/fsevents
> node install

[fsevents] Success: "~/github.com/luin/medis/node_modules/fsevents/lib/binding/Release/node-v64-darwin-x64/fse.node" already installed
Pass --update-binary to reinstall or --build-from-source to recompile

> node-sass@4.11.0 install ~/github.com/luin/medis/node_modules/node-sass
> node scripts/install.js

Downloading binary from https://github.com/sass/node-sass/releases/download/v4.11.0/darwin-x64-64_binding.node
Binary saved to ~/github.com/luin/medis/node_modules/node-sass/vendor/darwin-x64-64/binding.node
Caching binary to ~/.npm/node-sass/4.11.0/darwin-x64-64_binding.node

> electron@4.0.1 postinstall ~/github.com/luin/medis/node_modules/electron
> node install.js

Downloading tmp-84692-1-SHASUMS256.txt-4.0.1

> node-sass@4.11.0 postinstall ~/github.com/luin/medis/node_modules/node-sass
> node scripts/build.js

Binary found at ~/github.com/luin/medis/node_modules/node-sass/vendor/darwin-x64-64/binding.node
Testing binary
Binary is fine

> webpack-cli@3.2.0 postinstall ~/github.com/luin/medis/node_modules/webpack-cli
> opencollective postinstall
:(中略)
added 1152 packages from 1911 contributors and audited 9664 packages in 40.646s
found 1 high severity vulnerability
run `npm audit fix` to fix them, or `npm audit` for details

npm audit --- 脆弱性を報告する。

npm install した時、最後に表示されたのですが、こんなコマンドがあるそうです。
fix オプションを付けると、脆弱性を自動修復してくれるみたいです。

npm audit fix

npm WARN react-addons-css-transition-group@15.6.2 requires a peer of react@^15.4.2 but none is installed. You must install peer dependencies yourself.

updated 2 packages in 5.78s
fixed 1 of 1 vulnerability in 9664 scanned packages

npm run --- package.jsonファイルに定義されたスクリプトを実行する。

package.jsonファイルに"scripts"という項目があり、その中に定義されたスクリプトを引数に指定して実行するみたいです。

"scripts": {
"build": "rm -rf dist && webpack",
"watch": "WEBPACK_WATCH=true npm run build",
"start": "electron .",
"pack": "NODE_ENV=production npm run build && node bin/pack.js"
},

例えばこんな感じ。

npm run build

> medis@0.7.1 build ~/github.com/luin/medis
> rm -rf dist && webpack

ℹ 「atl」: Using typescript@3.2.4 from typescript
ℹ 「atl」: Using tsconfig.json from ~/github.com/luin/medis/tsconfig.json
ℹ 「atl」: Checking started in a separate process...
ℹ 「atl」: Time: 1574ms
ℹ 「atl」: Using typescript@3.2.4 from typescript
ℹ 「atl」: Using tsconfig.json from ~/github.com/luin/medis/tsconfig.json
ℹ 「atl」: Checking started in a separate process...
ℹ 「atl」: Time: 1162ms
Hash: af00e699e56bff2d3d09726a7603a6502dd44978
Version: webpack 4.28.3

そう言えば、コンパイルとビルドの違いって何でしょうか?
殆ど同じような意味で使われている印象を受けます。
そもそも昔はビルドという単語は聞いたことがなくて、何でもコンパイルだった気がするのですが、最近はビルドの方が主流な感じがします。

npm start --- アプリケーションを起動する。

npm run buildコンパイルしたものを起動するみたいです。

npm start

> medis@0.7.1 start ~/github.com/luin/medis
> electron .

npm cache clean --- キャッシュをクリアする。

どこにあるのか知りませんが、npmのキャッシュをクリアするそうです。
--force オプションを付けると、強制クリアだそうです。

npm cache clean --force

関連記事

rms-099.hatenablog.jp