Ts-node: TSError: ⨯ Unable to compile TypeScript?

Created on 24 Jul 2016  Â·  14Comments  Â·  Source: TypeStrong/ts-node

I get this weird error by trying to run a simple script:

class Greeter {
  constructor(public greeting: string) { }
  greet() {
    return "<h1>" + this.greeting + "</h1>";
  }
};

var greeter = new Greeter("Hello, world!");

console.log(greeter.greet());

I did:

$ npm install ts-node -g
$ npm install typescript -g
$ ts-node test.ts

And I get the following error:

                          TSError: ⨯ Unable to compile TypeScript
                          examples/echo.ts (2,7): Duplicate identifier 'Greeter'. (2300)
                          at getOutput (/home/arkotek/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:258:17)
                          at /home/arkotek/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:267:16
                          at Object.compile (/home/arkotek/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:403:17)
                          at loader (/home/arkotek/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:289:33)
                          at Object.require.extensions.(anonymous function) [as .ts] (/home/arkotek/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:306:14)
                          at Module.load (module.js:458:32)
                          at tryModuleLoad (module.js:417:12)
                          at Function.Module._load (module.js:409:3)
                          at Function.Module.runMain (module.js:575:10)
                          at Object.<anonymous> (/home/arkotek/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/_bin.ts:179:12)

Most helpful comment

I am also facing this issue while running the test cases for my Aurelia ( + Webpack + TypeScript) app. I have already asked about this in stack overflow: http://stackoverflow.com/questions/41137573/karma-error-config-invalid-config-file. As the TypeScript compilation fails from ts-node, the testing can't proceed further.

I also tried to do a simple ts-node test/unit/setup.ts, and I faced the similar issue with that as well:

C:\Path\to\my\Website>ts-node test/unit/setup.ts

C:\Users\spal\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:312
          throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
                ^
TSError: ⨯ Unable to compile TypeScript
Cannot find type definition file for 'dir1'. (2688)
Cannot find type definition file for 'dir2'. (2688)
Cannot find type definition file for 'dir3'. (2688)
Cannot find type definition file for 'lang1'. (2688)
Cannot find type definition file for 'dir4'. (2688)
Cannot find type definition file for 'lang2'. (2688)
Cannot find type definition file for 'maps'. (2688)
Cannot find type definition file for 'valueConverters'. (2688)
Cannot find type definition file for 'views'. (2688)
    at getOutput (C:\Users\spal\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:312:17)
    at C:\Users\spal\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:343:18
    at Object.compile (C:\Users\spal\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:502:17)
    at Module.m._compile (C:\Users\spal\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:406:44)
    at Module._extensions..js (module.js:579:10)
    at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\spal\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:409:12)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Function.Module.runMain (module.js:604:10)

Any suggestion on what I should look for?

All 14 comments

with [email protected] it works great

Works for me. Are you sure that's the code in use? Can you check if maybe it's a caching issue by running with --no-cache?

I have similar issue. And it works great using 0.9 version.
I use it to compile gulpfile.ts. I have gulpfile.js which requires gulpfile.ts like this:

require('ts-node').register({project: false, disableWarnings: true});
require('./gulpfile.ts');

Stacktrace:

Users/<project>/node_modules/ts-node/src/index.ts:259
          throw new TSError(diagnosticList)
                ^
TSError: ⨯ Unable to compile TypeScript
gulpfile.ts: Emit skipped
    at getOutput (/Users/<project>/node_modules/ts-node/src/index.ts:259:17)
    at /Users/<project>/node_modules/ts-node/src/index.ts:268:16
    at Object.compile (/Users/<project>/node_modules/ts-node/src/index.ts:404:17)
    at loader (/Users/<project>/node_modules/ts-node/src/index.ts:290:33)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/<project>/node_modules/ts-node/src/index.ts:307:14)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)

Also, how to use no-cache here?

@RobertoUa What happens when you don't disableWarnings? There could be some syntactic issue your hiding with that flag.

There are some type errors which I cannot resolve because ts-node (or typescript) doesn't support multiple tsconfigs. So I have to disable type errors.
Anyways, I'll check. But it doesn't explain why it worked with 0.9.3

If you can share code, that's great, otherwise there's not much I can do to help. There's no known changes in code emitted between 0.9 and 1.0.

If I had to guess, it's because you're using the configuration options wrong. In 0.9, project: false was not a thing - it didn't do anything and would have still loaded tsconfig.json from the current working directory. In 1.0, it is a thing (replaces noProject: true from 0.9) and now no tsconfig.json file is being loaded resulting in type errors.

Closing as both users have gone offline. Feel free to continue the discussion if you find out what was going wrong!

I am also facing this issue while running the test cases for my Aurelia ( + Webpack + TypeScript) app. I have already asked about this in stack overflow: http://stackoverflow.com/questions/41137573/karma-error-config-invalid-config-file. As the TypeScript compilation fails from ts-node, the testing can't proceed further.

I also tried to do a simple ts-node test/unit/setup.ts, and I faced the similar issue with that as well:

C:\Path\to\my\Website>ts-node test/unit/setup.ts

C:\Users\spal\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:312
          throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
                ^
TSError: ⨯ Unable to compile TypeScript
Cannot find type definition file for 'dir1'. (2688)
Cannot find type definition file for 'dir2'. (2688)
Cannot find type definition file for 'dir3'. (2688)
Cannot find type definition file for 'lang1'. (2688)
Cannot find type definition file for 'dir4'. (2688)
Cannot find type definition file for 'lang2'. (2688)
Cannot find type definition file for 'maps'. (2688)
Cannot find type definition file for 'valueConverters'. (2688)
Cannot find type definition file for 'views'. (2688)
    at getOutput (C:\Users\spal\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:312:17)
    at C:\Users\spal\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:343:18
    at Object.compile (C:\Users\spal\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:502:17)
    at Module.m._compile (C:\Users\spal\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:406:44)
    at Module._extensions..js (module.js:579:10)
    at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\spal\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:409:12)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Function.Module.runMain (module.js:604:10)

Any suggestion on what I should look for?

@Sayan751 I'd need to see more details about the project. Explicitly, where are those globals or imports for dir1 coming from? It seems like maybe there's some configuration you're using that ts-node is not.

Actually, it may be the same issue as https://github.com/TypeStrong/ts-node/issues/216. Is there a file expecting /// <reference types="dir1" /> somewhere and no way to resolve it?

I tried to run this file https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/lodash/scripts/generate-modules.ts under DefinitelyTyped repo, but got this error:

➜  lodash git:(master) ✗ ts-node scripts/generate-modules.ts --disableWarnings

/home/cloud/.npm/lib/node_modules/ts-node/src/index.ts:307
        throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
              ^
TSError: ⨯ Unable to compile TypeScript
scripts/generate-modules.ts (14,21): Type 'Set<string>' is not an array type or a string type. (2495)
scripts/generate-modules.ts (20,26): Type 'Set<string>' is not an array type or a string type. (2495)
    at getOutput (/home/cloud/.npm/lib/node_modules/ts-node/src/index.ts:307:15)
    at /home/cloud/.npm/lib/node_modules/ts-node/src/index.ts:336:16
    at Object.compile (/home/cloud/.npm/lib/node_modules/ts-node/src/index.ts:498:11)
    at Module.m._compile (/home/cloud/.npm/lib/node_modules/ts-node/src/index.ts:392:43)
    at Module._extensions..js (module.js:580:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/home/cloud/.npm/lib/node_modules/ts-node/src/index.ts:395:12)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Function.Module.runMain (module.js:605:10)

Both ts-node and typescript are latest by the time.

@e-cloud That looks like a legitimate issue to me. Check the error, it's not a problem with ts-node but the file itself. You can't iterate a Set unless you're compiling ES6 IIRC.

Edit: Actually, I was looking at the wrong tsconfig.json file. Let me review it.

Oh, actually, it is your issue. You need to be in the directory to use the correct tsconfig.json file. This works the same as TypeScript. You're currently using a different tsconfig.json file which is for ES5 and not ES6 - when you run it in the correct directory it uses the tsconfig.json file from there instead.

Sorry for not noticing the tsconfig issue. It indeed works if i cd to the scripts dir

Was this page helpful?
0 / 5 - 0 ratings