同時還給出了三種require的使用方法:
// Importing a local module: const myLocalModule = require('./path/myLocalModule'); // Importing a JSON file: const jsonData = require('./path/filename.json'); // Importing a module from node_modules or Node.js built-in module: const crypto = require('crypto');
從以上文檔中可以得出以下信息:
require實踐
在這里將分類討論require的實踐結論。
require導入JSON
JSON 是一種語法,用來序列化對象、數組、數值、字符串、布爾值和 null 。
在文章的開頭就提到了通過require("./package.json")文件來讀取package.json文件中的version屬性。這里將嘗試導入info.json文件并查看相關信息。
文件結構目錄如下:
. ├── index.js └── info.json
將info.json文件的內容修改為:
{ "name": "myInfo", "hasFriend": true, "salary": null, "version": "v1.0.0", "author": { "nickname": "Hello Kitty", "age": 20, "friends": [ { "nickname": "snowy", "age": 999 } ] } }
在info.json當中,包含了字符串、布爾值、null、數字、對象和數組。
將index.js的內容修改如下并在當前terminal運行命令 node index.js ,得到如下結果:
const info = require("./info.json") console.log(Object.prototype.toString.call(info)) // [object Object] console.log(info.version) // v1.0.0 console.log(info.hasFriend) // true console.log(info.salary) // null console.log(info.author.nickname) // Hello Kitty console.log(info.author.friends) // [ { nickname: 'snowy', age: 999 } ]
可以看到,require導入一個JSON文件的時候,返回了一個對象,Nodejs可以直接訪問這個對象里的所有屬性,包括String、Boolean、Number、Null、Object、Array。個人猜測這里可能用到了類似于JSON.parse()的方法。
通過這個結論也得出了一種思路,即通過require方法傳入JSON文件來讀取某些值,如在文章開頭中,webpack通過讀取package.json文件獲取到了version值。
require導入本地js文件
文件結構目錄如下:
. ├── index.js ├── module_a.js └── module_b.js
index.js文件中,分別按順序導入了module_a和module_b并賦值,然后將這兩個變量打印,內容如下:
console.log("*** index.js開始執行 ***") const module_a = require("./module_a") const module_b = require("./module_b") console.log(module_a, "*** 打印module_a ***") console.log(module_b, "*** 打印module_b ***") console.log("*** index.js結束執行 ***")
module_a文件中,未指定module.exports或者exports,但是添加了一個異步執行語句setTimeout,內容如下:
console.log("** module_a開始執行 **") let name = "I'm module_a" setTimeout(() => { console.log(name, "** setTimeout打印a的名字 **") }, 0) console.log("** module_a結束執行 **")
module_b文件中,指定了module.exports(也可以換成exports.name,但是不能直接使用exports等于某個對象,因為exports和module.exports其實是指向了一個地址,引用了相同的對象,如果使用exports等于其他的引用類型,則不再指向module.exports,無法改變module.exports里的內容),內容如下:
console.log("** module_b開始執行 **") let name = "I'm module_b" console.log(name, "** 打印b的名字 **") module.exports = { name } console.log("** module_b結束執行 **")
在當前目錄terminal下運行 node index.js 運行得到如下輸出:
*** index.js開始執行 ***
** module_a開始執行 **
** module_a結束執行 **
** module_b開始執行 **
I am module_b ** 打印b的名字 **
** module_b結束執行 **
{} '*** 打印module_a ***'
{ name: 'I am module_b' } '*** 打印module_b ***'
*** index.js結束執行 ***
I am module_a ** setTimeout打印a的名字 **
通過以上執行結果可以得出結論:
require導入模塊
我們先選擇一個npm包——cors。 進入文件夾,運行一下命令:
npm init -y // 初始化 echo -e "let cors = require(\"cors\")\nconsole.log(cors)" > index.js // 生成index.js文件 npm install cors --save // 安裝cors包
文件結構如下(...處省略了其他的模塊):
. ├── index.js ├── node_modules │ ├── cors │ │ ├── CONTRIBUTING.md │ │ ├── HISTORY.md │ │ ├── LICENSE │ │ ├── README.md │ │ ├── lib │ │ │ └── index.js │ │ └── package.json │ │ ... ├── package-lock.json └── package.json
index.js中的內容如下:
let cors = require("cors") console.log(cors)
運行 node index.js ,得出以下結果:
[Function: middlewareWrapper]
找到node_modules下的cors模塊文件夾,觀察cros模塊中的package.json文件,找到main字段: "main": "./lib/index.js" ,找到main字段指向的文件,發現這是一個IIFE,在IIFE中的代碼中添加,console.log("hello cors"),模擬代碼結構如下:
(function () { 'use strict'; console.log("hello cors"); // 這是手動添加的代碼 ... function middlewareWrapper(o) { ... } module.exports = middlewareWrapper; })()
再次運行 node index.js ,得出以下結果:
hello cors
[Function: middlewareWrapper]
為什么會打印出 hello cors 呢?因為require模塊的時候,引入的是該模塊package.json文件中main字段指向的文件。而這個js文件會自動執行,跟require引用本地js文件是相同的。
packjson文檔
在npm的官方網站中可以找到關于package.json中的main字段定義。
main The main field is a module ID that is the primary entry point to your program. That is, if your package is named foo, and a user installs it, and then does require("foo"), then your main module's exports object will be returned. This should be a module ID relative to the root of your package folder For most modules, it makes the most sense to have a main script and often not much else.
在以上說明中可以得出以下結論:
所以require導入模塊的時候,是運行的對應模塊package.json中main字段指定的文件。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com