'VS Code Integrated Terminal Throws `Cannot find module` & `nvm is not compatible with the npm config "prefix" option`

It looks like this is a fairly common issue, but I've tried everything I can find and still can't seem to resolve it. If I load my terminal outside of VS Code it works fine, but the integrated terminal keeps throwing this when it starts:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module '"/Users/me/Library/Application'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at Module._preloadModules (internal/modules/cjs/loader.js:901:12)
    at preloadModules (internal/bootstrap/node.js:601:7)
    at startup (internal/bootstrap/node.js:273:9)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
nvm is not compatible with the npm config "prefix" option: currently set to ""
Run `nvm use --delete-prefix v10.16.3 --silent` to unset it.

I worked through the steps here, which explain how to remove additional unwanted instances of npm/npx. The strange thing is that now which npm prints /Users/me/.nvm/versions/node/v10.16.3/bin/npm before the nvm initialization script, which makes it seem like nvm has been loaded properly, but the .zshrc file is being run again, which might be causing the errors.

It looks like there's an extra " in the file path in the error message ('"/Users/me/Library/Application'), but I don't see any typos like that in my nvm initialization script, so it seems like maybe it's also a result of the initialization script being called twice?

Something else of note is that node and npm can't be found in the integrated terminal, but they work fine outside of VS Code.

I've also tried various other things like setting this in my VS Code settings: "terminal.integrated.shellArgs.osx": [], uninstalling and reinstalling node/nvm both manually and with homebrew. Any help would be greatly appreciated!

Edit: I tried installing VS Code Insiders and copying over my settings and the integrated terminal loads without any issues. So it seems like this might be fixed in a future release. I'll post an update after the update is available in VS Code to see if this issue goes away.



Solution 1:[1]

I had this problem and I was just able to fix my issues by disabling auto attaching for debug in the VSCode settings. Hopefully I can re-enable this in the future since you mentioned there aren't issues with the latest insider release. I'm using a Bash shell by the way but hopefully this will help you as well.

"debug.node.autoAttach": "disabled",

Solution 2:[2]

I tried the accepted solution however it didnt work for me.

I had to use nvm to uninstall my versions, then just use nvm and install. after that I didnt have the error.

Solution 3:[3]

I recently ran into this issue while attempting to debug a Next.JS node project. The suggested answer did not work for me as there was an issue with the debugger connecting to the node instance I was trying to run. I did 2 things to overcome the issue.

I use nvm for managing node versions. My default is node v10 but I was running my Next.js project using node v16.

I went to the Next.JS website to look up their documentation for Debugging and setup a launch.config in the .vscode folder using the suggested configuration that allows for Client Side, Server Side, or Full Stack Debugging. Note that Client Side debugging is expecting Chrome DevTools.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: debug server-side",
      "type": "node-terminal",
      "request": "launch",
      "command": "npm run dev"
    },
    {
      "name": "Next.js: debug client-side",
      "type": "pwa-chrome",
      "request": "launch",
      "url": "http://localhost:3000"
    },
    {
      "name": "Next.js: debug full stack",
      "type": "node-terminal",
      "request": "launch",
      "command": "npm run dev",
      "console": "integratedTerminal",
      "serverReadyAction": {
        "pattern": "started server on .+, url: (https?://.+)",
        "uriFormat": "%s",
        "action": "debugWithChrome"
      }
    }
  ]
}

Unfortunately the debugger still wasn't able to attach. While the debugger was still running, I ran node -v to see that the debugger launched a new terminal window using the default node version, which was node v10. Once I changed the version in the Debugger terminal to be node v16 and then I ran yarn dev I was able to attach the debugger.

For me I will always be double checking or setting a default node version for a directory that I'm working in to prevent this from happening again.

Solution 4:[4]

MacOS version:10.15.6

  1. $ cd ~
  2. $ touch .zshrc
  3. $ vi .zshrc
export NODE_PATH="/usr/local/lib/node_modules"
  1. $ source .zshrc

Solution 5:[5]

Try running this command in the Visual Studio Code terminal:

nvm use --delete-prefix v12.20.0 --silent

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bergy
Solution 2 JohnnyBravo
Solution 3 Andrew Konken
Solution 4 xiaojun996
Solution 5 the Tin Man