Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在vscode中调试TypeScript时引用模块时出错_Node.js_Typescript_Visual Studio Code - Fatal编程技术网

Node.js 在vscode中调试TypeScript时引用模块时出错

Node.js 在vscode中调试TypeScript时引用模块时出错,node.js,typescript,visual-studio-code,Node.js,Typescript,Visual Studio Code,我正在尝试为nodejs/typescript项目设置我的vscode开发环境 当我在nodejs中编译和运行时,它工作得很好。但如果我尝试在vscode中调试,它会失败,原因如下: Process exited with code 1 Uncaught Error: Cannot find module './testmod.ts' Require stack: - c:\GitHub\home\bgtrader\build\index.js 但是,如果我从此更改我的要求: const te

我正在尝试为nodejs/typescript项目设置我的vscode开发环境

当我在nodejs中编译和运行时,它工作得很好。但如果我尝试在vscode中调试,它会失败,原因如下:

Process exited with code 1
Uncaught Error: Cannot find module './testmod.ts'
Require stack:
- c:\GitHub\home\bgtrader\build\index.js
但是,如果我从此更改我的
要求

const testmod = require("./testmod.ts");
为此:

const testmod = require("./testmod.js");  // note the change of extension from .ts to .js
然后我可以在vscode中调试,但它不会在nodejs中编译运行

如何让它在nodejs和vscode调试中工作,而不必在调用
require(…)
时始终在
.ts
.js
之间切换文件扩展名

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": ["es6"], 
    "allowJs": true, 
    "sourceMap": true, 
    "outDir": "build", 
    "rootDir": "src", 
    "strict": true,
    "noImplicitAny": true, 
    "esModuleInterop": true, 
    "resolveJsonModule": true, 
    "skipLibCheck": true, 
    "forceConsistentCasingInFileNames": true 
  },
  "include": [ "src/**/*" ]
}
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/src/index.ts",
            "preLaunchTask": "tsc: build - tsconfig.json",
            "outFiles": [
                "${workspaceFolder}/build/*.js"
            ]
        }
    ]
}
launch.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": ["es6"], 
    "allowJs": true, 
    "sourceMap": true, 
    "outDir": "build", 
    "rootDir": "src", 
    "strict": true,
    "noImplicitAny": true, 
    "esModuleInterop": true, 
    "resolveJsonModule": true, 
    "skipLibCheck": true, 
    "forceConsistentCasingInFileNames": true 
  },
  "include": [ "src/**/*" ]
}
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/src/index.ts",
            "preLaunchTask": "tsc: build - tsconfig.json",
            "outFiles": [
                "${workspaceFolder}/build/*.js"
            ]
        }
    ]
}
testmod.ts

const testmod = require("./testmod.ts"); // or .js if I wanna get debug working
console.log(testmod);    
module.exports = "test";