nodejs環境を構築する

6296 ワード

ここではlinux環境下のnodejs環境の構築だけを紹介します.
NVM
nvmフルネームNode Version Manageは、shellスクリプトによってnodeバージョンの制御を実現します.
インストール
現在のnvmバージョンは0.3.1で、インストール方式は2種類あります.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
または
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
インストールが完了したら、ホームディレクトリの下に.nvmディレクトリがもう一つあります.隠していますので、ll -aが必要です.
私のログインユーザはrootですので、nvmの位置は/root/.nvm/です.
設定
端末起動時の自動実行設定~/.bashrcファイルに以下のコマンドを追加します.
export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node
source /root/.nvm/nvm.sh
端末を再開けて、nvmを入力して車を返します.
[root@localhost html]# nvm

Node Version Manager

Note:  refers to any version-like string nvm understands. This includes:
  - full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
  - default (built-in) aliases: node, stable, unstable, iojs, system
  - custom aliases you define with `nvm alias foo`

 Any options that produce colorized output should respect the `--no-colors` option.

Usage:
  nvm --help                                Show this message
  nvm --version                             Print out the latest released version of nvm
  nvm install [-s]                 Download and install a , [-s] from source. Uses .nvmrc if available
    --reinstall-packages-from=     When installing, reinstall packages installed in 
    --lts                                   When installing, only select from LTS (long-term support) versions
    --lts=                        When installing, only select from versions for a specific LTS line
  nvm uninstall                    Uninstall a version
  nvm uninstall --lts                       Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.
  nvm uninstall --lts=            Uninstall using automatic alias for provided LTS line, if available.
  nvm use [--silent]               Modify PATH to use . Uses .nvmrc if available
    --lts                                   Uses automatic LTS (long-term support) alias `lts/*`, if available.
    --lts=                        Uses automatic alias for provided LTS line, if available.
  nvm exec [--silent]  [] Run  on . Uses .nvmrc if available
    --lts                                   Uses automatic LTS (long-term support) alias `lts/*`, if available.
    --lts=                        Uses automatic alias for provided LTS line, if available.
  nvm run [--silent]  []     Run `node` on  with  as arguments. Uses .nvmrc if available
    --lts                                   Uses automatic LTS (long-term support) alias `lts/*`, if available.
    --lts=                        Uses automatic alias for provided LTS line, if available.
  nvm current                               Display currently activated version
  nvm ls                                    List installed versions
  nvm ls                           List versions matching a given 
  nvm ls-remote                             List remote versions available for install
    --lts                                   When listing, only show LTS (long-term support) versions
  nvm ls-remote                    List remote versions available for install, matching a given 
    --lts                                   When listing, only show LTS (long-term support) versions
    --lts=                        When listing, only show versions for a specific LTS line
  nvm version                      Resolve the given description to a single local version
  nvm version-remote               Resolve the given description to a single remote version
    --lts                                   When listing, only select from LTS (long-term support) versions
    --lts=                        When listing, only select from versions for a specific LTS line
  nvm deactivate                            Undo effects of `nvm` on current shell
  nvm alias []                     Show all aliases beginning with 
  nvm alias                  Set an alias named  pointing to 
  nvm unalias                         Deletes the alias named 
  nvm reinstall-packages           Reinstall global `npm` packages contained in  to current version
  nvm unload                                Unload `nvm` from shell
  nvm which []                     Display path to installed node version. Uses .nvmrc if available
  nvm cache dir                             Display path to the cache directory for nvm
  nvm cache clear                           Empty cache directory for nvm

Example:
  nvm install v0.10.32                  Install a specific version number
  nvm use 0.10                          Use the latest available 0.10.x release
  nvm run 0.10.32 app.js                Run app.js using node v0.10.32
  nvm exec 0.10.32 node app.js          Run `node app.js` with the PATH pointing to node v0.10.32
  nvm alias default 0.10.32             Set default node version on a shell

Note:
  to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
この時はnvm install 7.7.1でnodeをインストールできます.
インストールが完了したら、npmのソースをタオバオソースに設定します.
npm config set registry https://registry.npm.taobao.org
注意、以上が重要です.スライドのためにnpmパッケージを設置します.
参照リンク
  • https://cnodejs.org/topic/5338c5db7cbade005b023c98
  • http://weizhifeng.net/node-version-management-via-n-and-nvm.html