Woodshop for old agent generation
Try the new agent generation
  • What is woodshop
  • How to's
    • Smart Relationship
      • GetIdsFromRequest
    • Smart views
      • Display a calendar view
      • Create a custom tinder-like validation view
      • Create a custom moderation view
      • Create a dynamic calendar view for an event-booking use case
    • Configure environment variables
      • NodeJS/Express projects
    • Elasticsearch Integration
      • Interact with your Elasticsearch data
      • Elasticsearch service/utils
      • Another example
    • Zendesk Integration
      • Authentication, Filtering & Sorting
      • Display Zendesk tickets
      • Display Zendesk users
      • View tickets related to a user
      • Bonus: Direct link to Zendesk + change priority of a ticket
    • Dwolla integration
      • Display Dwolla customers
      • Display Dwolla funding sources
      • Display Dwolla transfers
      • Link users and Dwolla customers
      • Dwolla service
    • Make filters case insensitive
    • Use Azure Table Storage
    • Create multiple line charts
    • Create Charts with AWS Redshift
    • View soft-deleted records
    • Send Smart Action notifications to Slack
    • Authenticate a Forest Admin API against an OAuth protected API Backend
    • Translate your project into TypeScript
      • V8
        • Migrate Mongoose files
        • Migrate Sequelize files
      • v7
        • Migrate Mongoose files
        • Migrate Sequelize files
      • v6
    • Geocode an address with Algolia
    • Display/edit a nested document
    • Send an SMS with Zapier
    • Hash a password with bcrypt
    • Display a customized response
    • Search on a smart field with two joints
    • Override the count route
    • Make a field readOnly with Sequelize
    • Hubspot integration
      • Create a Hubspot company
      • Display Hubspot companies
    • Impersonate a user
    • Import data from a CSV file
    • Import data from a JSON file
    • Load smart fields using hook
    • Pre-fill a form with data from a relationship
    • Re-use a smart field logic
    • Link to record info in a smart view
    • Display data in html format
    • Upload files to AWS S3
    • Display AWS S3 files from signed URLs
    • Prevent record update
    • Display, search and update attributes from a JSON field
    • Add many existing records at the same time (hasMany-belongsTo relationship)
    • Track users’ logs with morgan
    • Search on relationship fields by default
    • Export related data as CSV
    • Run automated tests
  • Forest Admin Documentation
Powered by GitBook
On this page
  • Install Dotenv in your project
  • Initialise your Dotenv package
  • Configure your environment variables

Was this helpful?

  1. How to's
  2. Configure environment variables

NodeJS/Express projects

PreviousConfigure environment variablesNextElasticsearch Integration

Last updated 3 years ago

Was this helpful?

When you develop a NodeJS project, you will most commonly have multiple environments and usually follow a flow akin to this one:

  1. You develop new features on your development environment

  2. You push the changes to a remote server (staging, pre-prod, sandbox, ...) with a similar configuration to your production environment

  3. You deliver the feature to your users in production

Setting up environment variables allow you for example to integrate third party services with API keys, identify the current environment to activate or deactivate some features, provide informations to your context, connect to the right database. This is a mandatory step to ensure scalability and to scope features from environment to environment. At Forest Admin, we do use this kind of app contextualisation to identify your environments, perform authentication on your servers and much more. Setting up such a context is mandatory to integrate Forest Admin to your project, and this document will explain how to setup environment variables.

Install Dotenv in your project

is a zero-dependency module that loads environment variables from a .env file into . To install it in your project, simply run the following at the root of your project:

npm install -s dotenv@latest

This will install the dotenv dependency in your project.

Initialise your Dotenv package

The initialisation of dotenv is pretty straightforward. To make it push the variables from your .env into your node environment, simply at the following at the very top of the entry script of your node application (most commonly app.js).

Do note that if the Dotenv package is not initialized before requiring forest packages, they might initialize themselves with wrong environment variables. Require the Dotenv configuration before anything else.

app.js
require('dotenv').config()
...

Configure your environment variables

Create a .env file at the root of your project, and add in your environment variables. Forest Admin environment variables will look like the following:

.env
APPLICATION_URL=http://localhost:3310
FOREST_AUTH_SECRET=fad0c3f44629c1892a0da65e957ac4ef8eb20f3dfbb4d861f032864062933a7c0b3c479ee42c0907179d2a3dafeaafb2
FOREST_ENV_SECRET=03940b538e6a7b58dd07754f40dcdf63edc03acd295d593e1746fad5dc6e8958

And that's it! You can now restart your server, and your environment variables will be automatically handled by dotenv.

Please keep in mind that every-time you change your environment variables, you need to restart your server so the changes will be applied.

If you have versioned your code, consider adding the .env file to your ignored files from git, in .gitignore

Dotenv
process.env