question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:25
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

34reactions
nnboskocommented, Jan 8, 2018

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

22reactions
blakeembreycommented, Jan 11, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript: Cannot use import statement outside a module
To solve the error "Cannot use import statement outside a module" in TypeScript, set the module option to commonjs in your tsconfig.json file...
Read more >
Cannot use import statement outside a module" error in ts ...
I executed my .ts file by nodemon which delgates the executing of ts-node when target file is .ts, but ts-node must be installed....
Read more >
Cannot use import statement outside a module [React ...
When building a web application, you may encounter the SyntaxError: Cannot use import statement outside a module error. This error might be ...
Read more >
TSConfig Option: esModuleInterop - TypeScript
the ES6 modules spec states that a namespace import ( import * as x ) can only be ... Turning on esModuleInterop will...
Read more >
Ts cannot find module paths - allpepper
/components/App. ts node ts-node failing on "import" statements where tsconfig states "commonjs" module. An alternative (comparing process. vue' or its ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found