LoginSignup
6
9

More than 3 years have passed since last update.

npmのインストール手順

Last updated at Posted at 2017-10-10

概要

npmのインストール手順です。

環境

  • CentOS7.2
  • node v6.11.3
  • npm 3.10.10
  • OpenSSL 1.0.2k-fips 26 Jan 2017

依存関係のインストール

$ sudo yum install -y libfontconfig.so.1 fontconfig
$ sudo yum update -y openssl

npmのインストール

$ sudo yum install -y epel-release
$ sudo yum install -y --enablerepo=epel nodejs npm

npm init

Is this ok? (yes) yesでyesと答える以外はとりあえず空白解答でデフォルト設定にする。
これで、package.jsonが生成される。
実行した場所にファイルとかディレクトリが生成されるので、適宜場所を移動してから実行してください。

$ npm init

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (contents)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords: # パッケージを公開した場合、npm searchでの検索キーワードをスペース区切りで設定する
author:
license: (ISC)
About to write to /home/vagrant/contents/package.json:

{
  "name": "contents",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this ok? (yes) yes

インストール例

例として、PhantomJSをインストールする。

$ sudo yum install -y bzip2
$ npm install --save phantomjs

devのみにインストールする場合は

$ npm install --save-dev phantomjs

saveオプションについて

--save は package.json の dependencies に追記される。
--save-dev は package.json の devDependencies に追記される。
--save-optional は package.json の optionalDependencies に追記される。

それぞれの違いだが、package.jsonがモジュールとして外部に公開し、他の人がnpm > installした時に影響する。

他の人が npm install した時に、dependencies に指定したパッケージが全てインストールされる。
参考:http://hblog.glamenv-septzen.info/entry/2015/03/22/233241

PATHを通す

$ npm bin phantomjs
$ echo 'export PATH="$HOME/node_modules/.bin/:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile

phantomjs実行

$ phantomjs -v
$ phantomjs --webdriver=4444
6
9
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
6
9