3

I'm trying to compile a typescript into JavaScript using tsc node package module. First of all, I've installed the module using npm install -g typescript. In my local directory I've created a file called classes.js containing a valid typescript code. When running tsc classes.js I get the following error: Error reading file "./classes.js": File not found

The error doesn't make much sense, since the file exist. Same error is shown when the absolute file path is used. I'm wondering if there is something wrong with tsc module or am I missing something?

2 Answers 2

3

The typescript compiler specifically looks for extensions .str and .ts. Here is the code that resolves input file names:

if(!TypeScript.isSTRFile(normalizedPath) && !TypeScript.isTSFile(normalizedPath)) {
    normalizedPath += ".ts";
}  

The compiler then looks for a file with name normalizedPath, which in your case corresponds to classes.js.ts, which does not exist. In my opinion, the compiler should output a better error message here.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't see why a tool would care about the extension of the file it should compile. Also, it doesn't output to the console the compiled js when the --out file is not used.
Hmm, I think in most cases that actually makes sense. Consider for example a C compiler which has to deal with .h and .c files. I just asked the javac compiler to compile a file Test.c which it refused to do.
The file extension matters because the top-level grammar of a .d.ts file is different from the grammar for .ts. Arguably the compiler should assume .ts given no other information, but as @Valentin notes, it's not really standard behavior for most compilers.
0

Apparently the tsc node module works only when the compiled typescript file has ts extension. I believe this is a temporary limitation which could be fixed in future versions of tsc.

1 Comment

I am using 0.9.1.1 and it is yet to be fixed. c:\Temp>tsc --declaration foo.js gives me the thoroughly misleading: error TS5007: Cannot resolve referenced file: 'foo.js'. when foo.js is there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.