Docs Home / mongosh / Write Scripts

Differences Between require() and load()require()load()的区别

The require() and load() methods include files and modules in your scripts for added functionality. However, require() and load() differ in their behaviors and availability.require()load()方法包括脚本中的文件和模块,以增加功能。然而,require()load()在行为和可用性方面有所不同。

Types of Scripts in mongoshmongosh中的脚本类型

You can use the following types of scripts with mongosh:您可以在mongosh中使用以下类型的脚本:

  • mongosh scripts, which can be any of the following:脚本,可以是以下任何一种:

  • Node.js scripts, which are any scripts loaded with require(), including npm packages. These scripts are always files.Node.js脚本,它是任何用require()加载的脚本,包括npm包。这些脚本始终是文件。

Availability of require() and load()require()load()的可用性

The require() and load() methods differ in availability depending on the type of script you are using.require()load()方法的可用性因您使用的脚本类型而异。

  • In mongosh scripts, both require() and load() are available.mongosh脚本中,require()load()都可用。
  • In Node.js scripts, only require() is available.在Node.js脚本中,只有require()可用。

File Paths for require() and load()require()load()的文件路径

The type of script determines how you specify file paths with require() or load().脚本的类型决定了如何使用require()load()指定文件路径。

  • In mongosh scripts:mongosh中:

    • require() uses the standard Node.js module resolution algorithm, starting from the current working directory of the shell.require()使用标准的Node.js模块解析算法,从shell的当前工作目录开始。
    • load() takes either:采取以下任一方式:

      • An absolute path, or绝对路径,或
      • A relative path. When using a relative path, the path is always interpreted as relative to the current working directory of the shell.一条相对的路径。使用相对路径时,该路径始终被解释为相对于shell的当前工作目录。
  • In Node.js scripts, require() uses the standard Node.js module resolution algorithm, starting from the file where require() was called.在Node.js脚本中,require()使用标准的Node.js模块解析算法,从调用require()的文件开始。

Load External Code in a mongosh Scriptmongosh脚本中加载外部代码

You can load external code in a mongosh script file, such as an npm package or a separate mongosh script.您可以在mongosh脚本文件中加载外部代码,例如npm包或单独的mongosh剧本。

  • To load a mongosh script from another mongosh script, use the __dirname environment variable. The __dirname environment variable returns the absolute path of the directory containing the file being executed.要从另一个mongosh脚本加载mongosh脚本,请使用__dirname环境变量。__dirname环境变量返回包含正在执行的文件的目录的绝对路径。

    Example示例

    To load a mongosh script named test-suite.js from another mongosh script, add the following line to your script:要从另一个mongosh脚本加载名为test-suite.jsmongosh剧本,请在脚本中添加以下行:

    load(__dirname + '/test-suite.js')

    Using the _dirname variable to specify an absolute path ensures that the separate script you are loading is not affected by external factors such as where mongosh started.使用_dirname变量指定绝对路径可确保您正在加载的单独脚本不受外部因素的影响,例如mongosh的起始位置。

  • To load a Node.js script from a mongosh script, use the require() method.要从mongosh脚本加载Node.js脚本,请使用require()方法。

    Example示例

    To load the date-fns module from a mongosh script called test-suite2.js, add the following lines to your script:要从名为test-suite2.jsmongosh脚本加载date-fns模块,请在脚本中添加以下行:

    const localRequire = require('date-fns').createRequire(__filename);
    const fileExports = localRequire('./test-suite2.js'); }

require() Packaging Considerationsrequire()包装注意事项

There are two packaging standards for Node.js modules.Node.js模块有两种打包标准。

Packaging Standard包装标准Works with require()使用require()
CommonJS (CJS)Yes
ECMAScript Module (ES Module)No

You cannot require() an ES module in mongosh. If you want to use functionality from an ES module, check to see if there is a CommonJS version that you can use instead. For more information, see:你不能在mongoshrequire()一个ES模块。如果你想使用ES模块的功能,请检查是否有可以使用的CommonJS版本。有关更多信息,请参阅:

Access to the mongosh API访问mongosh API

  • mongosh scripts can use the mongosh API.脚本可以使用mongosh API。
  • Node.js scripts do not have access to the mongosh API.Node.js脚本无法访问mongosh API。

For example, the db global variable (used to display the current database) is available inside of mongosh scripts. It is not available inside of Node.js scripts.例如,db全局变量(用于显示当前数据库)在mongosh脚本中可用。它在Node.js脚本中不可用。

Important

mongosh scripts and Node.js scripts run in different contexts. mongosh脚本和Node.js脚本在不同的上下文中运行。They may exhibit different behaviors when the same command is run in each type of script, such as returning different data types. 当在每种类型的脚本中运行相同的命令时,它们可能会表现出不同的行为,例如返回不同的数据类型。Therefore, you may observe unexpected results if you run mongosh code inside of a Node.js script.因此,如果在Node.js脚本中运行mongosh代码,可能会观察到意外的结果。

Generally, you should not keep mongosh-specific code inside Node.js scripts.一般来说,你不应该在Node.js脚本中保留特定于mongosh的代码。