Search code examples
node.jsenvironment-variablessequelize.jsplesksequelize-cli

Node app on Plesk - Environment variables on custom scripts


I'm running a Node v12 app on Plesk Obsidian v18.0.29

I've set the required environment variables in the dedicated section: Plesk configuration

Also the "Application Mode" should in theory act as NODE_ENV for the process and seems to do so. Just FYI I also tried adding it to the "Custom environment variables" but that doesn't solve my problem.

The problem being that, despite the app itself running fine, when I try to run a custom script (DB migrations) Plesk seems to ignore these variables, as you can see from Using environment "development".

> [email protected] migrate /var/www/vhosts/REDACTED/httpdocs
> sequelize db:migrate


[4mSequelize CLI [Node: 12.4.0, CLI: 5.4.0, ORM: 5.8.7][24m

Loaded configuration file "config/database.js".
Using environment "development".


[31mERROR:[39m Error parsing url: undefined
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] migrate: `sequelize db:migrate`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] migrate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /var/www/vhosts/REDACTED/.npm/_logs/2020-09-04T08_13_39_221Z-debug.log

If I try to create a custom script that forces the NODE_ENV variable, the correct environment is considered, but the other variables are still ignored. For example, by creating a script called migrateprod and running it through Plesk:

"scripts": {
  "test": "npm run test:unit && npm run test:e2e",
  "test:unit": "mocha tests/unit/**/**.spec.js --require ./tests/setup.js --exit",
  "test:e2e": "mocha tests/e2e/**/**.spec.js --require ./tests/setup.js --exit",
  "start": "node index.js",
  "watch": "nodemon index.js",
  "migrate": "sequelize db:migrate",
  "migrateprod": "NODE_ENV=production sequelize db:migrate",
  "migrate:down": "sequelize db:migrate:undo:all",
  "cli": "node cli/index.js"
},

I can now see the environment production but the DATABASE_URL variable is not found, getting an error instead:

> [email protected] migrateprod /var/www/vhosts/REDACTED/httpdocs
> NODE_ENV=production sequelize db:migrate

[4mSequelize CLI [Node: 12.4.0, CLI: 5.4.0, ORM: 5.8.7][24m

Loaded configuration file "config/database.js".
Using environment "production".


[31mERROR:[39m Error parsing url: undefined
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] migrateprod: `NODE_ENV=production sequelize db:migrate`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] migrateprod script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /var/www/vhosts/REDACTED/.npm/_logs/2020-09-04T08_21_51_188Z-debug.log

Bottom line is, Plesk seems to ignore or avoid setting the environment variables while running custom scripts. I tried searching around but I couldn't find any similar case, which makes me think I'm just missing something.

Any ideas? Thanks in advance for your time!


Solution

  • Turns out Plesk does not forward environment variables to custom scripts, no matter how hard you try. So I modified the package.json to set the variables on the fly before running the script.

    "scripts": {
      ... other scripts ...
      "cli": "export $(cat ../.env.production | xargs) && node cli/index.js"
    },
    

    There's an .env file in a protected folder outside the application file system, from which I get the variables and send them as args to the custom script.

    Not the most elegant solution, but it sorts out the problem.