Search code examples
node.jslernamonorepodotenv

How to use lerna with dotenv package?


I have a monorepo managed using Lerna. I need to use multiple environment variables to start my web server. My npm script to start the server is:

"scripts": {
    "dev:start": "lerna run --parallel dev:start"
}

I am also trying to use dotenv package to load environment variables for .env file. Since, dotenv is purely development helper to set env vars, I have installed it as a devDependency of top package.json. dotenv utilizes node.js preload script like: node -r dotenv/config server_script.js.

But with Lerna, I can no longer invoke preload script. Is there any way to use dotenv with lerna? Or alternately, how to run preload scripts with lerna?


Solution

  • But with Lerna, I can no longer invoke preload script

    That's not true. Lerna allows you to pass arguments to the npm script. You just need to pass it with a pair of double dashes:

    packages/ServerPkg/package.json:

    "dev:start": "node foo.js"
    

    package.json

    "dev:start": "lerna run dev:start -- -- -r /path/to/dotenv"
    

    Should execute:

    node foo.js -r /path/to/dotenv