Recently, I've become unable to run the JavaScript Debug Terminal in VS Code for my Node.js projects. I have a number of different projects all built on the same base, and trying to run npm start
in a debug terminal for any of them gives me the following error:
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module '"c:/Users/malcolm.mccrimmon/AppData/Local/Programs/Microsoft'
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)
Waiting for the debugger to disconnect...
Running npm start
from any other, non-debug terminal works just fine. Searching for this problem has only turned up a few issues with debugging projects that have been recently moved, or for which dependencies have not been installed correctly (e.g. vscode Debugger Cannot find module and internal/modules/cjs/loader.js:582 throw err).
I'm using a local (User) installation of VS Code and C:\Users\malcolm.mccrimmon\AppData\Local\Programs\Microsoft VS Code
is the installation directory, so it looks like it may be an issue with a space in the file path not being escaped correctly? I have no idea how to fix it.
I thought maybe it was a bug introduced in a recent update to VS Code, but I tried downgrading all the way to the January 2021 build (v 1.53) and was still able to reproduce it. A full uninstall and reinstall also failed to resolve the issue. At this point I'm not sure what else to try.
Below is one of the simpler package.json files among the projects I've reproduced this issue in:
{
"name": "metrics-graphql",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon -L index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"node": "10.16"
},
"dependencies": {
"apollo-boost": "^0.4.9",
"apollo-cache-inmemory": "^1.6.6",
"apollo-client": "^2.6.10",
"apollo-link-http": "^1.5.17",
"apollo-server": "^2.25.3",
"apollo-server-express": "^2.25.3",
"axios": "^0.19.2",
"dataloader": "^2.0.0",
"dotenv": "^8.6.0",
"elasticsearch": "^16.7.2",
"graphql": "^14.7.0",
"graphql-iso-date": "^3.6.1",
"graphql-redis-subscriptions": "^2.4.2",
"graphql-resolvers": "^0.4.2",
"graphql-subscriptions": "^1.2.1",
"https-proxy-agent": "^5.0.0",
"ioredis": "^4.28.2",
"moment": "^2.29.1",
"mongoose": "^5.13.13",
"node-fetch": "^2.6.6"
}
}
This sounds like the same issue as reported in https://github.com/microsoft/vscode-js-debug/issues/543.
To boot, there's even someone with a very similar error message as yours there: https://github.com/microsoft/vscode-js-debug/issues/543#issuecomment-656499137.
One of the maintainers there found the following steps that could reproduce the problem (though this doesn't necessarily mean these are the only steps that can reproduce it):
Verification steps:
- Use Node 10. nvm or a dev container would work well
- Have a launch config as linked where the runtimeExecutable is set to npm, yarn, or pnpm
- Verify that you can successfully run and debug scripts
That issue was purported to be fixed for version 1.47.2 of VS Code, but others in the thread there reported still experiencing it afterward.
Several users there have had success getting rid of the error by restarting VS Code.
Another user reported that the issue went away after they reinstalled NVM/Node and NPM.
I suggest you try those workarounds.
For your learning, I found this by googling "Error: Cannot find module" "/AppData/Local/Programs/Microsoft" internal
.
I also notice that in your package.json, you have "devDependencies": {"node": "10.16"},
, which might mean you need to follow that same maintainer's instructions in a separate but slightly related GitHub issue (#911):
It looks like you have a copy of Node.js installed in ./node_modules/.bin/node -- and since you're running with npm, we fall back and see Node there.
I recommend either:
- Uninstalling the copy of Node.js from your node_modules if it's not actually the version of node you want to run with
- Configure npm to use the copy of Node.js present in your node_modules (e.g. install npm locally as well and invoke that)
- Manually set nodeVersionHint: 10 to indicate you're running with the old version of Node.js
That same maintainer also gave this comment (2023-02-08):
Node 10 has been at end of life for nearly three years now; we no longer explicitly support it for the debug terminal or auto attach. Upgrading to any version of Node past Node 10 will resolve the issue; Node 18 is the current stable LTS release.