Skip to content

Commit 8f49c4c

Browse files
(DOCSP-10493): require() for playgrounds (mongodb#17)
* (DOCSP-10539): require() for playgrounds * fix typo in example
1 parent dca3229 commit 8f49c4c

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

source/playgrounds.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Tutorials
5252
- To learn how to use MongoDB Playgrounds for CRUD operations, see
5353
:ref:`vsce-crud`.
5454

55-
- To learn how to use MonogDB Playgrounds to run aggregation
55+
- To learn how to use MongoDB Playgrounds to run aggregation
5656
pipelines, see :ref:`vsce-aggregation`.
5757

5858
Consideration for Authentication
@@ -67,3 +67,4 @@ Consideration for Authentication
6767

6868
/crud-ops
6969
/run-agg-pipelines
70+
/require-modules

source/require-modules.txt

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
.. _vsce-require:
2+
3+
=============================================
4+
Use ``require()`` to Include External Modules
5+
=============================================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
.. include:: /includes/fact-vsce-preview.rst
16+
17+
.. important::
18+
19+
A complete description of Node.js, modules, and the
20+
`require() <https://nodejs.org/api/modules.html#modules_require_id>`__
21+
function is out of scope for this tutorial. To learn more, refer to
22+
the `Node.js Documentation <https://nodejs.org/api/modules.html>`__.
23+
24+
You can use the
25+
`require() <https://nodejs.org/api/modules.html#modules_require_id>`__
26+
function in your MongoDB Playgrounds to
27+
include modules which exist in separate files.
28+
29+
Require Native Modules
30+
----------------------
31+
32+
You can ``require()`` native Node modules (such as
33+
`fs <https://nodejs.org/api/fs.html#fs_file_system>`__) in your
34+
Playground without any additional setup or configuration.
35+
36+
.. example::
37+
38+
The following Playground uses the ``fs`` module to write a document
39+
from the ``test.employees`` collection to a file named
40+
``employee.txt``:
41+
42+
.. code-block:: javascript
43+
44+
const fs = require('fs');
45+
46+
use("test");
47+
48+
const document = db.employees.findOne();
49+
50+
fs.writeFileSync('employee.txt', JSON.stringify(document));
51+
52+
.. seealso::
53+
54+
- `fs.writeFileSync
55+
<https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options>`__
56+
57+
- `require()
58+
<https://nodejs.org/api/modules.html#modules_require_id>`__
59+
60+
Require Non-Native Modules
61+
--------------------------
62+
63+
To ``require()`` non-native Node modules (such as those downloaded from
64+
`npm <https://www.npmjs.com/>`__) you must install the module in one of
65+
the following folders based on your operating system:
66+
67+
.. list-table::
68+
:header-rows: 1
69+
:widths: 20 40
70+
71+
* - Operating System
72+
- Module Location
73+
74+
* - macOS and Linux
75+
- One of either:
76+
77+
- ``$HOME/.node_modules``
78+
79+
- ``$HOME/node_modules``
80+
81+
- ``$HOME/.vscode/extensions/node_modules``
82+
83+
- ``$HOME/.vscode/extensions/mongodb.mongodb-vs-code-<version>\node_modules``
84+
85+
* - Windows
86+
- One of either:
87+
88+
- ``C:\Users\.node_modules``
89+
90+
- ``C:\Users\node_modules``
91+
92+
- ``C:\Users\<user>\node_modules``
93+
94+
- ``C:\Users\<user>\.vscode\extensions\node_modules``
95+
96+
- ``C:\Users\<user>\.vscode\extensions\mongodb.mongodb-vscode-<version>\node_modules``
97+
98+
Once you install or copy your desired package to one of the module
99+
directories, you can ``require()`` that package.
100+
101+
.. example::
102+
103+
The following Playground uses the
104+
`moment <https://www.npmjs.com/package/moment>`__ package to write
105+
the current date to a file called ``date.txt``:
106+
107+
.. code-block:: javascript
108+
109+
const moment = require('moment');
110+
const fs = require('fs');
111+
112+
const currentDate = moment().format("MMMM DD YYYY");
113+
114+
fs.writeFileSync('date.txt', currentDate);
115+
116+
.. seealso::
117+
118+
- `fs.writeFileSync
119+
<https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options>`__
120+
121+
- `require()
122+
<https://nodejs.org/api/modules.html#modules_require_id>`__

0 commit comments

Comments
 (0)