Search code examples
node.jsnode-modulesnvm

Why doesn't nvm swap in/out the user roaming/npm folder when we switch versions?


Introduction

So, I am using Nvm to switch between Node.js 8 and Node.js 14, and this is making me suffer.

Whenever I switch between versions, I have incompatibilities and issues in some of my global tools/modules. E.g., when I work on a legacy project in Node.js 8 everything is fine.

If I switch to Node.js 14, and install some tools from there...then switch back... I get various errors, e.g.,

λ rimraf
C:\Users\bak\AppData\Roaming\npm\node_modules\rimraf\dist\esm\bin.mjs:2
import { readFile } from 'fs/promises';
^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3

I need to uninstall and reinstall rimraf to get it to work again (using an older version):

npm uninstall -g rimraf
npm install -g [email protected]

And then, when switching back to 14, it is again the same.

Question

So questions:

  1. Why doesn't nvm do this automatically?
  2. Is it normal to have this problem when switching versions? I find it weird that they would make nvm to switch Node.js versions... and not consider swapping in/out the Node.js modules.

Other considerations:

I'm considering writing a script to move my 'AppData\Roaming\npm\node_modules' folder when I switch versions. Basically, swap in, swap out.

Would this work well?


Solution

  • From the comments, I started suspecting that maybe residual settings were left from my initial Node.js installation.

    λ npm config list
    ; builtin config undefined
    prefix = "C:\\Users\\bak\\AppData\\Roaming\\npm"
    

    And indeed, running config list shows that a prefix is being set to the Appdata/Roaming folder.