Node.js – npm 管理


測試環境為 CentOS 8 x86_64

Node.JS 所需套件須透過 npm 來下載,所以必須安裝 npm (Node Package Manager) 套件.

[root@localhost ~]# yum install -y nodejs
[root@localhost ~]# yum install -y npm

檢視一下 Node.JS 與 npm 版本.

[root@localhost ~]# node -v
[root@localhost ~]# npm -v


[root@localhost ~]# mkdir myapi
[root@localhost ~]# cd myapi

程式可能會使用到不同的 npm 模組,可以透過 npm init 建立檔案 package.json 來記錄,就先都使用預設選項.

[root@localhost myapi]# 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 init` for definitive documentation on these fields
and exactly what they do.

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

Press ^C at any time to quit.
package name: (myapi) 
version: (1.0.0) 
git repository: 
license: (ISC)
About to write to /root/myapi/package.json:

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

Is this OK? (yes)

先來安裝要使用的 express 套件(預設是安裝在當下目錄).使用 –save 會記錄用了哪些套件(及其相依性)到檔案 package.json.

[root@localhost myapi]# npm install express --save

剛剛安裝 express 模組是存放在當前目錄下(區域型),如要要放在全域需使用參數 -g (安裝到 /usr/local/lib/node_modules/ 目錄)

[root@localhost myapi]# npm install express -g

其他 npm 操作.

# npm update express 
# npm uninstall express  

檢視一下 package.json 檔案內容.

[root@localhost myapi]# ll
total 24
drwxr-xr-x 52 root root  4096 May 21 20:20 node_modules
-rw-r--r--  1 root root 14281 May 21 20:20 package-lock.json
-rw-r--r--  1 root root   251 May 21 20:20 package.json

可以看到 dependencies 記錄了 express ,有了這個 package.json 檔案之後搬到其他地方就不需要一一安裝所需套件,只需下 npm install 即可.

[root@localhost myapi]# cat package.json 
  "name": "myapi",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "express": "^4.17.1"

試一下 package.json 就不需要一一安裝所需套件.

[root@localhost myapi]# mkdir ../testapi
[root@localhost ~]# cd testapi/
[root@localhost testapi]# cp ../package.json ./
[root@localhost testapi]# npm install
npm WARN deprecated Express@3.0.1: Package unsupported. Please use the express package (all lowercase) instead.
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN root@1.0.0 No description
npm WARN root@1.0.0 No repository field.

added 60 packages from 48 contributors and audited 60 packages in 2.379s
found 0 vulnerabilities
[root@localhost testapi]# cd ../myapi/

以下為使用 Express 模組的 http server 程式,關於程式說明請參考 –

[root@localhost myapi]# vi Node.js
var express = require('express')
var app = express()
app.get('/', function (req, res) {
  res.send('hello world')

伺服器端透過 node.js 執行該程式.

[root@localhost myapi]# node Node.js 

以下是在 Mac OS 執行,透過 curl (文字版的遊覽器),來檢視一下 http server : port 3000 ,的確有看到程式在執行.

Ben@Ben10 ~ % curl    
hello world%   


發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料