Search code examples
node.jsnpmpackagepackage.jsonnpm-package

Including a command from a C library on my npm package


I have a .exe file from a C library that my package depends. My package works fine when the user already have this command included on his PATH. But there is any way of when the user install my package, NPM also install this command from this C library? I tried to include the .exe file on the bin on package.json and when a install my package globally this command is available for the prompt. But isn't available for the child_process.spawn. When my code tries to use it I receive an error:

Error: spawn fpcalc ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (internal/child_process.js:273:12)
at onErrorNT (internal/child_process.js:469:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn fpcalc',
path: 'fpcalc',

// EDIT

Basically I need to set a PATH variable when my package installs, so the command "fpcalc" will be available for the child_process


Solution

  • I found a solution to my problems. Actually including a .exe on package.json works! The problem was actually with the method child_process.spawn. For some reason, it doesn't execute external commands, at least as I tried. So I ended up using the child_process.exec and works fine.

    // package.json
    {
     "bin": {
          "your-command": "file_path.exe"
     }
    }
    

    By the way, I created a library that implements a C library to recognize music. See more here.