当前位置: 移动技术网 > IT编程>脚本编程>NodeJs > Nodejs中的require函数的具体使用方法

Nodejs中的require函数的具体使用方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

说明

本文参考node官网文档版本为v11.12.0。

本文主要分析了nodejs中require导入json和js文件时得到的结果,同时简单涉及到了nodejs中模块导出module.exports和exports的用法。

引言

在阅读webpack源码的过程当中,见到如下一行代码:

const version = require("../package.json").version

故引申出对nodejs中require的学习。

require介绍

在node.js的文档中,require的相关文档是在modules目录下,属于nodejs模块化系统的一部分。

require是一个函数。通过typeof或者object.prototype.tostring.call()可以验证这个结论:

console.log(require) // 输出:function
console.log(object.prototype.tostring.call(require) // 输出:[object function]

通过直接打印require,可以发现在require函数下还挂载着若干个静态属性,这些静态属性也可以在nodejs的官方文档中直接找到相关的说明:

{ [function: require]
 resolve: { [function: resolve] paths: [function: paths] },
 main:
  module {
   id: '.',
   exports: {},
   parent: null,
   filename: '/users/bjhl/documents/webpacksource/index.js',
   loaded: false,
   children: [],
   paths:
   [ '/users/bjhl/documents/webpacksource/node_modules',
    '/users/bjhl/documents/node_modules',
    '/users/bjhl/node_modules',
    '/users/node_modules',
    '/node_modules' ] },
 extensions:
  [object: null prototype] { '.js': [function], '.json': [function], '.node': [function] },
 cache:
  [object: null prototype] {
   '/users/bjhl/documents/webpacksource/index.js':
   module {
    id: '.',
    exports: {},
    parent: null,
    filename: '/users/bjhl/documents/webpacksource/index.js',
    loaded: false,
    children: [],
    paths: [array] } } }

require函数静态属性

这里之后再详细补充。

require使用

在官网文档中可以看到如下关于require的说明:

require(id)#  added in: v0.1.13  id module name or path  returns: exported module content  used to import modules, json, and local files. modules can be imported from node_modules. local modules and json files can be imported using a relative path (e.g. ./, ./foo, ./bar/baz, ../foo) that will be resolved against the directory named by __dirname (if defined) or the current working directory.

同时还给出了三种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');

从以上文档中可以得出以下信息:

  1. require接受一个参数,形参名为id,类型是string。
  2. require函数return的是模块到处的内容,类型是任意。
  3. require函数可以导入模块、json文件、本地文件。模块可以通过一个相对路径从node_modules、本地模块、json文件中导出,该路径将针对__dirname变量(如果已定义)或者当前工作目录。

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的名字 **

通过以上执行结果可以得出结论:

  1. require某个js文件时,如果未通过exports或者module.exports指定导出内容,则require返回的结果是一个空对象;反之可以通过module.export或者给exports属性赋值来导出指定内容。
  2. require某个js文件时,该文件会立即sync执行。

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文件是相同的。

在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.

在以上说明中可以得出以下结论:

  1. main字段是一个模块id,是程序的主入口。
  2. 当使用require("xxx")的时候,导入的是main字段对应的js文件里的module.exports。

所以require导入模块的时候,是运行的对应模块package.json中main字段指定的文件。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网