Unrecognized PORT command
Asked Answered
G

10

17

In my localhost, running below command

PORT=4080 node server.js

But, it was throwing an unexpected error.

'PORT' is not recognized as an internal or external command, operable program or batch file.

Do I need to install any module or did I miss any syntax to run it properly?

Gunboat answered 14/12, 2017 at 5:47 Comment(4)
The above command is Unix (Ubuntu, Mac, etc…). In windows enviroment, you need a different syntax. Google something like "set env in windows command line"Cupulate
I configured environment variable for nodeJs, If I run node server.js, It was running on port 3000. But I need to run it on port 4030. I thought, I need a module for the port but I didn't find anything related to that in google. I thought it would be useful for others if I raise this as an issue.Gunboat
You said you have configured for port 3000, you an change that configuration to 4080, it should work??Cupulate
@Subburaj, It is a default port but I need to change it to 4080. For that configuration, I didn't find any article.Gunboat
G
38

Apart from cross-env, Below command can serve the purpose.

SET PORT=4080 && node server.js 

PS: Set environment variable for nodeJs, run command in project folder.

Gunboat answered 14/12, 2017 at 6:18 Comment(3)
This would cause your system variables to change, which is not very desirable. cross-env solves this.Hargett
Will this work in Linux/MacOS?Peculium
No, it is for windows.Gunboat
T
9
  • Install this additional package cross-env in your node environment. The above command you mentioned is Unix (Ubuntu, Mac, etc…). In the windows environment, you need a different syntax and cross-env does your job.
  • You can also create an .env file with

PORT=3006

and save it in your project directory.

Talanian answered 14/12, 2017 at 6:3 Comment(2)
Hey please check my answer and verify it :)Gunboat
I suggest going with this solution. Chances are, there will be other variables and you'll be monkey patching this all day long. Leave that to the authors from cross-env.Lermontov
G
7

In Windows you can set scripts in package.json:

"start": " set PORT=portnumber && react-app-rewired start"

or

"start": " set PORT=3001 && react-script start".

It worked for me:

in package.json file

Gravy answered 6/9, 2020 at 19:36 Comment(1)
Nice one! This worked for me in Windows in VSCode. ThanksLydon
A
2

SET PORT=8090 && nodemon server.js that worked for me

Anibalanica answered 1/10, 2018 at 7:4 Comment(4)
I need to run port 4080Gunboat
Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. See: How do I write a good answer?. ThanksEdging
If things dont work out this way, leave it like nodemon server.js then in your terminal say export port=4080 (your choice port of course) then npm nodemon to start the serverAnibalanica
@Anibalanica Thanks for sharing your knowledge, unfortunately, I solved it and not tested with your explanation.Gunboat
P
1

Install cross-env to support all OS.

npm install --save-dev cross-env
or
yarn add -D cross-env

And finally add cross-env to your command in package.json.

cross-env PORT=4080 node server.js
Peculium answered 28/2, 2023 at 16:31 Comment(0)
B
0

Here's a moronic way I found to recreate this problem:

  1. Create a npm based app on one system
  2. check it out on another system
  3. FAIL to run npm install
  4. run npm start

Receive error described in question.

Bernoulli answered 1/1, 2020 at 18:49 Comment(0)
P
0

automatically run this port :3000,I tried windows working react

"devDependencies": {
    "react-scripts": "0.8.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
Presser answered 11/12, 2020 at 3:28 Comment(1)
did you even try to read the question?Whiles
S
0

You can try this way also

 "scripts": {
    "build": "concurrently \"cd client && npm build\" \"cd server && npm build\"",
    "install": "(cd client && npm) && (cd server && npm)",
    "start": "concurrently \"cd client && SET PORT=3000 && npm start\" \"cd server && SET PORT=3001 && npm start\"",
    "start:prod": "cd server && npm start:prod"
  }
Saltsman answered 4/1, 2021 at 7:15 Comment(0)
E
0

In Windows you can set scripts in package.json:

Delete set PORT=portnumber &&

and keep it like this :

"start": "react-scripts start"
Embree answered 29/10, 2022 at 13:22 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Adkison
M
0

Once I realised that the issue is it's a linux command, I switch shells to use Git Bash(I'm using Vs-code) and it worked seamlessly. Hoping this is helpful for a newbie that finds the other suggestions daunting.

Meteoroid answered 12/6, 2023 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.