> This page location: /docs.md > /docs/tools-and-connectors.md > /docs/mongodb-vscode.md > /docs/mongodb-vscode/playgrounds.md > /docs/mongodb-vscode/require-playgrounds.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Use require() to Load Local Files

You can use the [require()](https://nodejs.org/api/modules.html#modules_require_id) function in your MongoDB Playgrounds to include code from local files. You can store your code in a single location and reuse that code in different playgrounds.

## About this Task

This tutorial shows how to use `require()` to load local scripts. You can also use `require()` to load Node modules, like those downloaded from [npm](https://www.npmjs.com/). For more information, see [Use require() to Include Node.js Modules.](https://www.mongodb.com/docs/mongodb-vscode/require-playgrounds/require-modules.md#std-label-vsce-require)

## Steps

1. Create a script file

   The following script file validates documents to ensure that the required fields are present. Save the script to your local filesystem as `validate.js`:

   ```javascript
   // validate.js

   const required_fields = [ 'name', 'email' ]

   const validate_data = (document) => {

      let is_valid = true;

      for (const field of required_fields) {
         if (document[field] == null) {
               is_valid = false;
         }
      };
      return is_valid;
   };

   module.exports = validate_data;
   ```

2. Create a playground that uses the validation script

   The following playground uses `require()` to call the `validate_data` function specified in `validate.js`. The `validate_data` function is called on two sample documents. If the document contains the required fields `name` and `email`, it is inserted into the `people` collection.

   **Important:**

   Update the first line of the playground with the path to the `validate.js` file:

   ```javascript
   // playground-1.mongodb.js

   const validate = require('/path/to/validate.js');

   use('mongodbVSCodePlaygroundDB');

   const doc1 = { _id: 1, 'name': 'Taylor', 'email': 't123@gmail.com' };

   const doc2 = { _id: 2, 'name': 'Taylor' };

   const docs = [ doc1, doc2 ];

   let inserted_count = 0;

   for (const doc of docs) {
      if (validate(doc)) {
         db.getCollection('people').insertOne(doc);
         inserted_count++;
      }
   };

   console.log("Inserted " + inserted_count + " documents");
   ```

3. Run the playground

   To run your Playground, press the  button at the top right corner of the Playground View. VS Code Extension splits your Playground and outputs the results of your Playground in the Playground
   Results pane. If you disabled split-view, VS Code Extension outputs the results of your Playground in a new tab.

## Results

Only `doc1` contains both required fields and is inserted into the collection. `doc2` does not contain the required field `email`, and is not inserted.

To confirm that the correct document was inserted, query the `people` collection:

```javascript
use mongodbVSCodePlaygroundDB

db.people.find()
```

Output:

```javascript
[
   { _id: 1, name: 'Taylor', email: 't123@gmail.com' }
]
```

## Learn More

- [Schema Validation](https://www.mongodb.com/docs/manual/core/schema-validation.md#std-label-schema-validation-overview)

- [Use require() to Include Node.js Modules](https://www.mongodb.com/docs/mongodb-vscode/require-playgrounds/require-modules.md#std-label-vsce-require)

- [Create Databases and Collections](https://www.mongodb.com/docs/mongodb-vscode/playground-databases.md#std-label-vsce-playground-databases)
