ts-node: 'Unexpected token import' running mocha with TS 2.0

Hi, I was hoping you could help me troubleshoot something.

I’ve been using ts-node to run unit tests with mocha for a while:

mocha  --opts ./src/___tests___/mocha.opts

./src/**tests**/mocha.opts:

--compilers ts:ts-node/register
--compilers tsx:ts-node/register
**/*.spec.ts

This works great with typescript 1.8.10. However since upgrading to typescript 2.0.0 I’m getting this:

/Users/Seth/code/angular-redux/ng2-redux/examples/counter/node_modules/ng2-redux/src/___tests___/components/ng-redux.spec.ts:1
(function (exports, require, module, __filename, __dirname) { import 'reflect-metadata';
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:513:28)
    at Module._extensions..js (module.js:550:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/Seth/code/angular-redux/ng2-redux/node_modules/ts-node/src/index.ts:304:16)
    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 /Users/Seth/code/angular-redux/ng2-redux/node_modules/mocha/lib/mocha.js:220:27
    at Array.forEach (native)
    at Mocha.loadFiles (/Users/Seth/code/angular-redux/ng2-redux/node_modules/mocha/lib/mocha.js:217:14)
    at Mocha.run (/Users/Seth/code/angular-redux/ng2-redux/node_modules/mocha/lib/mocha.js:469:10)
    at Object.<anonymous> (/Users/Seth/code/angular-redux/ng2-redux/node_modules/mocha/bin/_mocha:404: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 (bootstrap_node.js:352:7)
    at startup (bootstrap_node.js:144:9)
    at bootstrap_node.js:467:3

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 14
  • Comments: 34 (8 by maintainers)

Most upvoted comments

I’m able to compile using the es6 flag by setting module to “commonJS” in my tsconfig @tomitrescak. It appears as though the tsc is not properly compiling the imports otherwise. So I think it is actually a tsc bug.

Hi @blakeembrey, I’m able to get my TS tests to run just fine by pointing ts-node to a project file that states my module format to be “commonjs” (even though my build process actually converts to ES2015). My command line looks like:

TS_NODE_PROJECT=“tsconfig.testing.json” mocha --require ts-node/register ‘test/**/*-spec.ts’

The problem I am running into is that I use the lodash-es library which appears to only export ES2015 code. My source code is transpiled from ES to CJS but libraries in `node_modules appear not to be. I sort of suspect that this is a “mocha” thing more than a “ts-node” thing but the boundary lines are a bit blurry to me. Was wondering if maybe you could weigh in on this.

Oh for reference the the tsconfig.testing.json looks like this:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2015",
    "lib": ["es2017"],
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "inlineSourceMap": true,
    "moduleResolution": "node"
  },
  "include": ["scripts/**/*.ts", "src/**/*.ts", "node_modules/lodash-es/**/*.js"]
}

The attempt to add “node_modules/lodash-es/**/*.js” at the end is just pure desperation 😃

Thanks @jeffrey-l-turner . You save me. 👍

ts-node -O '{ "module": "commonjs" }' work for me. 🎉

@fsaldivars Node.js doesn’t support ES6 modules.

A variation of the above information looks like this in package.json to invoke mocha:

  "scripts": {
    "test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha"
  },

This will let mocha invoke ts-node however it may, and ts-node will pick up the compiler options from the environment variable.

Strangely for me, I fixed this with @SethDavenport’s example though slightly differently.

I had the files to test glob in the test script like so

"test": "mocha --opts tests/mocha.opts tests/**/*.spec.ts"

Moving tests/**/*.spec.ts to mocha.opts magically fixed the issue. Strange!

--compilers ts:ts-node/register
tests/**/*.spec.ts

Yep that was it.

Need to change **/*.spec.ts to src/**/spec.ts in my mocha.opts. Sorry for wasting your time 😃

Not sure why this used to work…

I’m using node 6.3.0.

Here’s my tsconfig.json:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "rootDir": "src",
    "outDir": "lib/",
    "noImplicitAny": false,
    "declaration": true
  },
  "compileOnSave": false,
  "buildOnSave": false,
  "exclude": [
    "node_modules",
    "dist",
    "lib",
    "examples"
  ]
}

@blakeembrey
Hi Could you explain me why when I replace in my “tsconfig,json” => “module”: “es6” by “module”: “commonjs”, everything works ok. I try to find the answer but I cand find it.

I appreciate your comments.

@emzero In my case, I use "module": "es2015". So, it won’t work because ts-node doesn’t support "module": "es2015". (#212) But in the other case, I don’t know. 😅