I have a nodejs/express app which I'm deploying to Azure Appservice on Linux platform. Azure provides the port 8080 on which my app runs. However, when I deploy it, it gives me an error
Error listen EADDRINUSE: address already in use :::8181
error Command failed with exit code 1.
the main command with which I'm running the app is env NODE_ENV=production node index.js
.
The port that the app uses is 8080
. But the error that's coming is for port 8181
. I've tried restarting the server and redeploying it many times over.
I followed this tutorial to deploy. https://learn.microsoft.com/en-us/azure/app-service/quickstart-nodejs?pivots=platform-linux
I've deleted the whole appservice and its plan and redeployed it only to find the same error.
I ssh into the container and checked if 8181
is in use. But it isn't.
Here is how I am starting the application:
const port = 8080;
const server = app.listen(process.env.PORT || port, () => {
console.log(`App running on port ${port}`);
});
Here's a screenshot of the deploy log
What should I do?
EDIT:
Here's my package.json
{
"name": "test-express",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon index.js",
"build:azure": "env NODE_ENV=production node index.js"
},
"dependencies": {
"express": "^4.17.1",
"morgan": "^1.10.0"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}
In the official documentation that I've mentioned below, one of the scripts that starts the application is "build:azure".
Moreover, I added these lines to my code to see what environment variables are getting passed.
console.log("=============");
console.log(process.env.PORT, process.env.NODE_ENV);
console.log("=============");
and the final result of the build process that I got was
...
7:00:08 pm test-express: [1/4] Resolving packages...
7:00:09 pm test-express: [2/4] Fetching packages...
7:00:30 pm test-express: info fsevents@2.3.2: The platform "linux" is incompatible with this module.
7:00:30 pm test-express: info "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation.
7:00:30 pm test-express: [3/4] Linking dependencies...
7:00:45 pm test-express: [4/4] Building fresh packages...
7:00:45 pm test-express: Done in 37.21s.
7:00:45 pm test-express: Running 'yarn run build:azure'...
7:00:46 pm test-express: yarn run v1.22.10
7:00:46 pm test-express: $ env NODE_ENV=production node index.js
7:00:46 pm test-express: =============
7:00:46 pm test-express: 8181 production
7:00:46 pm test-express: =============
7:00:46 pm test-express: events.js:292
7:00:46 pm test-express: throw er; // Unhandled 'error' event
7:00:46 pm test-express: ^
7:00:46 pm test-express: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
7:00:46 pm test-express: Error: listen EADDRINUSE: address already in use :::8181
7:00:46 pm test-express: at Server.setupListenHandle [as _listen2] (net.js:1318:16)
7:00:46 pm test-express: at listenInCluster (net.js:1366:12)
7:00:46 pm test-express: at Server.listen (net.js:1452:7)
7:00:46 pm test-express: at Function.listen (/tmp/8d8e87f8a5dd6db/node_modules/express/lib/application.js:618:24)
7:00:46 pm test-express: at Object.<anonymous> (/tmp/8d8e87f8a5dd6db/index.js:8:20)
7:00:46 pm test-express: at Module._compile (internal/modules/cjs/loader.js:1063:30)
7:00:46 pm test-express: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
7:00:46 pm test-express: at Module.load (internal/modules/cjs/loader.js:928:32)
7:00:46 pm test-express: at Function.Module._load (internal/modules/cjs/loader.js:769:14)
7:00:46 pm test-express: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
7:00:46 pm test-express: Emitted 'error' event on Server instance at:
7:00:46 pm test-express: at emitErrorNT (net.js:1345:8)
7:00:46 pm test-express: at processTicksAndRejections (internal/process/task_queues.js:80:21) {
7:00:46 pm test-express: code: 'EADDRINUSE',
7:00:46 pm test-express: errno: -98,
7:00:46 pm test-express: syscall: 'listen',
7:00:47 pm test-express: address: '::',
7:00:47 pm test-express: port: 8181
7:00:47 pm test-express: }
7:00:47 pm test-express: error Command failed with exit code 1.
7:00:49 pm test-express: /opt/Kudu/Scripts/starter.sh oryx build /tmp/zipdeploy/extracted -o /home/site/wwwroot --platform nodejs --platform-version 14 -i /tmp/8d8e87f8a5dd6db -p compress_node_modules=tar-gz --log-file /tmp/build-debug.log
7:01:07 pm test-express: Deployment failed.
So yes, 8181 is getting passed.
FIXED:
Yes, I got it fixed by changing the script attribute build:azure
to start
. That was the only change I needed to make in package.json
to get it to run.