A child class of Module, which loads modules from the mock object if specified, otherwise will load unmocked modules in its own mocked environment, so that they will load the mocked modules as well.

Hierarchy (view full)

Constructors

Properties

children: Module[]
exports: any
filename: string
id: string
isPreloading: boolean

true if the module is running during the Node.js preload

loaded: boolean
parent: undefined | null | Module

since v14.6.0 Please use require.main and module.children instead.

path: string

v11.14.0

The directory name of the module. This is usually the same as the path.dirname() of the module.id.

paths: string[]
Module: typeof Module
SourceMap: typeof SourceMap
_cache: {
    [k: string]: CorrectModule;
}
builtinModules: string[]
findSourceMap: ((path: string, error?: Error) => SourceMap)

Type declaration

    • (path, error?): SourceMap
    • path is the resolved path for the file for which a corresponding source map should be fetched.

      Parameters

      • path: string
      • Optionalerror: Error

      Returns SourceMap

      Returns module.SourceMap if a source map is found, undefined otherwise.

      v13.7.0, v12.17.0

syncBuiltinESMExports: (() => void)

Type declaration

    • (): void
    • The module.syncBuiltinESMExports() method updates all the live bindings for builtin ES Modules to match the properties of the CommonJS exports. It does not add or remove exported names from the ES Modules.

      const fs = require('node:fs');
      const assert = require('node:assert');
      const { syncBuiltinESMExports } = require('node:module');

      fs.readFile = newAPI;

      delete fs.readFileSync;

      function newAPI() {
      // ...
      }

      fs.newAPI = newAPI;

      syncBuiltinESMExports();

      import('node:fs').then((esmFS) => {
      // It syncs the existing readFile property with the new value
      assert.strictEqual(esmFS.readFile, newAPI);
      // readFileSync has been deleted from the required fs
      assert.strictEqual('readFileSync' in fs, false);
      // syncBuiltinESMExports() does not remove readFileSync from esmFS
      assert.strictEqual('readFileSync' in esmFS, true);
      // syncBuiltinESMExports() does not add names
      assert.strictEqual(esmFS.newAPI, undefined);
      });

      Returns void

      v12.12.0

Methods

  • Override require() to perform our mocked require

    Parameters

    • id: string

    Returns any

  • Parameters

    • path: string | URL

    Returns NodeRequire

  • Parameters

    • moduleName: string

    Returns boolean

  • Type Parameters

    • Data = any

    Parameters

    • specifier: string | URL
    • OptionalparentURL: string | URL
    • Optionaloptions: RegisterOptions<Data>

    Returns void

  • Type Parameters

    • Data = any

    Parameters

    • specifier: string | URL
    • Optionaloptions: RegisterOptions<Data>

    Returns void

  • Returns void

  • Parameters

    • code: string

    Returns string