Node.js - require ( )


Posted by mijouhsieh on 2023-04-09

對 Node.js 來說:

  • 如果沒有提供檔案路徑, Node.js 讀取它時就會預設是模組。
  • 如果有包括路徑,那 Node.js 就知道這是個檔案。

  • 而對於副檔名,Node.js 是這樣去判斷的:
    不寫副檔名的時候,預設 require 會先把它當成 .js 處理
    如果不是 .js 則會當作 .json 處理
    如果不是 .json,就會當作 .node 處理
    如果都沒辦法讀取,程式就會炸掉

// require packages and file used in the project
const exphbs = require('express-handlebars') //模組
const movieList = require('./movies.json') //檔案

const movieList = require('./movies') //也可簡化,最好寫完整
關於 require 模組與檔案的語法,可以參考 Node 的官方文件


#node.js #require( )







Related Posts

[JS Behind The Scene] 從簡單例子了解 hoisting 的運作機制

[JS Behind The Scene] 從簡單例子了解 hoisting 的運作機制

React-[元件篇]-傳遞資料透過props

React-[元件篇]-傳遞資料透過props

學會 HTML & CSS (關於 HTML 的部份)

學會 HTML & CSS (關於 HTML 的部份)


Comments