ARTICLE AD BOX
I am trying to debug a TypeScript program using Visual Studio Code. I can run and debug automated tests that import locally-defined modules using the interactive debugger, but trying to run a simple script that imports any locally-defined module fails. I get Uncaught Error Error: Cannot find module '/path/to/project/src/importme' imported from /path/to/project/src/program.ts. If I don't import any locally-defined modules at all, the program runs and I can set breakpoint and use other interactive debugger features. How can I get that working with a file that imports said modules?
program.ts, with commented out import statement generated by Visual Studio code that works when debugging automated tests, noting that importing modules that way works when debugging tests:
// import { ImportMe } from "./importme"; const main = () => { // const importMe = new ImportMe(); console.log("done"); } main();An example of a test in the tests directory that can be debugged interactively:
import { ImportMe } from "../src/importme"; describe("test suite", () => { it("works", () => { const importMe = new ImportMe(); } })launch.json:
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Debug main.ts", "program": "${workspaceFolder}/src/main.ts", "preLaunchTask": "tsc: build - tsconfig.json", "outFiles": [ "${workspaceFolder}/out/**/*.js" ], }, { "type": "node", "request": "launch", "name": "Debug Jest Tests", "runtimeArgs": [ "--inspect-brk", "${workspaceRoot}/node_modules/.bin/jest", "--runInBand", "--coverage", "false" ], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen" } ] }jest.config.js:
const { createDefaultPreset } = require("ts-jest"); const tsJestTransformCfg = createDefaultPreset().transform; /** @type {import("jest").Config} **/ module.exports = { testEnvironment: "node", testRunner: "jest-jasmine2", transform: { ...tsJestTransformCfg, }, };package.json:
{ "dependencies": { "@types/jest": "^30.0.0", "@types/node": "^25.3.3", "@types/random": "^2.1.1", "jest": "^29.7.0", "jest-jasmine2": "^29.7.0", "random": "^5.4.1", "ts-jest": "^29.4.6" }, "scripts": { "test": "jest" } }tsconfig.json:
{ "compilerOptions": { "target": "esnext", "module": "nodenext", "outDir": "out", "sourceRoot": "src", "sourceMap": true, "moduleResolution": "nodenext", }, }Versions:
% tsc --version Version 5.9.3 % node --version v23.11.0 % npm --version 10.9.2The closest question I see here on Stack Overflow is this one which has no answers and is 8 years old. The other questions I see are about modules defined in installed packages.
