Ts-node: Compilation error with mocha and TypeScript 2.0 on Windows 10

Created on 7 Aug 2016  Â·  13Comments  Â·  Source: TypeStrong/ts-node

I'm trying to run mocha tests using --compilers ts:ts-node/register with TypeScript 2.0 but get the following error only on Windows (10) while it works flawlessly on a Mac -

C:\temp\nodets\node_modules\ts-node\src\index.ts:259
          throw new TSError(diagnosticList)
                ^
TSError: ⨯ Unable to compile TypeScript
test\greeter-spec.ts (6,1): Cannot find name 'describe'. (2304)
test\greeter-spec.ts (7,3): Cannot find name 'it'. (2304)
    at getOutput (C:\temp\nodets\node_modules\ts-node\src\index.ts:259:17)
    at C:\temp\nodets\node_modules\ts-node\src\index.ts:268:16
    at Object.compile (C:\temp\nodets\node_modules\ts-node\src\index.ts:404:17)
    at loader (C:\temp\nodets\node_modules\ts-node\src\index.ts:290:33)
    at Object.require.extensions.(anonymous function) [as .ts] (C:\temp\nodets\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)
    at C:\temp\nodets\node_modules\mocha\lib\mocha.js:220:27
    at Array.forEach (native)
    at Mocha.loadFiles (C:\temp\nodets\node_modules\mocha\lib\mocha.js:217:14)
    at Mocha.run (C:\temp\nodets\node_modules\mocha\lib\mocha.js:485:10)
    at Object.<anonymous> (C:\temp\nodets\node_modules\mocha\bin\_mocha:405:18)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.runMain (module.js:575:10)
    at run (node.js:348:7)
    at startup (node.js:140:9)
    at node.js:463:3
npm ERR! Test failed.  See above for more details.

You can replicate the issue using the following steps -

git clone https://github.com/ospatil/tsnode-issue.git
cd tsnode-issue
npm install
npm test

I managed to get it working on Windows by adding a triple slash reference to mocha type definition /// <reference path="../node_modules/@types/mocha/index.d.ts" /> to test/greeter-spec.ts but it seems redundant since it works flawlessly without it on Mac.

Most helpful comment

Giving "types": [ "node", "mocha", "chai" ] under compilerOptions on tsconfig.json solve the issue.

All 13 comments

Same issue here, and the triple slash workaround also worked for me.

Same issue here (although it's occurring w/ @types/protractor and not mocha), on Windows with [email protected]. The triple-slash fix worked for me.

Same issue on Ubuntu using @types - triple slash also fixed it for me.

Giving "types": [ "node", "mocha", "chai" ] under compilerOptions on tsconfig.json solve the issue.

@budiadiono Can you elaborate? I added that to my tsconfig.json to no avail.

{
  "compilerOptions": {
    "types": [ "node", "mocha", "chai", "bunyan", "lodash"],
    "typeRoots": ["typings"]
  }
}

I've also tried specifying the typeRoots in compilerOptions as well. This does not work either. I'm not entirely sure why.

Also is there anyway this can be addressed without having to maintain a separate list of typings? This is a rather broken approach, in my opinion.


I was able to resolve this issue by adding this block to my tsconfig.json.

{
  "include": [ "**/*.ts" ],
}

@jamelt, maybe you forgot to install @types/[module] for requested types? Thus you have to do "include": [ "**/*.ts" ] that makes typescript look for the scripts in entire location.

I had similar issue of ts-node on windows not finding protractor types under node_modules/@types without explicit "include": [ "**/*.ts" ] configuration while in osx everything seemed to work without it.

edit: Actually include was not the key since I forgot explicit /// <reference path="../node_modules/@types/protractor/index.d.ts" /> in place which is enough to fix the problem by itself. And for clarification /// <reference types="protractor" /> is not enough to make it work. Neither of the tripleslash annotations are needed in osx since types are already mentioned in typescript configuration's "types" array.

edi2: And for the reference node node_modules\typescript\lib\tsc.js --types node,protractor scripts\e2e.ts compiles just fine on windows without tripleslash annotations.

Giving "types": [ "node", "mocha", "chai" ] under compilerOptions on tsconfig.json solve the issue.

this works for me.

import "mocha"; works for me.

This doesn't work so well for node library types though (would have to add each individually), and adding node to types in compilerOptions causes conflicts in ts-node if another module also uses node typings.

/// <reference> seems to be the best option in that case, but this seems to be indicative of a larger problem which only happens in ts-node and not the typescript compiler.

Closing in favour of https://github.com/TypeStrong/ts-node/issues/216. It looks like all the issues stem from Windows and I'd guess it's a path issue with TypeScript (they do some odd escaping and expect inputs escaped in certain ways - possibly it's a related issue to this). If someone finds the cause, feel free to submit a PR.

If you're working around it, use tsconfig.json _or_ <reference types>. Do not use an import as it will emit a runtime require statement, which is why you can't use it for global declarations.

I was running into a very similar problem, ensuring typeRoots was defined and correct in my tsconfig.json solved the issue for me, e.g.

yarn add -D @types/mocha
{
   "types": [
     "mocha"
  ],
  "typeRoots": [
    "./node_modules/@types"
  ]
}

@wrumsby, thanks, that worked for me as well!

The only thing that worked for me in Ubuntun 17.04 was:

npm install --save-dev @types/mocha

and adding this to tsconfig.json

{
   "types": [
     "mocha"
  ],
  "typeRoots": [
    "./node_modules/@types"
  ]
}

thanks @wrumsby

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JoseLion picture JoseLion  Â·  3Comments

sanex3339 picture sanex3339  Â·  4Comments

dakom picture dakom  Â·  3Comments

watzon picture watzon  Â·  3Comments

nehalist picture nehalist  Â·  3Comments