How do I set Linux environment variables so that pm2 can see them for node.js apps?
<p>When I run my nodejs application on my Ubuntu Linux server using <code>node server.js</code> it works correctly, and outputs the value of the environment variable <code>$db</code> using <strong>process.env.db</strong>.</p>

<p>However, the app breaks when running <code>sudo pm2 start server.js</code>, seeing the environment variable as <strong>undefined</strong>.</p>

<p>I've tried adding the variable in the following files:</p>

<ul>
<li>etc/environment: <code>db="hello"</code></li>
<li>~/.ssh/environment : <code>db="hello"</code></li>
<li>~/.bashrc : <code>export db="hello"</code></li>
</ul>

<p>I've also rebooted and run <code>source ~/.bashrc</code> to ensure the variable is available.</p>

<p>I think I've tried everything mentioned here, I don't know what else to do:</p>

<ul>
<li><a href="https://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables">https://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables</a></li>
<li><a href="https://github.com/Unitech/pm2/issues/867" rel="noreferrer">https://github.com/Unitech/pm2/issues/867</a></li>
<li><a href="https://stackoverflow.com/questions/216202/why-does-an-ssh-remote-command-get-fewer-environment-variables-then-when-run-man">Why does an SSH remote command get fewer environment variables then when run manually?</a></li>
<li><a href="https://serverfault.com/questions/389601/etc-environment-export-path">https://serverfault.com/questions/389601/etc-environment-export-path</a></li>
</ul>