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: https://github.com/JohannesMerkt/spawn-event-emitter-issue
This is the project that is trying to use the package via npm: https://github.com/JohannesMerkt/spawn-event-emitter-issue-package-test

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:
        print("Message")
        sys.stdout.flush()
        time.sleep(1)

if __name__ == "__main__":
    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() {
    super();
    this.pythonCode = null;
  }

  //Start python code and listen to the output of the code
  start() {
    this.pythonCode = spawn("python", ["pythonCode.py"]);
    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.stdout.removeAllListeners("data");
      this.pythonCode.stdin.pause();
      this.pythonCode.kill();
      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.start();

moduleObject.on("data", data => {
console.log(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.start();

moduleObject.on("data", data => {
console.log(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", ["pythonCode.py"], { cwd: __dirname })
1 Like

Thanks a lot! That was the issue.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.