When I try to install NestJs application on IIS-server (with iisnode installed) I always just encounter issue which says "...Error: listen EADDRINUSE: address already in use :::3450.." (see below) And, as you can see from the console.log (see below), the NestJs application is first fully initialized and launched, but then comes always the port already in use issue. And it persists no matter how many different ports I try. I have also tried to restart iisnode.exe and killed the process and the port but it does not help. Anyone any experience with this and found the solution?
node dist/main
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[NestFactory] [39m[32mStarting Nest application...[39m
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[InstanceLoader] [39m[32mConfigHostModule dependencies initialized[39m[33m +34ms[39m
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[InstanceLoader] [39m[32mAppModule dependencies initialized[39m[33m +0ms[39m
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[InstanceLoader] [39m[32mConfigModule dependencies initialized[39m[33m +1ms[39m
hello from NestJs app
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[RoutesResolver] [39m[32mAppController {}:[39m[33m +13ms[39m
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[RouterExplorer] [39m[32mMapped {, GET} route[39m[33m +4ms[39m
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[RoutesResolver] [39m[32mItemsController {/items}:[39m[33m +0ms[39m
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[RouterExplorer] [39m[32mMapped {/items, GET} route[39m[33m +1ms[39m
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[RouterExplorer] [39m[32mMapped {/items/:id, GET} route[39m[33m +2ms[39m
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[RouterExplorer] [39m[32mMapped {/items, POST} route[39m[33m +1ms[39m
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[RoutesResolver] [39m[32mShoppingCartController {/shopping-cart}:[39m[33m +1ms[39m
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[RouterExplorer] [39m[32mMapped {/shopping-cart, POST} route[39m[33m +0ms[39m
[32m[Nest] 36096 - [39m09/01/2020, 1:43:54 PM [33m[NestApplication] [39m[32mNest application successfully started[39m[33m +4ms[39m
App is running in port: 61302
events.js:292
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::61302
at Server.setupListenHandle [as _listen2] (net.js:1313:16)
at listenInCluster (net.js:1361:12)
at Server.listen (net.js:1447:7)
at ExpressAdapter.listen (C:\...\node_modules\@nestjs\platform-express\adapters\express-adapter.js:45:32)
at NestApplication.listen (C:\...\node_modules\@nestjs\core\nest-application.js:146:26)
at async bootstrap (C:\...\dist\main.js:8:9)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1340:8)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EADDRINUSE',
errno: 'EADDRINUSE',
syscall: 'listen',
address: '::',
port: 61302
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nest-restaurant-api@0.0.1 start:prod: `node dist/main`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nest-restaurant-api@0.0.1 start:prod 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:\...\AppData\Roaming\npm-cache\_logs\2020-09-01T12_43_54_285Z-debug.log
In iisnode server error log I can also find the following: (this proved to be logged in previous day - and not to be seen anymore in the logs. So, this iisnode log message may not be relevant for the issue at all)
(node:45472) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(node:45472) UnhandledPromiseRejectionWarning: RangeError [ERR_SOCKET_BAD_PORT]: options.port should be >= 0 and < 65536. Received NaN.
at validatePort (internal/validators.js:182:11)
at Server.listen (net.js:1439:5)
at ExpressAdapter.listen (C:\...\node_modules\@nestjs\platform-express\adapters\express-adapter.js:45:32)
at NestApplication.listen (C:\...\node_modules\@nestjs\core\nest-application.js:146:26)
at async bootstrap (C:\...\dist\main.js:7:5)
(node:45472) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:45472) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
In addition here is the /dist/main.js file in its present form (I have added those try / catch -statements).
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@nestjs/core");
const app_module_1 = require("./app.module");
async function bootstrap() {
try {
const app = await core_1.NestFactory.create(app_module_1.AppModule);
await app.listen(parseInt(process.env.PORT) || 8080)
.catch((e) => {
console.error(e);
process.exit(1);
});
console.log('App is running in port: ' + process.env.PORT);
}
catch (error) {
console.log("Error message: " + error);
}
}
bootstrap().catch(e => {
console.error(e);
});
//# sourceMappingURL=main.js.map
As suggested by Bruce Zhang in his comment I will post the solution I found by myself.
So, the cause of the error was quite silly indeed.
1.) Using Plesk controlling panel (in IIS-server), irrespective of whether the NestJs application is deployed to site root or sub folder (like the default dist folder ), it is essential to get path settings to project to refer correctly:
Now, in the image above, path settings to project are correct when deployed to sub folder (i.e. default build folder dist). My error was that path reference for the Application Startup File was "/dist/main.js" as it should be just main.js.
After this simple correction app started normally with default IIS-port (port 80 and for SSL-protected connection 443). The main.ts -file does not have to anything else than just the following with the IIS default port fetched automatically via process.env.PORT:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT);
}
bootstrap();
2.) The second simple thing that must be correctly configured is web.config (and this was originally correct in my case):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="dist/main.js" verb="*" modules="iisnode" />
</handlers>
<iisnode nodeProcessCommandLine="C:\Program Files\nodejs\node.exe">
</iisnode>
<rewrite>
<rules>
<rule name="nestjs">
<match url="/*" />
<action type="Rewrite" url="main.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I hope this help those who might get confused with IISnode settings in Plesk. And these settings apply generally to all Node.js applications, they are not limited to NestJs which is just upper level framework on node.js, and particularly much on Express. But error messages in IISNode can sometimes be a bit misleading and it takes some pain to see through them.
NestJs is really awesome and it is great that those who have to, or love to, use IIS can easily deploy super modern NestJs backend apps to IIS-server, thanks to those who implemented IISnode.