Windows10下VSCode运行TypeScript报错:找不到对应JavaScript文件
Let's walk through resolving this error step by step. The core issue here is that TypeScript isn't compiling to JavaScript properly, or your VS Code configurations aren't pointing to the right paths for the compiled files.
1. Fix Your tsconfig.json Location & Configuration
First, your tsconfig.json should live in your project root directory (E:\typescriptBasis), not inside the .vscode folder. This ensures the TypeScript compiler automatically picks it up. Let's update it to specify an output directory for compiled JS files (cleaner and easier to reference):
{ "compilerOptions": { "target": "es5", "module": "commonjs", "sourceMap": true, "outDir": "./dist" // Compiles JS to a /dist folder }, "include": ["**/*.ts"] // Ensures all .ts files in your project are compiled }
2. Correct tasks.json Paths
Your tasks are pointing to a tsconfig.json inside .vscode, which we just moved. Update the paths to reference the root directory config, and add clear labels for clarity:
{ "version": "2.0.0", "tasks": [ { "type": "typescript", "tsconfig": "./tsconfig.json", "option": "watch", "problemMatcher": ["$tsc-watch"], "label": "tsc: watch" // Auto-recompiles when you edit .ts files }, { "type": "typescript", "tsconfig": "./tsconfig.json", "problemMatcher": ["$tsc"], "group": { "kind": "build", "isDefault": true }, "label": "tsc: build" // One-time compilation for debugging } ] }
3. Update launch.json to Link Compiled Files
We need to make sure the debugger knows where to find the compiled JS, and we'll add a pre-launch task to auto-compile before debugging:
{ "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "sourceMaps": true, "name": "Launch Program", "program": "${workspaceFolder}/main.ts", "outFiles": ["${workspaceFolder}/dist/**/*.js"], // Points to our /dist output folder "preLaunchTask": "tsc: build" // Runs compilation automatically before debugging } ] }
4. Verify Compilation Works Manually
Before debugging, open the VS Code terminal and run:
tsc
Check if a dist folder appears with main.js and main.js.map inside. If not:
- Run
tsc -vto confirm your global TypeScript installation (v2.8.3) is working. - Double-check that
main.tsis in your project root (E:\typescriptBasis) so theincluderule intsconfig.jsonpicks it up.
5. Test Debugging
Now press F5 to start debugging. The pre-launch task will compile your TypeScript first, and the debugger should find the corresponding JS file in the dist folder. You'll see your console log output in the debug terminal.
内容的提问来源于stack exchange,提问作者Anil Avhad

