Open twigs67 opened 3 years ago
This seems to be related to this part of Node.js: https://github.com/nodejs/node/blob/5ce015ec72be98f064041d1bf5c3527a89c276cc/lib/events.js#L422-L429
It looks like your code is trying to add an event listener to undefined
... Did this error appeared after an upgrade of a Node.js version? If so, could you share the version number you were using before? If not, it's more likely a bug in your code (or one dependency you're using) and not a bug inn Node.js, and we should transfer this issue to nodejs/help.
@aduh95 We did not. It's being deployed in docker. Ok, I'll post it there. Thanks for the prompt response.
Wait @Mesteery @aduh95 can we reopen this? I mistakenly opened that duplicat.
It's already open. By the way, could you share this line /usr/src/app/dist/src/core/node-cluster.js:40
so we can more easily help you?
@Mesteery
let AppClusterService = class AppClusterService {
static clusterize(callback) {
if (cluster_1.isMaster) {
console.log(`Master server started on ${process.pid}`);
for (let i = 0; i < numCPUs; i++) {
(0, cluster_1.fork)();
}
(0, cluster_1.on)('exit', (worker, code, signal) => { // line 40
console.log(`Worker ${worker.process.pid} died. Restarting`);
(0, cluster_1.fork)();
});
}
else {
console.log(`Cluster server started on ${process.pid}`);
callback();
}
}
};
What's the reason for using (0, cluster_1.on)(
instead of cluster_1.on(
? That's the cause of your error as this syntax doesn't set the this
of the method to the expected instance:
class Foo{ bar(){ return this } }
let foo = new Foo;
console.log(foo.bar() === foo); // true
console.log((0, foo.bar)() === undefined); // true
@aduh95 you were correct. It was the node-cluster that was causing the issue. We have just removed it for now, but we would like to get this working. This is the original code (code snippet above is the compiled version).
// node-cluster.ts
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@services';
import * as cluster from 'cluster';
import * as os from 'os'
const numCPUs = os.cpus().length;
@Injectable()
export class ClusterService {
static clusterize(callback: Function): void {
if (cluster.isMaster && !ConfigService.isDevEnvironment()) {
console.log(`MASTER SERVER (${process.pid}) IS RUNNING `);
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
callback();
}
}
}
Any idea how I might fix this?
Maybe something related to @Injectable()
? Maybe there's an option you can add to disable this behavior?
Looks like when transpiler (babel in my case) tries to output commonjs modules, we end up with the code that loses this
.
F.e. the following code...
import { on } from 'process';
on('beforeExit', () => console.log('exit'));
...are getting transpiled to this:
"use strict";
var _process = require("process");
(0, _process.on)('beforeExit', () => console.log('exit'));
Here is the example in babel playground
Is this issue still occuring? If not, feel free to self-close.
Version
14.4.0
Platform
linux
Subsystem
No response
What steps will reproduce the bug?
Hello,
Apologies if this isn't the correct place for this, but I'm completely lost as to why I'm getting this error. events=target._events does not exist anywhere in my codebase, so i'm guessing it is due to a node_module, but I can't find the culprit.
Any help would be greatly appreciated.
How often does it reproduce? Is there a required condition?
No response
What is the expected behavior?
No response
What do you see instead?
No response
Additional information
No response