2020年10月28日 星期三

Node.js - 支援 ES6 module

Nodejs 支援 ES6 module

    要讓 nodejs 去支援 ES6 module,必須去改寫 package.json。
    開啟一個新的 nodejs project
  • 首先是 node package manager
        npm init
        # 應該會有一堆要填的,若沒任何想法可以改下 npm init -y 自動幫你填完
  • 這時候多一個 package.json 的檔案,裡面有你剛填的資料
        {
          "name": "react",
          "version": "1.0.0",
          "description": "",
          "main": "index.js",
          "scripts": {
            "test": "echo \"Error: no test specified\" && exit 1"
          },
          "repository": {
            "type": "git",
            "url": "git+https://github.com/JunYe1993/React.git"
          },
          "author": "JunYe1993",
          "license": "ISC",
          "bugs": {
            "url": "https://github.com/JunYe1993/React/issues"
          },
          "homepage": "https://github.com/JunYe1993/React#readme"
        }
  • package.json 裡多加一個設定
        ...
          "type": "module"
        }
    若 node 版本低於 v13, 會噴 Warning。
  • node 版本低於 v13
        $ node HelloWorld.js 
        (node:29009) ExperimentalWarning: The ESM module loader is 
        experimental.
        Hello World
參考資料 :

Related Posts:

  • 重新踏入網頁開發 (6) - Express - 2  Express - Route parameters     Route parameters 可以用來擷取 URL 上的 value。 server.js import express from 'express' // 建立 express 這 module var app = express() const port = 8888 // 回傳 { "… Read More
  • 重新踏入網頁開發 (6) - Express - 1  Express     Fast, unopinionated, minimalist web framework for Node.js,這是 Express 的自我介紹。這裡的 unopinionated 之於 opinionated 較為信任開發者,所以你可以擁有很多作法去達到相同的目的,例如像 PERL/PHP。而 opinionated 的 software 則會只提供一個方法去達到目的,例如撰寫維基百… Read More
  • 重新踏入網頁開發 (6) - Express - 3  Express - Middleware     Middleware 是會處理 req, res, next 三個物件並在 routing 時執行之 function,基本上就是 routing 時會處理的 callback function。是 Express 中蠻重要的概念。但用例子來理解比較容易。 import express from 'express' // 建立 express 這… Read More
  • 重新踏入網頁開發 (7) - React  React - Introduction     A JavaScript library for building user interfaces. React 在 MVC 分類上屬於 View,也就是主要用來開發前端。比起直接使用 npx create-react-app,我這裡想紀錄一些較簡單且原始的 React Sample Code。 原本既有的程式碼 <!-- Some HT… Read More
  • 重新踏入網頁開發 (6) - Express - 4  Express - App v.s. Router     Express 通常都是用 app.Method 來做 request 的 routing。而 Express 4.0 加入的 router 則可視為可以掛載的迷你 app。 import express from 'express' var app = express() var router = express.Router… Read More

0 意見:

張貼留言

Popular Posts