Use require() in Playgrounds
On this page
You can use the require() function in
your playgrounds to reuse code in your playgrounds. The code you
include with require()
can be either:
Use Cases
Using require()
to import code helps keep your playgrounds
organized. If you are often performing the same tasks in your
playgrounds, you can use require()
to reuse code and avoid writing
full functions in each of your playgrounds.
For example, consider the following scenarios where reusing code can simplify logic and improve productivity:
You need to ensure that documents are inserted with a consistent structure. You can import a JavaScript file to validate the data format before the playground script performs the insert.
You store monetary data and want to convert values between different currencies. You can import a JavaScript function to perform conversion calculations.
You need to import data from files on your local computer. You can use the native Node module
fs
to read data from local files.
Get Started
To learn how to use require()
in your playgrounds, see the following
tutorials: