Node.js Clone下來的專案怎麼Run?
當未來接手你專案的工程師沒碰過Node.js時,可能連專案怎麼Run起來都不清楚(?),又懶得每次都要重頭再教導一遍…
這篇就是為此而存在的懶人教學。
第一步、準備開發環境!
沒Node.js怎麼開發專案捏!?
雖然VSCode也還是能開專案改Code啦,但總要測試Run專案吧>"<
關於這部分我有再另一篇寫過,請看這邊:
Windows Node.js開發環境建置
第二步、Clone專案!
快樂登入GitLab或GitHub,找到要抓下來的專案,
然後$git clone <url>
。
你會得到一個只有git、沒有任何套件,很乾淨的專案(下圖)。
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
小備註:
一般來說設定檔.env不會上傳到Git上,那後人要怎麼知道設定檔要設定那些東西呢?
.env.example就是用來提示你設定檔要放哪些設定的!
複製一份改檔名.env,將資料照欄位填寫上去就可以囉
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
第三步、Install套件!
Git上的專案通常不會連同套件都放上去,太佔空間也浪費時間。
因此package.json和package-lock.json會記錄目前該專案的所有套件與其版本,只要下指令就會針對文件中的資訊,下載安裝對應的套件。
會得到一個node_modules,存放所有的套件
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
第四步、Run專案!!
在下指令之前,首先要到package.json檢查scripts設定的條件,預設通常都長這樣:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
有些開發者會為了測試方便,在這邊額外寫測試用的設定。
我們可以看到圖中有一個"start"是執行啟動專案"node ./bin/www"這動作的,只要下指令$npm run <start>
,就會照start設定的動作去run專案。
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
看到這畫面,沒有噴Error就代表有成功在Run囉~