> ## Documentation Index
> Fetch the complete documentation index at: https://docs.usebruno.com/llms.txt
> Use this file to discover all available pages before exploring further.

# External Libraries

Bruno allows you to load any npm module or package for use in your scripting workflows.

> [!WARNING]
>
> Always turn on [Developer Mode](https://docs.usebruno.com/get-started/configure/javascript-sandbox.md) when using external libraries in Bruno.

## Prerequisites

Before you begin, ensure that you have the following:

1. Node.js installed on your system.
2. Bruno installed.

## Project structure

The folder structure depends on your collection format.

> [!NOTE]
>
> Starting with Bruno v3.1.0, YAML (OpenCollection format) is the default file format when creating new collections. Learn more about create collection [here](https://docs.usebruno.com/get-started/bruno-basics/create-a-collection.md).

> [tab] YAML Collections
>
> **bruno-collection**
>
> **external-lib**
>
> **echo-api.yml**
>
> **auth.yml**
>
> **data.csv**
>
> **package.json**
>
> **opencollection.yml**
>
> **.gitignore**
>
> **node\_modules**

> [tab] Bru Collections
>
> **bruno-collection**
>
> **external-lib**
>
> **echo-api.bru**
>
> **auth.bru**
>
> **data.csv**
>
> **package.json**
>
> **bruno.json**
>
> **.gitignore**
>
> **node\_modules**

## Configuration

You need a `package.json` file at the root of your Bruno collection.

1. Open your collection folder in **Bruno** or in an editor such as **VS Code**.
2. Open the integrated terminal (in Bruno or your editor) and go to the collection root:

```bash theme={null}
cd path/to/your/bruno-collection
```

3. Initialize `package.json`:

```bash theme={null}
npm init -y
```

4. Install the package (run this in the same terminal, from the collection root):

```bash theme={null}
npm install csv-parse
```

5. Confirm **`csv-parse`** appears under `dependencies` in `package.json`, for example:

```json theme={null}
"dependencies": {
  "csv-parse": "^6.2.1"
}
```

Exact versions may differ, but what matters is that the dependency is listed after a successful install.

6. In Bruno, enable **[Developer Mode](https://docs.usebruno.com/get-started/configure/javascript-sandbox.md)** for the collection so `require()` can load packages from `node_modules`.

## Example: Parse CSV with csv-parse

This example uses [csv-parse **<sup>↗</sup>**](https://www.npmjs.com/package/csv-parse) to turn CSV text into JavaScript objects, then sends the first row as JSON on the request. See the [npm package page **<sup>↗</sup>**](https://www.npmjs.com/package/csv-parse) for options, streaming APIs, and more.

Use a **Pre Request** script with **Developer Mode** on:

```javascript theme={null}
const { parse } = require('csv-parse/sync');

const csvData = `name,email
Smith,smith@example.com
John,john@example.com`;

const records = parse(csvData, {
  columns: true,
  skip_empty_lines: true
});

req.setHeader('Content-Type', 'application/json');
req.setBody(records[0]);
```

![Bruno Pre Request script using an external library (csv-parse)](https://mintcdn.com/bruno-a6972042/seGko4JB7JPOWh_n/images/screenshots/script/external-lib.webp?fit=max\&auto=format\&n=seGko4JB7JPOWh_n\&q=85\&s=6f88af94e1eac0582bc561155f653a7d)

`records[0]` is the first data row as an object (for example `{ name: 'Smith', email: 'smith@example.com' }`) because `columns: true` uses the first line as headers.

### Reading CSV from a file

You can also read a CSV file. Enable [Developer Mode](https://docs.usebruno.com/get-started/configure/javascript-sandbox.md) so `fs` and `path` are available.

Place `data.csv` **next to the request** `.yml` file and use `__dirname`:

```javascript theme={null}
const fs = require('fs');
const path = require('path');
const { parse } = require('csv-parse/sync');

const csvData = fs.readFileSync(path.join(__dirname, 'data.csv'), 'utf8');
const records = parse(csvData, {
  columns: true,
  skip_empty_lines: true
});

req.setHeader('Content-Type', 'application/json');
req.setBody(records[0]);
```

If the file lives at the **collection root** (or in a shared folder under it), use `bru.cwd()` instead:

```javascript theme={null}
const csvData = fs.readFileSync(path.join(bru.cwd(), 'data.csv'), 'utf8');
```

`__dirname` is the directory of the executing file (the request, folder, or collection script). `bru.cwd()` is always the collection root. See [`__dirname` and `__filename`](https://docs.usebruno.com/testing/script/javascript-reference#__dirname-and-__filename).
