Ihsan Fajar Ramadhan
Ihsan Fajar Ramadhan

Reputation: 1033

"sudo npm start" doesn't work but "npm start" work

so I'm trying to run my react dev server on port 80, I need to use sudo to use PORT=80

but when I try to start the server with

sudo npm start

it returns error like this:

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: spawn cmd.exe ENOENT
    at _errnoException (util.js:1022:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
    at onErrorNT (internal/child_process.js:372:16)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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 log file

the log file:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/local/bin/npm', 'start' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle [email protected]~prestart: [email protected]
6 info lifecycle [email protected]~start: [email protected]
7 verbose lifecycle [email protected]~start: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~start: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/mnt/d/project/excercises/node/react$9 verbose lifecycle [email protected]~start: CWD: /mnt/d/project/excercises/node/react/counter-app
10 silly lifecycle [email protected]~start: Args: [ '-c', 'react-scripts start' ]
11 silly lifecycle [email protected]~start: Returned: code: 1  signal: null
12 info lifecycle [email protected]~start: Failed to exec start script
13 verbose stack Error: [email protected] start: `react-scripts start`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at EventEmitter.emit (events.js:214:7)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at ChildProcess.emit (events.js:214:7)
13 verbose stack     at maybeClose (internal/child_process.js:925:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid [email protected]
15 verbose cwd /mnt/d/project/excercises/node/react/counter-app
16 verbose Linux 4.4.0-18362-Microsoft                                                                                                                          17 verbose argv "/usr/bin/node" "/usr/local/bin/npm" "start"                                                                                                    18 verbose node v8.10.0                                                                                                                                         19 verbose npm  v6.12.1
20 error code ELIFECYCLE
21 error errno 1
22 error [email protected] start: `react-scripts start`
22 error Exit status 1
23 error Failed at the [email protected] start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

but if I simply use "npm start" it works perfectly, but I want to use port 80

Upvotes: 1

Views: 4252

Answers (2)

Shadi Naif
Shadi Naif

Reputation: 184

Perhaps you are looking for the wrong solution. It is a bad idea to run npm start with sudo.

Instead; you can forward port 80 to react's port 3000

sudo iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 3000

https://coderwall.com/p/plejka/forward-port-80-to-port-3000

this is the simple way; but you can search for the nginx way if it is a production server

Good luck,

Upvotes: 1

Quinox
Quinox

Reputation: 583

Did you already try to define the port when launching your server ?

set PORT= 80 && node app.js

You could also update your package.json to just use npm run start. package.json:

...
  "scripts": {
    "start": "set PORT= 80 && node app.js"
...

Upvotes: 2

Related Questions