LetMeSOThat4U
LetMeSOThat4U

Reputation: 6758

Starting TypeScript program from Visual Studio Code: "Cannot launch program 'hw.ts' because corresponding JavaScript cannot be found."

I have hw.ts file with this content:

function greeter(x: string) {
    return "Hello" + x;
}

let u = "John";
document.body.innerHTML = greeter(u);

I select Start without debugging and VSCode says:

Cannot launch program 'hw.ts' because corresponding JavaScript cannot be found.

OK so I compile file from the commandline:

tsc hw.ts

Now I do have hw.js in the same folder.

So again I select Start without debugging and VSCode again says Cannot launch program 'hw.ts' because corresponding JavaScript cannot be found..

Is there any way to compile & run TypeScript program from VSCode? What am I missing?

(I do have node in my PATH, it should be visible to VSCode)

Upvotes: 7

Views: 3164

Answers (4)

Yogesh Umesh Vaity
Yogesh Umesh Vaity

Reputation: 48149

I had the same issue. I solved it using the following steps:

1.Configure

Specify output directory and enable sourceMap in tsconfig.json

"outDir": "./out",  /* Specify .js output files. */
"sourceMap": true   /* Generate corresponding .map files. */

2.Build

Now Click Run Build Task (Shift + Command(Ctrl) + B) from the Terminal menu of the VS Code and type the following command and press enter:

tsc: watch - tsconfig.json

You need to Run Build Task once when you first open the project. This will start watching for code changes in the project.

3.Run

Now go to the Typescript program that you want to run (Make sure your program file .ts has the focus).

From the Run menu, click Run Without Debugging(Ctrl + F5).

You can see the output in the Debug Console.

Upvotes: -1

kellsaro
kellsaro

Reputation: 26

In your launch.json file replace

"outFiles": ["${workspaceFolder}/**/*.js"]

for

"outFiles": ["${fileDirname}/**/*.js"]

It worked for me.

Upvotes: 0

Linus
Linus

Reputation: 4783

It worked for me as soon as I changed

  "outFiles": [
    "./dist/**/*.js"
  ],

to

  "outFiles": [
    "${workspaceFolder}/dist/**/*.js"
  ],

Upvotes: 3

HaaLeo
HaaLeo

Reputation: 11722

Be sure that you have set the correct path to your *.js files in the launch.json. This can be done by defining the outFiles option. Furthermore, to enable debugging *.ts files you can set sourceMaps to true. This tells vscode that it should try to map the compiled *.js files to the corresponding *.ts files.
Example:

"sourceMaps": true,
"outFiles": [
    "${workspaceFolder}/path/to/your/jsFiles/**/*.js"
] 

Upvotes: 0

Related Questions