Skip to content

Commit 4d439bb

Browse files
author
Dave Cuthbert
authored
DOCSP-32118 require modules (#285)
* DOCSP-32118 require modules * DOCSP-32118 require modules * Self review * Self review * Review feedback * Force build * Review feedback * Review feedback * Review feedback * Review feedback * Self review * Self review * review feedback * review feedback
1 parent c12d0d1 commit 4d439bb

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
There are two packaging standards for Node.js modules.
2+
3+
.. list-table::
4+
:header-rows: 1
5+
6+
* - Packaging Standard
7+
- Works with require()
8+
9+
* - ``CommonJS`` (CJS)
10+
- Yes
11+
12+
* - ``ECMAScript Module`` (ES Module)
13+
- No
14+
15+
You cannot ``require()`` an ES module in ``mongosh``. If you want to use
16+
functionality from an ES module, check to see if there is a CommonJS
17+
version that you can use instead. For more information, see:
18+
19+
- `Node.js module documentation
20+
<https://nodejs.org/api/esm.html#modules-ecmascript-modules>`__
21+
- `Installing older versions of npm packages
22+
<https://docs.npmjs.com/cli/v9/commands/npm-install>`__
23+

source/write-scripts/require-external-modules.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ install the modules either:
114114
- Globally
115115
- In the ``node_modules`` directory in your current working directory.
116116

117+
.. include:: /includes/scripting/fact-require-ems-modules.rst
118+
117119
.. tip::
118120

119121
You can require remote npm modules using this construction:

source/write-scripts/require-load-differences.txt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ The ``require()`` and ``load()`` methods include files and modules in
1616
your scripts for added functionality. However, ``require()`` and
1717
``load()`` differ in their behaviors and availability.
1818

19-
Types of Scripts in ``mongosh``
20-
-------------------------------
19+
Types of Scripts in mongosh
20+
---------------------------
2121

2222
You can use the following types of scripts with ``mongosh``:
2323

@@ -30,8 +30,8 @@ You can use the following types of scripts with ``mongosh``:
3030
- Node.js scripts, which are any scripts loaded with ``require()``,
3131
including npm packages. These scripts are always files.
3232

33-
Availability of ``require()`` and ``load()``
34-
--------------------------------------------
33+
Availability of require() and load()
34+
------------------------------------
3535

3636
The ``require()`` and ``load()`` methods differ in availability
3737
depending on the type of script you are using.
@@ -40,8 +40,8 @@ depending on the type of script you are using.
4040

4141
- In Node.js scripts, only ``require()`` is available.
4242

43-
File Paths for ``require()`` and ``load()``
44-
-------------------------------------------
43+
File Paths for require() and load()
44+
-----------------------------------
4545

4646
The type of script determines how you specify file paths with
4747
``require()`` or ``load()``.
@@ -73,7 +73,7 @@ The type of script determines how you specify file paths with
7373
To change the shell's working directory, use the :ref:`cd()
7474
<mongosh-native-method-cd>` method in your script.
7575

76-
Load External Code in a ``mongosh`` Script
76+
Load External Code in a mongosh Script
7777
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7878

7979
You can load external code in a ``mongosh`` script file, such as an npm
@@ -111,8 +111,13 @@ package or a separate ``mongosh`` script.
111111
const localRequire = require('date-fns').createRequire(__filename);
112112
const fileExports = localRequire('./test-suite2.js'); }
113113

114-
Access to the ``mongosh`` API
115-
-----------------------------
114+
require() Packaging Considerations
115+
----------------------------------
116+
117+
.. include:: /includes/scripting/fact-require-ems-modules.rst
118+
119+
Access to the mongosh API
120+
-------------------------
116121

117122
- ``mongosh`` scripts can use the ``mongosh`` API.
118123

@@ -134,4 +139,3 @@ available inside of Node.js scripts.
134139
Generally, you should not keep mongosh-specific code inside Node.js
135140
scripts.
136141

137-

0 commit comments

Comments
 (0)