HOST is not recognized as an internal or external command [duplicate] - node.js

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:

Related

How do I get npm start to open a specific profile on Chrome?

I have a separate Chrome profile to do development work in.
When I run npm start it opens my default Chrome profile.
I'd like it to open my development profile.
I've tried creating a .env file in the root of the project with the following in it
REACT_APP_BROWSER=chrome
REACT_APP_BROWSER_ARGS=--profile-directory="Profile 1"
installing env-cmd and changing the start command in the scripts section of the package.json file
"start": "env-cmd .env npm react-scripts start",
but when I run npm start I get the following error:
alex#Alex-PC:~/code/udemy/grider/pics$ npm start
> pics#0.1.0 start /home/alex/code/udemy/grider/pics
> env-cmd .env npm react-scripts start
spawn .env ENOENT
npm ERR! code ELIFECYCLE
npm ERR! errno 130
npm ERR! pics#0.1.0 start: `env-cmd .env npm react-scripts start`
npm ERR! Exit status 130
npm ERR!
npm ERR! Failed at the pics#0.1.0 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:
npm ERR! /home/alex/.npm/_logs/2020-12-04T22_27_25_381Z-debug.log
Am I even on the right track or have I went in completely the wrong direction?
There is no need for any additional libraries. It seems you are using react and react-scripts is already using dotenv as it can be seen here. This also means that there is no addition needed to start script: "start": "react-scripts start".
Using dotenv you can just create a .env file in your root directory with the following options as it can be seen from here.
.env
BROWSER=google-chrome
BROWSER_ARGS=--profile-directory="Profile 1"
Note: google-chrome on Linux / chrome on Windows for the BROWSER variable
BUT the ISSUE with this seems to be that because of the space in the profile name, it will only be passed the part before the space. Chrome uses an internal profile name for the profile directory and by default creates the internal profile name with space in them.
There does not seem to be a way to pass the profile name with spaces in it (no matter if using quotes/escaped quotes/additional variables etc.) but I really hope I will stand corrected on this issue.
A WORKAROUND to the spacing issue: Create a new profile name without space. This will happen automatically if you just run npm start and the profile name you assigned in the .env file does not match any of the ones already in your browser. Afterwards just sync your account in it and use it as normal.

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.

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:
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.

Trying to set up react.js environment, not working

I used a tutorial from https://www.tutorialspoint.com/reactjs/reactjs_environment_setup.htm
got below message from command prompt when i entered "npm start" I did everything and i have node.js installed. PLEASE HELP!!
edit: wjwar is username
C:\Users\wjwar\Desktop\reactApp>npm start
> reactApp#1.0.0 start C:\Users\wjwar\Desktop\reactApp
> webpack-dev-server --mode development --open --hot
'webpack-dev-server' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! reactApp#1.0.0 start: `webpack-dev-server --mode development --open --hot`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the reactApp#1.0.0 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:
npm ERR! C:\Users\wjwar\AppData\Roaming\npm-cache\_logs\2019-02-27T06_01_00_391Z-debug.log
Step 3 in the guide https://www.tutorialspoint.com/reactjs/reactjs_environment_setup.htm uses npm install --save for webpack, webpack-dev-server and webpack-cli, which is not correct. It should be npm install --save-dev
>npm install webpack --save-dev
>npm install webpack-dev-server --save-dev
>npm install webpack-cli --save-dev
Also the guide seems to be pretty outdated, I suspect it uses webpack version 3, while currently the default version of webpack is 4. You can try to find another sample with newer setup, or stick with create-react-app.
Instead, Use Create-react-app where you'll get the whole boilerplate set up, edit app.js to make your changes
install webpack and webpack-dev-server globally:
open cmd as an administrator
npm install -g webpack webpack-cli webpack-dev-server
restart cmd

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.

Resources