VSCode 1.23版本调试Node.js时出现模块缺失及语法错误问题
Hey there, let's work through these VSCode debugging issues you're having post-upgrade to 1.23. I've seen similar problems before, so here's a structured approach to fix them:
1. Fix Cannot find module 'bufferutil' & Workspace Path Mismatch
The main culprit here is likely that the debugger isn't running from your project's root directory, so it can't resolve all node_modules correctly. Let's tweak your launch.json first:
Update launch.json to Set Correct Context
Add the cwd field to force the debugger to use your project root, and optionally specify the local Node executable if you're using a project-specific version (like via nvm or local install):
{ "type": "node", "request": "launch", "name": "Launch Program", "program": "${workspaceFolder}/src/test/bin/www", "cwd": "${workspaceFolder}", // Ensures debugger uses your project root as working directory "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/node" // Use local Node to match your manual run environment }
Reinstall Dependencies to Fix Partial Module Issues
Even though manual runs work, the debugger might be pulling from a corrupted cache. Run these commands in your project root:
rm -rf node_modules package-lock.json npm install
2. Fix Jade's Unexpected token ) Syntax Error
Since this only happens during debugging, the issue is probably a mismatch in how Jade compiles when the debugger spins up the process:
- Upgrade Jade for Compatibility: Older Jade versions might have syntax parsing bugs with newer Node.js. Update it with:
npm install jade@latest --save - Mirror Manual Run Environment Variables: The debugger might be using a different
NODE_ENVor other vars that affect Jade's compilation. Add this to yourlaunch.json:"env": { "NODE_ENV": "development", "PATH": "${env:PATH}" }
3. Rule Out VSCode 1.23 Debugger Regression
Since your setup worked before the upgrade, this could be a bug in the 1.23 version's Node debugger:
- Downgrade VSCode Temporarily: Roll back to the previous version where debugging worked to confirm if the issue is version-specific.
- Update the Node Debug Extension: Check the Extensions tab in VSCode to make sure the built-in Node.js debug extension is up to date.
Quick Recap
The key difference between "Auto Attach" and "Launch Program" is that auto attach hooks into a process already running with your project's correct environment, while launch spins up a new process with its own context. The fixes above align that launch context with what you use for manual runs.
内容的提问来源于stack exchange,提问作者perrypel

