npm Community Forum (Archive)

The npm community forum has been discontinued.

To discuss usage of npm, visit the GitHub Support Community.

NPM Package behaves differently from using code directly

I have spend the entire day trying to figure out why my package isnt running as expected, but found no solution to my problem.

I am trying to create a package that executes python code, listens to its continuous stdout responses and emits events when they are recieved.

Testing the code inside the package itself works perfektly fine, but once I publish the package to npm and try to use the package inside a different project it stops immediatly and no events are triggered.

I have created to repositories to simulate the issue im having.

This is the package:
This is the project that is trying to use the package via npm:

Inside the package I have this python code im trying to listen to:

import sys
import time

# sends a message every second
def main():
    while True:

if __name__ == "__main__":

Its really simple code that sends a message every second

My package module file index.js looks like this:

const EventEmitter = require("events").EventEmitter;
const spawn = require("child_process").spawn;

module.exports = class BalanceBoard extends EventEmitter {
  constructor() {
    this.pythonCode = null;

  //Start python code and listen to the output of the code
  start() {
    this.pythonCode = spawn("python", [""]);
    this.pythonCode.stdout.on("data", data => {
      //Process data would be here ...
      //Emit data
      this.emit("data", data.toString().trim());

  //Stop python code
  stop() {
    if (this.pythonCode != null) {
      this.pythonCode = null;

I also have a test.js file to test if the code is working inside the package:

const ModuleClass = require("./index");

var moduleObject = new ModuleClass();


moduleObject.on("data", data => {

It works as expected and logs “Message” every second

Now I have created a different project and installed the package with npm i -s

The test.js file within the project has basically the same code as the other test.js file just the require has been changed:

const ModuleClass = require("spawn-event-emitter-issue");

var moduleObject = new ModuleClass();


moduleObject.on("data", data => {

When I run this it immediatly stops and doesnt log anything.

Do you know if I missed something? I am hoping anyone can help me with this issue.

Thank you for taking your time to try and help me,
Johannes Merkt

I think the current working directory might be wrong when calling spawn. You could try something with __dirname:

    this.pythonCode = spawn("python", [""], { cwd: __dirname })

Thanks a lot! That was the issue.