Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

1.0 KB
Raw Blame
---
title: "Inject secrets into a PM2 app"
sidebarTitle: "PM2"
description: "How to use Infisical to inject environment variables and secrets with PM2 into a Node.js app."
---

Prerequisites:

- Set up and add envars to [Infisical Cloud](https://app.infisical.com)
- [Install the CLI](/cli/install)

## Initialize Infisical for your Node.js app

```bash
# navigate to the root of your of your project
cd /path/to/project

# then initialize infisical
infisical init
```

## Create a bash or js script

<CodeGroup>

```bash infisical-run.sh
infisical sm run -- npm start
```

```js infisical-run.js
const spawn = require("child_process").spawn;

const infisical = spawn("infisical", ["sm", "run", "--", "npm", "start"]);

infisical.stdout.on("data", (data) => console.log(`${data}`));
infisical.stderr.on("data", (data) => console.error(`${data}`));
```

</CodeGroup>

## Start your application as usual but with the script

<CodeGroup>

```bash infisical-run.sh
pm2 start infisical-run.sh
```

```bash infisical-run.js
pm2 start infisical-run.js
```

</CodeGroup>