Search code examples
node.jsbashsshnpmcygwin

Error occurred npm command via ssh (Cygwin)


On my MacOSX (10.11.6), I tried to execute npm ls command to Cygwin's bash (Windows10) via ssh, but below error occurred.

# npm ls
Error: Failed to replace env in config: ${APPDATA}
   at C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:418:13
   at RegExp.[Symbol.replace] (native)
   at RegExp.[Symbol.replace] (native)
   at String.replace (native)
   at envReplace (C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:414:12)
   at parseField (C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:392:7)
   at C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:335:17
   at Array.forEach (native)
   at Conf.add (C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:334:23)
   at ConfigChain.addString (C:\Program Files\nodejs\node_modules\npm\node_modules\config-chain\index.js:244:8)

Error: Failed to replace env in config: ${APPDATA}
   at C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:418:13
   at RegExp.[Symbol.replace] (native)
   at RegExp.[Symbol.replace] (native)
   at String.replace (native)
   at envReplace (C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:414:12)
   at parseField (C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:392:7)
   at C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:335:17
   at Array.forEach (native)
   at Conf.add (C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:334:23)
   at ConfigChain.addString (C:\Program Files\nodejs\node_modules\npm\node_modules\config-chain\index.js:244:8)

So I tried to execute other npm commands (ex. npm install, etc.), but the result was same error. If I executed npm commands on Cygwin console, it worked normally. Moreover, on MacOSX I executed a command other than npm to Cygwin via ssh, it also looked fine. (When I login via ssh, Cygwin cannot load some paths normally...?) What is the reason for this?

Info.

# node -v
v6.9.4

# npm -v
Error: Failed to replace env in config: ${APPDATA}
    at C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:418:13
    at RegExp.[Symbol.replace] (native)
    at RegExp.[Symbol.replace] (native)
    at String.replace (native)
    at envReplace (C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:414:12)
    at parseField (C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:392:7)
    at C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:335:17
    at Array.forEach (native)
    at Conf.add (C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:334:23)
    at ConfigChain.addString (C:\Program Files\nodejs\node_modules\npm\node_modules\config-chain\index.js:244:8)

3.10.10

# uname -rv
2.6.1(0.305/5/3) 2016-12-16 11:55

Solution

  • When you connect to Cygwin's SSH server, you don't get a login environment in the same way that you do when you open a console with your own existing logon session. Apparently, this is an insurmountable limitation. One of the developers writes:

    The child process started by sshd inherits its minimal environment from its sshd server parent process. The sshd server is running under another account and so has another set of environment variables. The setuid() call does not change the environment. There's no mechanism to fetch a user-specific environment.


    The reason you are seeing this error is because you have something referencing ${APPDATA} in your config (~/.npmrc). You can either change the config to not rely on this variable, or you can set the variable in your SSH session, for example:

    export APPDATA="$SYSTEMDRIVE\\Users\\$USERNAME\\AppData\\Roaming"