Using envvault with Node.js/Express.js
Note: Please ensure that you have completed the previous steps
Prerequisites
- Node.js installed on your system
- envvault CLI tool installed
- An existing Node.js/Express.js project
Usage
Running Your Application
To run your Node.js application with environment variables from envvault:
$ envvault run --env=dev <your-command-to-run-your-project>
This command will:
- Load environment variables from the dev environment
- Execute your command to run the project like
npm run dev
with these variables
Caching Environment Variables
For better performance, you can cache your environment variables:
$ envvault run --env=dev -c -- npm run dev
The -c
flag creates a local cache of your environment variables, improving startup time for subsequent runs.
Example Implementation
Here is how to set up a basic Express.js application with envvault:
// app.js
const express = require('express');
const app = express();
// Your environment variables are automatically loaded
const PORT = process.env.PORT || 3000;
const DB_URL = process.env.DB_URL;
app.get('/', (req, res) => {
res.send('Hello from envvault!');
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Best Practices
- Always use the cached mode (
-c
) in development for better performance - Keep your access token secure and never commit it to version control
- Use different environments (dev, staging, prod) for different stages of development
- Regularly update your cached environment variables to ensure you have the latest values
Troubleshooting
Common Issues
- Authentication Failed:Ensure your access token is valid and correctly entered
- Environment Not Found:Verify that the environment name is correct (e.g., dev, staging, prod)
- Cache Issues:Try clearing the cache and running without the -c flag