src/find-npm.js
function _getPathFromPowerShell () {
  return new Promise(resolve => {
    const psArgs = 'Get-Command npm | Select-Object -ExpandProperty Definition'
    const args = [ '-NoProfile', '-NoLogo', psArgs ]
    const child = spawn('powershell.exe', args)

    let stdout = []
    let stderr = []

    child.stdout.on('data', (data) => {
      debug(`PowerShell: Stdout received: ${data.toString()}`)
      stdout.push(data.toString())
    })

    child.stderr.on('data', (data) => {
      debug(`PowerShell: Stderr received: ${data.toString()}`)
      stderr.push(data.toString())
    })

    child.on('exit', () => {
      const cmdPath = (stdout[0] && stdout[0].trim) ? stdout[0].trim() : null

      if (stderr.length === 0 && cmdPath && cmdPath.slice(cmdPath.length - 7) === 'npm.cmd') {
        // We're probably installed in a location like C:\Program Files\nodejs\npm.cmd,
        // meaning that we should not use the global prefix installation location
        const npmPath = cmdPath.slice(0, cmdPath.length - 8)
        debug(`PowerShell: _getPathFromPowerShell() resolving with: ${npmPath}`)
        resolve(npmPath)
      } else {
        resolve(null)
      }
    })

    child.stdin.end()
  })
}
function _getPathFromPowerShell () {
  return new Promise(resolve => {
    const psArgs = 'Get-Command npm | Select-Object -ExpandProperty Definition'
    const args = [ '-NoProfile', '-NoLogo', psArgs ]
    const child = spawn('powershell.exe', args)

    let stdout = []
    let stderr = []

    child.stdout.on('data', (data) => {
      debug(`PowerShell: Stdout received: ${data.toString()}`)
      stdout.push(data.toString())
    })

    child.stderr.on('data', (data) => {
      debug(`PowerShell: Stderr received: ${data.toString()}`)
      stderr.push(data.toString())
    })

    child.on('exit', () => {
      const cmdPath = (stdout[0] && stdout[0].trim) ? stdout[0].trim() : null

      if (stderr.length === 0 && cmdPath && cmdPath.slice(cmdPath.length - 7) === 'npm.cmd') {
        // We're probably installed in a location like C:\Program Files\nodejs\npm.cmd,
        // meaning that we should not use the global prefix installation location
        const npmPath = cmdPath.slice(0, cmdPath.length - 8)
        debug(`PowerShell: _getPathFromPowerShell() resolving with: ${npmPath}`)
        resolve(npmPath)
      } else {
        resolve(null)
      }
    })

    child.stdin.end()
  })
}
function _getPathFromPowerShell ( ) { return new Promise ( resolve => { const psArgs = 'Get-Command npm | Select-Object -ExpandProperty Definition' const args = [ '-NoProfile' , '-NoLogo' , psArgs ] const child = spawn ( 'powershell.exe' , args ) let stdout = [ ] let stderr = [ ] child . stdout . on ( 'data' , ( data ) => { debug ( ` ${ data . toString ( ) } ` ) stdout . push ( data . toString ( ) ) } ) child . stderr . on ( 'data' , ( data ) => { debug ( ` ${ data . toString ( ) } ` ) stderr . push ( data . toString ( ) ) } ) child . on ( 'exit' , ( ) => { const cmdPath = ( stdout [ 0 ] && stdout [ 0 ] . trim ) ? stdout [ 0 ] . trim ( ) : null if ( stderr . length === 0 && cmdPath && cmdPath . slice ( cmdPath . length - 7 ) === 'npm.cmd' ) { // We're probably installed in a location like C:\Program Files\nodejs\npm.cmd, // meaning that we should not use the global prefix installation location const npmPath = cmdPath . slice ( 0 , cmdPath . length - 8 ) debug ( ` ${ npmPath } ` ) resolve ( npmPath ) } else { resolve ( null ) } } ) child . stdin . end ( ) } ) }
Attempts to get npm's path by calling out to "Get-Command npm"

@returns {Promise.<string>} - Promise that resolves with the found path (or null if not found)
Attempts to get npm s path by calling out to Get - Command npm