1. 程式人生 > >nodejs備忘總結(一) -- node和express安裝與配置,新建簡單專案(附安裝配置過程中遇到問題的解決方法)

nodejs備忘總結(一) -- node和express安裝與配置,新建簡單專案(附安裝配置過程中遇到問題的解決方法)

  • 安裝node

本文以安裝node_v8.9.0為例(win10環境),下載node-v8.9.0-x64.msi外掛

下載後,安裝,安裝目錄預設為C:\Program Files\nodejs

配置環境變數,系統變數->path,新增“C:\Program Files\nodejs\”

 

執行cmd,輸入node -v

C:\Windows\system32>node -v
v8.9.0

 

  • 安裝express

找到node安裝目錄C:\Program Files\nodejs,命令列執行

C:\Program Files\nodejs> npm install express

執行結果

npm WARN registry Unexpected warning for https://registry.npmjs.org/: Miscellaneous Warning ENOENT: request to https://registry.npmjs.org/express failed, reason: getaddrinfo ENOENT registry.npmjs.org:443

npm WARN registry Using stale package data from https://registry.npmjs.org/ due to a request error during revalidation.

npm WARN enoent ENOENT: no such file or directory, open 'C:\Program Files\nodejs\package.json'e_modules\libnpx

npm WARN nodejs No description

npm WARN nodejs No repository field.

npm WARN nodejs No README data

npm WARN nodejs No license field.

 

npm ERR! path C:\Program Files\nodejs\node_modules\npm\node_modules\bytes

npm ERR! code ENOENT

npm ERR! errno -4058

npm ERR! syscall rename

npm ERR! enoent ENOENT: no such file or directory, rename 'C:\Program Files\nodejs\node_modules\npm\node_modules\bytes' -> 'C:\Program Files\nodejs\node_modules\bytes'

npm ERR! enoent This is related to npm not being able to find a file.

npm ERR! enoent

 
npm ERR! A complete log of this run can be found in
: npm ERR!

npm install express失敗可能導致nodejs/node_modules資料夾被清空,導致後續的npm install操作會失敗,通過重新安裝node-v8.9.0-x64.msi修復即可。

 

上面操作失敗是因為找不到相關的檔案導致,需要更改到npm資料夾目錄下去操作

> cd C:\Program Files\nodejs\node_modules\npm
> npm install express

還是報錯,查找了下原因,當前電腦使用者沒有修改下面這個資料夾的許可權(C:\Program Files\nodejs\node_modules\npm\node_modules),

install的express是安裝在這個目錄下,網上提供的解決方法是用管理員的身份執行命令,

(win10環境)筆者是直接右擊資料夾,屬性->安全,組或使用者那裡選擇當前電腦使用者,

然後許可權把“修改”設定為允許。接著重新執行npm install express就可以正常安裝

 執行結果

npm notice created a lockfile as package-lock.json. You should commit this file.
+ [email protected]4.16.3
added 47 packages in 125.339s

 

 安裝express-generator

> npm install express-generator

執行結果

+ [email protected]4.16.0
added 7 packages in 6.866s

 

安裝完成後,檢視版本號

C:\Windows\system32>express -V
'express' 不是內部或外部命令,也不是可執行的程式
或批處理檔案。

 

在網上查了一部分資料,解決這個問題的關鍵點在於找到express.cmd被安裝的位置

不同版本安裝的位置可能有所不同,比如有的會在(C:\Program Files\nodejs\node_modules\.bin),

有的是被直接安裝在node_modules資料夾下;

本人電腦是被安裝位置到C:\Program Files\nodejs\node_modules\npm\node_modules\.bin

 

 

配置express環境變數

系統變數->path,新增C:\Program Files\nodejs\node_modules\npm\node_modules\.bin

配置完成後,執行查詢版本命令

C:\Windows\system32>express -V

 

語法錯誤,修改成express --version

C:\Windows\system32>express --version
4.16.0

到這裡,node和express安裝配置完成

 

  • 新建專案

開啟要新建專案的檔案目錄,新建專案myApp

> cd node_workspace/debugDemo

> express myApp

執行結果:

 warning: the default view engine will not be jade in future releases

  warning: use `--view=jade' or `--help' for additional options

   create : myApp\

   create : myApp\public\

   create : myApp\public\javascripts\

   create : myApp\public\images\

   create : myApp\public\stylesheets\

   create : myApp\public\stylesheets\style.css

   create : myApp\routes\

   create : myApp\routes\index.js

   create : myApp\routes\users.js

   create : myApp\views\

   create : myApp\views\error.jade

   create : myApp\views\index.jade

   create : myApp\views\layout.jade

   create : myApp\app.js

   create : myApp\package.json

   create : myApp\bin\

   create : myApp\bin\www

   change directory:

     > cd myApp

   install dependencies:

     > npm install

   run the app:

     > SET DEBUG=myapp:* & npm start

 

在檔案目錄下可以找到新建的myApp專案

在myApp目錄下新建hello.js

var http = require("http");

http.createServer(function(request, response) {  

    response.writeHead(200, {"Content-Type": "text/plain"});  

    response.write("Hello World");  

    response.end();

}).listen(8888);

console.log("nodejs start listen 8888 port!");

然後在專案目錄下,執行node hello.js

E:\node_workspace\debugDemo\myApp>node hello.js

nodejs start listen 8888 port!

在瀏覽器輸入http://127.0.0.1:8888/

頁面顯示 Hello World

--本篇到此結束--