ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

"Unexpected token import" when dependency in .ts is an es6 module

Open rafaelcichocki opened this issue 8 years ago • 22 comments

Possibly related issue: https://github.com/TypeStrong/ts-node/issues/505

Setup

ts-node: 7.0.0
typescript: 2.9.2
tape: 4.9.1

Problem: Es6 module dependency doesn't work

I'm trying to import functions from ramda in my typescript file.

src/index.spec.ts

import add from "ramda/es/add"

node_modules/ramda/es/add.js

import _curry2 from './internal/_curry2';
var add = /*#__PURE__*/_curry2(function add(a, b) {
  return Number(a) + Number(b);
});
export default add;

Then I try to run my test with tape:

ts-node node_modules/tape/bin/tape src/**/*.spec.ts
C:\project\node_modules\ramda\es\add.js:1
(function (exports, require, module, __filename, __dirname) { import _curry2 from './internal/_curry2';
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Module._extensions..js (module.js:663:10)
    at Object.require.extensions.(anonymous function) [as .js] (C:\project\node_modules\ts-node\src\index.ts:431:14)
...

CommonJs Dependency works

src/index.spec.ts

import add = require("ramda/src/add")

node_modules/ramda/src/add.js

var _curry2 = /*#__PURE__*/require('./internal/_curry2');
var add = /*#__PURE__*/_curry2(function add(a, b) {
  return Number(a) + Number(b);
});
module.exports = add;

Then I try to run my test with tape:

ts-node node_modules/tape/bin/tape src/**/*.spec.ts
...
# tests 5
# pass  5

# ok

Questions

Is this intended behavior for ts-node? Are there any options I have to use es6 dependencies, or is ts-node not an option in that case?

rafaelcichocki avatar Jun 25 '18 17:06 rafaelcichocki

This just started cropping up for us as well. It happened, possibly by coincidence, when we started exporting a function instead of a class. I notice that the example given above with ramda is also a function, but perhaps this is a red herring.

ddurschlag6river avatar Jun 26 '18 15:06 ddurschlag6river

I have a very similar problem.

This is linked to the values of target and module in the tsconfig.json.

Certain people in other issues recommend to use "module": "commonjs" since they say ES6 is not supported with this tool. It does not work for me since the library enzyme uses import locally from within functions, which seems to be specific to ES6.

import will be fixed for finding enzyme, but not for the local import within enzyme. [...]node_modules/enzyme/src/index.js:1 (function (exports, require, module, __filename, __dirname) { import ReactWrapper from './ReactWrapper'; SyntaxError: Unexpected token import

And if I look at the code of rafaelcichocki who create this issue, he has a local import from within a function, which seems to not work when moving to "module": "commonjs".

As a solution, I am trying to transpile ES6 into ES5 first.

MagicPoulp avatar Jun 28 '18 13:06 MagicPoulp

As a possible solution, one can replace ts-node by webpack to chain a typescript parser with babel. Because webpack is so much popular, it will have good tools to process Typescript, even with ES6 Javascript.

Here is a very nice blog explaining how to set up the mocha testing environment to process both typescript and babel. They use the library "mixer" called mocha-webpack. Many people recommend to use mocha with ts-node. This blog gives an alternative. It may be that, with this alternative, the warnings will show up earlier. Besides, webpack and its plugins is very clever to find the node_modules that typescript on its own cannot find unless you cutomize tsconfig.json in a tricky way.

https://engineering.policygenius.com/typescript-mocha-and-babel-ffd07369792a

Edit: the solution above did not work for me. Probably because an old version of webpack is required.

MagicPoulp avatar Jun 28 '18 14:06 MagicPoulp

I found an acceptable solution.

I installed babel-cli I produced a new version of each packaged which had the import error. I made the ts_config use them as paths using wildcards.

My mocha test that passes while it combines mocha, ts-node, tsconfig-paths, babel-core looks like this:

TS_NODE_PROJECT=tsconfig_test.json BABEL_ENV=test mocha --require ts-node/register --require tsconfig-paths/register --require babel-core/register assets/js/test/unit_tests/tests/test_poseidon_navigator.ts

MagicPoulp avatar Jun 28 '18 16:06 MagicPoulp

TS Node does not compile files in node_modules by default. If you want to consume ES modules, you should update to a node.js version that supports ES modules natively, use something like esm or alter the default behaviour by changing --ignore.

blakeembrey avatar Jun 29 '18 16:06 blakeembrey

Both these projects provide non-ES2015 module versions by transpiling for NPM, so it looks like you're both going out of your way to import files unsupported by your node.js version (e.g. https://github.com/airbnb/enzyme/blob/48e0d20fa1d17931534da52568e91efcd7a7e1d7/packages/enzyme/package.json). Maybe skip the ES2015 modules until you have a better grasp on how node.js treats these files. You can, of course, transpile them again yourself into something node.js works with if you wish - but I'm unsure why you'd want to go that path?

blakeembrey avatar Jun 29 '18 16:06 blakeembrey

In our case, we're importing our own code (in our private repo, no transpiling -- pure .ts files) on node 10.4.1 using tsconfig excerpt:

"module": "commonjs",
"target": "es2017",

Unless I misread your comment, your explanation wouldn't seem to apply to us. Copy/pasting the code from our private package into the one attempting to use it resolves the issue, so it appears to have something to do with TypeScript dependencies. Other TypeScript dependencies continue to work fine -- it is this one function which we've oddly been unable to reference without error.

ddurschlag6river avatar Jun 29 '18 16:06 ddurschlag6river

@ddurschlag6river You'll need to provide something I can look at to reproduce. The other two comments were about node_modules so I only referred to them. I also don't understand what you mean when you say you copy/paste code. Are you installing this provide package via node_modules? If so, it applies.

blakeembrey avatar Jun 29 '18 17:06 blakeembrey

The function in question is:

export function isSingle<T, S extends Model&Instance<T>, M extends Model&Data>(context: S|M): context is S {
	return (<S>context).instance !== undefined;
}

When imported from node_modules (where it is in a .ts file) it gives this error. When copy/pasted into a .ts file not in node_modules (specifically right into the file that wanted to use it) it works fine. Other .ts files in the same subdirectory of node_modules work fine and can be imported from. The only differences I've noted are:

  1. In this case I'm importing a function, not a class (more common), and
  2. In this case I'm using the TS type system more... aggressively

Unfortunately, this is all embedded in a significant non-public codebase, and I haven't had time to boil it down to a minimal repro case yet. It's on my todo list, but with the copy/paste workaround available it's not high up, unfortunately.

ddurschlag6river avatar Jun 29 '18 17:06 ddurschlag6river

What version are you using? What flags have you enabled? This behaviour shouldn’t be possible by default because, as noted, it’s not compiling anything in node modules. Can you share the error? It’ll at least have a stack trace I can review.

blakeembrey avatar Jun 30 '18 02:06 blakeembrey

@blakeembrey This is a valid case. Basically problem comes when your test files are importing actual code from node_modules. But when you are importing just types or your code outside of node_modules it works fine.

So the goal would be to import code from node_modules in test files (files that mocha executes).

Tell me if you didn't understand and need more info :)

henrikra avatar Aug 15 '18 11:08 henrikra

I don’t understand and need more info, what exactly are you asking for? It’s not possible to run the compiler for node modules by default.

blakeembrey avatar Aug 15 '18 15:08 blakeembrey

Btw I solved this. The real solution is to compile your npm package code to JS and turn declarations to true on tsconfig. This way you can safely import code in your test files from node_modules plus with declarations you will get TS types too 😀

So @blakeembrey you were right: node_modules should include only valid JS code + d.ts files

henrikra avatar Aug 16 '18 05:08 henrikra

will we ever be able to depend on TypeScript dependencies? I'm really tired of compiling into JS. I just want to ts-node .

Having to tsc -w on my linked dependencies in another process isn't fun

intellix avatar Oct 12 '18 13:10 intellix

In my case, flag allowJs: true in tsconfig.json resolved the similar problem 🎉

hypeofpipe avatar Nov 27 '18 20:11 hypeofpipe

@hypeofpipe allowJs, not allowJS

briangonzalez avatar Apr 26 '19 19:04 briangonzalez

The same problem, I think throwing the Error, because you haven't allowJs or module is not commonjs in tsconfig, I'm the latter.

mistricky avatar Apr 30 '19 04:04 mistricky

Why I have to use module:commonjs in tsconfig.json to make my project run in ts-node?

gitowiec avatar Jul 03 '19 20:07 gitowiec

Because node.js doesn’t understand ESM natively right now.

blakeembrey avatar Jul 03 '19 23:07 blakeembrey

TS Node does not compile files in node_modules by default. If you want to consume ES modules, you should update to a node.js version that supports ES modules natively, use something like esm or alter the default behaviour by changing --ignore.

Btw I solved this. The real solution is to compile your npm package code to JS and turn declarations to true on tsconfig. This way you can safely import code in your test files from node_modules plus with declarations you will get TS types too 😀

So @blakeembrey you were right: node_modules should include only valid JS code + d.ts files

@henrikra How did you compile your tests in node_modules ? Did you compile manually or did you use babel-register/typescript and compiled on the fly? Can you please provide steps? We have similar mocha test project which we want to import to typescript project and it fails to compile with this error.

tatthipatla avatar Aug 20 '19 21:08 tatthipatla