華文網

npm 配置和安裝 express4.X 遇到的問題及解決

前言:懶得看前面兩篇介紹的也可以從本節直接參考,但建議最好瞭解下,因為 4.X 的express 已經把命令列工具分離出來 (連結https://github.com/expressjs/generator)

環境:win7

安裝完成後不要立馬使用 nodeJS 或 npm命令。

2. 使用命令列改變 npm 啟動 和 緩存 位置(可以不更改,若不更改則會佔用 C盤 空間):

npm一般都會配置成全域模組,這樣以防以後使用不方便,一般情況下我們會在NodeJs安裝目錄下下建立"node_global"及"node_cache"兩個資料夾,如下圖:

接著:使用命令列改變npm啟動和緩存位置(此命令以管理員方式或使用者方式都可):

1 npm config set prefix "F:\Program Files\nodejs\node_global" # 開機檔案位置: 使用 npm 命令安裝的 全域模組 檔存儲位置 2 npm config set cache "F:\Program Files\nodejs\node_cache" # 緩存位置:快取檔案位置

再接著:更改 npm 啟動(即 全域模組)的環境變數,步驟如下:

1 更改位置: 電腦->屬性->高級系統設置->環境變數->Path 2 3 原來的(nodeJS,npm): 4 C:\Users\UserName\AppData\Roaming\npm # (即 預設的 全域模組 檔存儲位置,根據以上 npm啟動存儲位置的更改,
該部分要變更) 5 G:\Program Files\nodejs #(即:nodeJS 安裝路徑,該路徑不變) 6 7 改為: 8 G:\Program Files\node_global # 改為:自訂的 全域模組 存儲位址 9 G:\Program Files\nodejs # 不用變更,安裝時自動已識別

3. 安使用 npm 命令安裝模組,鑒於上篇提到的建立專案時已遇到的問題:

查了許多文檔:

主要原因:The issue is caused by recent changes in express-generator structure: express.js has been renamed to express-cli.js

導致結果:無法使用某些 IDE(eg: webstorm,...) 來創建 nodeJS 項目(如本人)

查閱的主要解決辦法:(1)Try running the command prompt as administrator. (我未實踐成功)

(2)To Fix :Run command prompt as administrator, and create the folder. (已實踐)

1 npm install -g express-generator 2 express project_name 3 cd project_name, npm install 4 in webstorm, File | open, choose project_name folder

本人主要使用 (2) 解決了以上問題,以下說明幾個模組的安裝:

聲明:(a) 按前面的建議,

最好以管理員方式建立使用以下命令。

(b) 對於 3.X,4.X 版本來說,主要是 express 模組的安裝,對於 4.X 版本來說,要注意首先需 express-generator 的安裝(必須全域安裝),再才能安裝express(可全域也可當前安裝) 及其他模組,具體如下:

$ npm install -g express-generator # express-generator 必須全域安裝, 安裝指定版本: npm install -g express-generator@3.5.0$ npm install express --save # express 沒必要全域安裝,但最好 安裝的同時,進行保存,即 加上 --save. 此處 express 最好和 上面的 express-generator 版本一致
$ npm install *** --save # 其他模組的安裝類似 express 模組,也可不全域安裝,但最好 安裝的同時,進行保存,
即 加上 --save.
此外:要卸載模組使用: npm uninstall express

4. 創建 nodeJS 項目:

首先要安裝好 nodeJS 使用 框架的相關模組後,用 IDE 創建項目,出現以上問題,查看原因後,使用 命令列 方式創建專案,再用 IDE 打開即可。創建過程如下:

(1)先進入你想創建專案的目錄,eg:F:\workspace

(2 ) 創建專案,創建一個範本引擎為jade,應用名叫 mytest的工程,命令如下:

$ express --view=jade mytest參數:jade :是創建的專案範本 # 專案範本共有多種 mytest:是項目名稱

回車後,出現如下:

1 create : mytest 2 create : mytest/package.json 3 create : mytest/app.js 4 create : mytest/public 5 create : mytest/public/images 6 create : mytest/routes 7 create : mytest/routes/index.js 8 create : mytest/routes/users.js 9 create : mytest/public/javascripts 10 create : mytest/public/stylesheets 11 create : mytest/public/stylesheets/style.css 12 create : mytest/views 13 create : mytest/views/index.jade 14 create : mytest/views/layout.jade 15 create : mytest/views/error.jade 16 create : mytest/bin 17 create : mytest/bin/www 18 install dependencies: 19 $ cd mytest && npm install 20 run the app: 21 $ DEBUG=mytest:* npm start

說明--專案範本(常見的有): ejs,hbs,pug,jade,haml,....等。

可使用 express 説明命令查看已有的範本等,如下:

$ express -h Usage: express [options] [dir] Options: -h, --help output usage information --version output the version number -e, --ejs add ejs engine support --hbs add handlebars engine support --pug add pug engine support -H, --hogan add hogan.js engine support -v, --view add view support (ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade) -c, --css add stylesheet support (less|stylus|compass|sass) (defaults to plain css) --git add .gitignore -f, --force force on non-empty directory

(3)按照提示,進入到 mytest 目錄,然後安裝依賴:

$ cd mytest && npm install # 該句實際包含兩條命令:進入項目 和 安裝依賴包

(4)安裝完成後,啟動我們的應用:

$ npm start # 這裡需要注意 express 4.x 無法以 node app.js 為啟動方式,而是用指令 npm start 作為啟動

訪問 http://localhost:3000/出現熟悉的 Welcome to Express,證明安裝成功,如下圖:

該項目創建到此就真的完成了!!

此後,就可以用 webstorm 等 IDE 打開該項目,並可以編寫自己的代碼了。

該項目創建到此就真的完成了!!

此後,就可以用 webstorm 等 IDE 打開該項目,並可以編寫自己的代碼了。