Kai Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 虎年行大運 ~ NodeJS 學習 - NVM、Yarn、Node 環境安裝 ## 【前言】 最近因緣際會碰了 NodeJS 這一塊完全沒有經驗的領域,趁機學習如何建置NVM、NPM、Node 環境,並嘗試用知名的 Express 框架建立 API。 這邊連動的 DB 是 Firebase,一款 Google 非常知名的雲端資料倉儲服務,其實也已經支援非常多功能了,因此就瞬邊碰碰當作經驗。 ## 【NVM 安裝】 **NVM** 全名 **Node Version Manager** 即為 Node 版本控制,這會與將要使用哪一種版本的 NodeJS 進行開發完全相關,也因為 NPM 逐漸被 Yarn 取代了,因此這裡不會介紹如何安裝 NPM 的部分。 [NVM Github](https://github.com/nvm-sh/nvm) 請到上述連結進行相關環境的 NVM 檔案下載,Kai 因為是用 Windows 環境進行開發,因此另外提供適用於 Windows 的連結在此 [NVM for Windows](https://github.com/coreybutler/nvm-windows/releases) 安裝好後,可以在終端機透過以下指令確認相關訊息 ``` nvm ``` ![](https://i.imgur.com/LMmYLxi.png) > 圖示就是 NVM 環境安裝好的 > 若無法取得相同結果,請檢查 > 1. 環境參數是否有正確設置 > 2. 嘗試用系統管理者權限開啟終端機 > 3. 又或者是安裝在非 C 槽環境的話,記得去 /nvm/settings.txt 將內容改成以下 > ``` > root: D:nvm > path: D:\nvm\nodejs > ``` > 這樣環境指令才能正確抓到哦! 可以透過以下指令進行 NVM 的控制 | Command | Info | | ------- | ---- | | nvm ls-remote | 會列出目前可以下載的 NodeJS 版本 | | nvm install [version] | 下載安裝 NodeJS,若無輸入 version 則會下載最新的版本 | | nvm ls | 列出本地端安裝過的 NodeJS 版本 | | nvm use [version] | 設定使用的 NodeJS 版本 | | nvm current | 顯示目前使用的 NodeJS 版本 | ## 【Yarn】 裝好 NodeJS 的時候,其實會連同 NPM 一起裝好,但 Kai 不打算使用 NPM 協助處理套件的版本管理,因此在開好任何一個 NodeJS 專案後,在專案本地端的終端機使用以下指令進行安裝 ``` npm install yarn [--g] ``` > 要不要使用 --g 裝成 Global 就看各位選擇了 未來所有的套件都可以使用 Yarn 進行處理,這時候原先採用 NPM 的檔案 **package-lock.json** 就可以刪掉改成只使用 **yarn.lock** 了 那到底 Yarn 與 NPM 差異在哪裡? 為什麼要改用 Yarn 而非沿用 NPM 就好? Yarn 是一個比 NPM 更專門的 Package 管理服務,避免了 NPM 管理會日益肥大的問題,並有著更安全的處理模式,避免危險程式碼侵入,同時也提供更加簡單易懂的指令集。 以下分享兩者常用的指令差異 |Yarn|NPM|Info| |----|----|----| | yarn install | npm install | 下載所有依賴,若無輸入則會將 package.json 全數下載 | | yarn add [package] | npm install --save [package] | 下載特定依賴,並儲存在 package.json 中的 dependecies | | yarn add [package] --dev | npm install --save-dev [package] | 下載特定依賴,並儲存在 package.json 中的 devDependecies | | yarn global add [package] | npm install --global [package] | 下載特定依賴,並儲存在電腦全域 | | | npm uninstall | 移除所有套件,Yarn 不支援移除所有套件功能 | | yarn remove [package] | npm uninstall --save [package] | 移除特定套件 | | yarn upgrade | rm -rf node_modules && npm install | 更新 node_modules | ## 【建立 Node Express 專案】 在建立 Express 專案的時候,可能還是要先透過 NPM 進行。 ``` // 下載 Express 套件 npm install express-generator // 建置專案 express --view=ejs [Project Name] ``` 創建後 Express 的檔案結構如下,其中 index.js 和 users.js 是預設的兩個 API 服務內容,讓新手可以快速理解如何架設 API 服務。 ``` ├── app.js ├── bin │ └── www ├── package.json ├── public │ ├── images │ ├── javascripts │ └── stylesheets │ └── style.css ├── routes │ ├── index.js │ └── users.js └── views ├── error.ejs └── index.ejs ``` 此時可以透過 Yarn 進行 NodeJS API 服務的啟動 ``` yarn start ``` 這個指令設立在 package.json 中,會建議加上以下 dev-start 這一條 ``` "scripts": { "start": "node ./bin/www", "dev-start": "set DEBUG=[Project Name]:* & yarn start" } ``` 這樣未來就可以使用以下指令啟動開發者模式測試了 ``` yarn dev-start ``` ## 【結語】 是第一次碰 NodeJS 系列的開發,非常新鮮,後續有學到不同的東西再寫上來分享。 首頁 [Kai 個人技術 Hackmd](/2G-RoB0QTrKzkftH2uLueA) ###### tags: `NodeJS`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully