ts-node failing on "import" statements where tsconfig states "commonjs" module

Created on 5 Jan 2018  路  11Comments  路  Source: TypeStrong/ts-node

I read through the now closed issue 212 and I think this is related but separate. In a nutshell what I'm trying to do is:

  • run mocha leveraging ts-node
  • my npm build script DOES compile to es2015 modules _but_ the tsconf.json explicitly states the module type as commonjs (aka, the build script just overrides the default setting for module)
  • When I run mocha, however, I get the all too familiar "Unexpected token import" error (full error below)
  • What's important to contextualize though is the specific import that it's falling over on is a npm dependency that exports a es2015 module
  • I'd have thought ts-node would treat an external es2015 module the same as a local files using es2015 but maybe there's a difference?
  • In my example the library module I'm working on is firemodel which depends on abstracted-admin. I'm the author of both and have tried switching over to es2015 to be more "rollup friendly"

My mocha command is:

mocha --compilers ts:ts-node/register --compilerOptions --recursive test/*/-spec.ts

where the TS_NODE_COMPILER_OPTIONS ENV variable is set to:

'{ "noImplicitAny": false, "module": "commonjs" }'

Here's the full error I'm getting:

SyntaxError: Unexpected token import
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/ken/mine/forest-fire/firemodel/test/create-spec.ts:2:1)
    at Module._compile (module.js:570:32)
    at Module.m._compile (/Users/ken/mine/forest-fire/firemodel/node_modules/ts-node/src/index.ts:392:23)
    at Module._extensions..js (module.js:579:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/ken/mine/forest-fire/firemodel/node_modules/ts-node/src/index.ts:395:12)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at /Users/ken/mine/forest-fire/firemodel/node_modules/mocha/lib/mocha.js:231:27
    at Array.forEach (native)
    at Mocha.loadFiles (/Users/ken/mine/forest-fire/firemodel/node_modules/mocha/lib/mocha.js:228:14)
    at Mocha.run (/Users/ken/mine/forest-fire/firemodel/node_modules/mocha/lib/mocha.js:514:10)
    at Object.<anonymous> (/Users/ken/mine/forest-fire/firemodel/node_modules/mocha/bin/_mocha:480:18)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:383:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:496:3

Most helpful comment

ts-node doesn't compile dependencies in node_modules. See https://github.com/TypeStrong/ts-node#configuration-options for more info. I'll look into improving the README overall as soon as I get a chance, but feel free to submit your own PR.

All 11 comments

I'm in the same situation, but my command isn't even as complex as yours:

"mocha -r ts-node/register -r tsconfig-paths/register -r chai/register-expect \"./spec/*/Spec.js""

There's something wrong with your tsconfig.json, probably. Check carefully.

Using of --compilers ts:ts-node/register/type-check helps to find missed definitions in my case.

ts-node doesn't compile dependencies in node_modules. See https://github.com/TypeStrong/ts-node#configuration-options for more info. I'll look into improving the README overall as soon as I get a chance, but feel free to submit your own PR.

Ended up with funny script that just copies my *.ts files out from node_modules to some other dir, symlinking it, and then ts-node finally compiles it. Same files. Only difference - they are in a folder that is not named node_modules. Look very weird, why is it such a big deal for ts-node that files are in node_modules?

@kirillgroshkov because this is tsc behaviour, not ts-node. tsc never compiles things in node_modules.

@blakeembrey Does it work if I use import with native Node module (e.g. import { resolve } from 'path';) instead of node_modules?

@gluons I don't think you understood this issue and I'm not sure how you ended up here. You would never want to compile a native node.js module as TypeScript, but you can always import with valid types (e.g. @types/node).

@blakeembrey Oh! I solved my problem. Sorry for dumb question.

I try to create an new fresh project to test ts-node with Native Node module (like path or fs). It works perfectly.
Maybe I made some mistake in my project. 馃槄

Thank you very much for clarification. I really appreciate.
Really sorry for disturbance. 馃檱

@ blakeembrey "ts-node doesn't compile dependencies in node_modules. "

Perhaps something like this could appear in an error message?

You are welcome to create a PR, but what error message are you describing?

Was this page helpful?
0 / 5 - 0 ratings