Docs Menu
Docs Home
/
MongoDB for VS Code
/

Use require() in Playgrounds

On this page

  • Use Cases
  • Get Started

You can use the require() function in your playgrounds to reuse code in your playgrounds. The code you include with require() can be either:

  • A native Node module (such as fs)

  • An external Node module downloaded from npm

  • A local JavaScript file

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.

To learn how to use require() in your playgrounds, see the following tutorials:

  • Use require() to Include Node.js Modules

  • Use require() to Load Local Files

Back

Export to Language