Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path issue on windows using NVM to manage node, "ERROR: Cannot find npm binary" #851

Closed
evelant opened this issue Sep 17, 2020 · 3 comments
Labels

Comments

@evelant
Copy link

evelant commented Sep 17, 2020

Describe The Bug:
When using nvm on windows logs show "ERROR: Cannot find npm binary"

I think this is due to this function here https://github.com/oznu/homebridge-config-ui-x/blob/45a49188e2cadf52857314bb8620d594e1bb6cc7/src/modules/plugins/plugins.service.ts#L639

It checks a couple set paths on windows but does not check the $PATH environment variable. When using NVM the node/npm installation is on the path but not at any of the fixed locations checked by that function. I think this could be quickly resolved by also just checking if npm is on the path when using windows.

To Reproduce:
Install node via nvm windows, install homebridge-config-ui-x and service. Run it and see this error in the logs.

Expected behavior:
homebridge-config-ui-x should always check the path for binaries, even on windows.

Logs:

https://github.com/oznu/homebridge-config-ui-x/blob/45a49188e2cadf52857314bb8620d594e1bb6cc7/src/modules/plugins/plugins.service.ts#L639

Homebridge Config:

N/A

Screenshots:
N/A

Environment:

  • Node.js Version: 12.18.3
  • NPM Version: 6.14.6
  • Homebridge Version: 1.2.2
  • Homebridge Config UI X Version: 4.27.1
  • Operating System: Windows
  • Process Supervisor: none
@evelant evelant added the bug label Sep 17, 2020
@evelant
Copy link
Author

evelant commented Sep 17, 2020

I tried to build locally to make a fix for a PR but I couldn't get config-ui-x to build on windows with the instructions in CONTRIBUTING.MD

There are two ways to fix this that I see, one is

npm install --save command-exists

plugins.service.ts

import { sync as commandExistsSync } from 'command-exists';

....
 /**
   * Helper function to work out where npm is
   */
  private getNpmPath() {
    // Check if npm is on the path, regardless of system
    if(commandExistsSync('npm')){
      const npmPath = ['npm'];
      if (os.platform() === 'win32'){
        // Add global if on Windows
        npmPath.push('-g');
      }
      return npmPath;
    }

    if (os.platform() === 'win32') {

      // if npm not on path and running on windows find the full path to npm in some common locations
      const windowsNpmPath = [
        path.join(process.env.APPDATA, 'npm/npm.cmd'),
        path.join(process.env.ProgramFiles, 'nodejs/npm.cmd'),
      ]
        .filter(fs.existsSync);

      if (windowsNpmPath.length) {
        return [windowsNpmPath[0], '-g'];
      } else {
        this.logger.error(`ERROR: Cannot find npm binary. You will not be able to manage plugins or update homebridge.`);
        this.logger.error(`ERROR: You might be able to fix this problem by running: npm install -g npm`);
      }

    }
    // Fallback if commandExistsSync somehow didn't find it, Linux and macOS don't require the full path to npm
    return ['npm'];
  }

OR

add NVM_SYMLINK to the windows paths searched

plugins.service.ts

 // if npm not on path and running on windows find the full path to npm in some common locations
      const windowsNpmPath = [
        path.join(process.env.APPDATA, 'npm/npm.cmd'),
        path.join(process.env.ProgramFiles, 'nodejs/npm.cmd'),
        path.join(process.env.NVM_SYMLINK, 'npm.cmd'),
      ]
        .filter(fs.existsSync);

@evelant
Copy link
Author

evelant commented Sep 17, 2020

I worked around the issue for now with a symlink, from an administrator cmd prompt mklink /J "C:\Program Files\nodejs" C:\path\to\nvm\nodejs

@oznu oznu closed this as completed in a86a5b4 Sep 25, 2020
@oznu
Copy link
Member

oznu commented Sep 25, 2020

Thanks, fixed with the NVM_SYMLINK solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants