Contents
  1. 1. Setting up nodejs environment on Ubuntu
    1. 1.1. install nodejs
    2. 1.2. install npm
  2. 2. npm - install packages
    1. 2.1. package.json
    2. 2.2. some useful npm command

Nodejs紅了好一陣子了,對於它我還是不太了解,可是手邊蠻多日常工具如hexo都跟nodejs和npm密切相關,故用此篇來紀錄探索的過程。

Setting up nodejs environment on Ubuntu

install nodejs

Nodejs已經釋出v5了(source),Ubuntu可用以下方式安裝最新版nodejs

  • curl -s代表silent mode,不會印出任何訊息包括下載進度
  • curl -L :
    (HTTP/HTTPS) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place.
  • sudo -E –preserve-env:
    Indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the user does not have permission to preserve the environment.
  • bash - :mysterious ‘-‘
1
2
3
4
5
6
7
# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_5.x | bash -
apt-get install -y nodejs

install npm

npm是node的套件管理工具,安裝node會一併安裝npm,因為npm的更新速度較node快,故可用sudo npm install npm -g來更新npm。

npm - install packages

npm安裝套件分為locally跟globally,若只是當前開發的程式require其他node package,則npm install <package_name>會執行install locally,在當前目錄下產生node_modules並把你要求的package放入;-g --global則是全域安裝,通常是把package拿來當執行檔用。

npm install <package_name> --save,會下載安裝package,並將package name和版本紀錄到package.json

package.json

package.json是一個用來紀錄你project開發時所depend的package,其他開法者只要clone你的project並執行npm install就會安裝好所需的package,project剛開始也可用npm init產生對應的package.json。

some useful npm command

  • npm search <package name> : 搜尋套件
  • npm ls -g :列出全域安裝的套件,加上-l能列出更詳細的資訊
  • npm update -g : 更新全域安裝的套件
  • cd some/project && npm update : 更新project用到的套件
Contents
  1. 1. Setting up nodejs environment on Ubuntu
    1. 1.1. install nodejs
    2. 1.2. install npm
  2. 2. npm - install packages
    1. 2.1. package.json
    2. 2.2. some useful npm command