Node app on Plesk - Environment variables on custom scripts - node.js

I'm running a Node v12 app on Plesk Obsidian v18.0.29
I've set the required environment variables in the dedicated section:
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".
> REDACTED#0.1.0 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! REDACTED#0.1.0 migrate: `sequelize db:migrate`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the REDACTED#0.1.0 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:
> REDACTED#0.1.0 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! REDACTED#0.1.0 migrateprod: `NODE_ENV=production sequelize db:migrate`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the REDACTED#0.1.0 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!

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.

Related

Why can't npm start?

I got a new notebook, so I install all languages I use. I use nodejs v10.16.
I had an Angular code which I wanted to run on the new notebook. The version of the node was the same. However, when I wrote in the command line npm start, I got these message:
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! <my-project>-client#0.0.0 start: `ng serve`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the <my-project>-client#0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Why did it happend? I install node_modules before starting and it ran on my earlier notebook.
How should I solve this problem?
Make sure you're running the start script from the same location as your package.json file.
Check the format of package.json scripts object, ensuring the script name is correct and denoting where the script/object ends with a comma.
"scripts": {
"start": "react-scripts start"
},
Ensure the app/module your start script is calling is installed/installed correctly.
Use the full npm command to run your start script:
npm run start
If all else fails, rename your node_modules to node_modules_old, check your package.json file has all your required dependencies and run npm install from the same directory to generate a new node_modules folder before trying to run the script again with npm run start.

npm: start script fails

I'm developing a simple backend project with Node.js (v12.16.1) on Windows 10.
My package.json file contains the following command:
"scripts": {
"start": "NODE_ENV=production node --experimental-modules --require dotenv/config server.mjs dotenv_config_path=config/config.env",
...
}
And when I type npm run start, I get
> NODE_ENV=production node --experimental-modules --require dotenv/config server.mjs dotenv_config_path=config/config.env
'NODE_ENV' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! devcamper-api#1.0.0 start: `NODE_ENV=production node --experimental-modules --require dotenv/config server.mjs dotenv_config_path=config/config.env`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the devcamper-api#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Why does this failure occure?
Windows doesn't understand this way of setting environment variables.
Use cross-env package instead: https://www.npmjs.com/package/cross-env
npm install --save-dev cross-env
and then in your script:
"cross-env NODE_ENV=production ... "

HOST is not recognized as an internal or external command [duplicate]

This question already has answers here:
Setting environment variables package.json in Windows 10
(4 answers)
Closed 3 years ago.
After cloning a Git repo with a ReactJS and NodeJS project and install NodeJS
wanted to see the result in the browser and for that had to run in the root folder which had a package.json
npm install
and after
npm start
First command worked fine but the second was giving the following error
> HOST=0.0.0.0 PORT=8000 ./node_modules/.bin/react-scripts start
'HOST' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ...: `HOST=0.0.0.0 PORT=8000 ./node_modules/.bin/react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ... start 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:
The package.json has the following
"scripts": {
"start": "HOST=0.0.0.0 PORT=8000 ./node_modules/.bin/react-scripts start",
Tested using Git Bash, CMD and PowerShell and kept on getting the same error, as you can see in the following images
Git Bash
CMD
PowerShell
Method 1
If I run (without using the npm wrapper script)
HOST=0.0.0.0 PORT=8000 ./node_modules/.bin/react-scripts start
it works fine. As Quentin says,
Must be something to do with how npm shells out then
To fix it, I've gone to package.json and changed the "start" script to
"start": "./node_modules/.bin/react-scripts start",
Then npm start works fine.
Method 2
Use the cross-env package.
For that install it using the following command
npm i cross-env
then go to package.json and change it to
"start": "cross-env ./node_modules/.bin/react-scripts start",
And then running npm start will also work fine:

Elastic Beanstalk to run 'concurrently' command via npm script

I am trying to deploy my React + API server application to AWS Elastic Beanstalk and experiencing some trouble right now.
I was following this tutorial and I configured my npm scripts as such:
"scripts": {
"start": "concurrently \"npm run server\" \"npm run client\" ",
"server": "node server.js",
"client": "node start-client.js"
},
However, EC2 seems to be not able to find the concurrently command, when I read the logs for the failure.
Concurrently is installed as dev-dependencies but I understand that Elastic Beanstalk installs dev-dependencies as well unless asked otherwise.
With some googling, I came down to a suspicion that EC2 instance does not have the location of the installed node packages in its PATH but I am not quite sure how to solve this issue.
EDIT: I copied the error log from EBS log console.
\> kid-server#0.0.0 start /var/app/current
\> concurrently "npm run server" "npm run client"
sh: concurrently: command not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! kid-server#0.0.0 start: `concurrently "npm run server" "npm run client" `
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the kid-server#0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm update check failed
Try running with sudo or get access
to the local update config store via
sudo chown -R $USER:$(id -gn $USER) /tmp/.config
According to this answer posted on SO, Elastic Beanstalk now defaults to running npm install --production. So you have two options to get npm package, concurrently, installed.
Add it to your production dependencies.
Instruct Elastic Beanstalk to not add the --production flag.
Option 1 is easier to implement (though may not meet your requirements). Option 2 would involve adding a any.config (name doesn't matter) file to a .ebextensions directory in your project root that contains the following yaml:
option_settings:
- namespace: aws:elasticbeanstalk:application:environment
option_name: NPM_USE_PRODUCTION
value: false
If you have ssh set up for your EB instance, ssh into it and navigate to /var/app/current/node_modules. You will likely see that concurrently is not there.

working node.js projects on windows 10 now no longer working

Node.js projects are no longer working. Have node.js 7.10.x npm 4.2.1
Projects that were working are no longer working. I uninstalled node.js, removed npm-cache, reinstalled fresh. Deleted node_modules directory, ran npm install on project's package.json and npm start xxxx etc. No matter what project I run, the error remains the same as shown below. The same projects installed on Linux Ubuntu work just fine.
No clue as to why these projects are no longer working. The following error is consistent with each node project I've tested.
Any help would be appreciated. Thanks in advance.
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "test"
npm ERR! node v7.10.1
npm ERR! npm v4.2.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! muber#1.0.0 test: `NODE_ENV=test nodemon --exec 'mocha --recursive -R min'`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the muber#1.0.0 test script 'NODE_ENV=test nodemon --exec 'mocha --recursive -R min''.
npm ERR! Make sure you have the latest version of node.js and npm installed.
I think I know what the problem is if it works on Linux but not the Windows machine. Windows cannot run CLI scripts the same; it requires a different syntax.
To fix it, you simply have to do npm install --save cross-env and then in your package.json file, add cross-env to the beginning of the script declaration:
ie:
"scripts": {
"start": "NODE_ENV=test nodemon --exec 'mocha --recursive -R min'"
},
will become:
"scripts": {
"start": "cross-env NODE_ENV=test nodemon --exec 'mocha --recursive -R min'"
},
That should fix it.
To fix it on Windows, you'd have to change it to (I think):
"scripts": {
"start": "NODE_ENV=test&&nodemon --exec 'mocha --recursive -R min'"
},
You can do a quick test and change it to that and run it. If that fixes it, this is exactly your problem. But, that will break it on Linux, so generally, cross-env is recommended. Make sure you dont have spaces also at &&.

Resources