Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
yankeeinlondon opened this issue Jan 5, 2018 · 11 comments

Comments

@yankeeinlondon
Copy link

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
@cryptoquick
Copy link

cryptoquick commented Jan 5, 2018

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""

@nnboskovic
Copy link

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

@mnasyrov
Copy link

mnasyrov commented Jan 9, 2018

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

@blakeembrey
Copy link
Member

blakeembrey commented Jan 10, 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.

@kirillgroshkov
Copy link

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?

@Auxx
Copy link

Auxx commented Mar 7, 2018

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

@gluons
Copy link

gluons commented May 3, 2018

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

@blakeembrey
Copy link
Member

blakeembrey commented May 4, 2018

@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).

@gluons
Copy link

gluons commented May 4, 2018

@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. 🙇

@paxperscientiam
Copy link

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

Perhaps something like this could appear in an error message?

@blakeembrey
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants