Possibly related issue: https://github.com/TypeStrong/ts-node/issues/505
ts-node: 7.0.0
typescript: 2.9.2
tape: 4.9.1
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)
...
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
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?
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.
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.
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.
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
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.
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?
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 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.
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.
What version are you using? What flags have you enabled? This behaviour shouldn鈥檛 be possible by default because, as noted, it鈥檚 not compiling anything in node modules. Can you share the error? It鈥檒l at least have a stack trace I can review.
@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 :)
I don鈥檛 understand and need more info, what exactly are you asking for? It鈥檚 not possible to run the compiler for node modules by default.
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
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
In my case, flag allowJs: true in tsconfig.json resolved the similar problem 馃帀
@hypeofpipe allowJs, not allowJS
The same problem, I think throwing the Error, because you haven't allowJs or module is not commonjs in tsconfig, I'm the latter.
Why I have to use module:commonjs in tsconfig.json to make my project run in ts-node?
Because node.js doesn鈥檛 understand ESM natively right now.
TS Node does not compile files in
node_modulesby default. If you want to consume ES modules, you should update to a node.js version that supports ES modules natively, use something likeesmor 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
declarationstotrueontsconfig. This way you can safely import code in your test files fromnode_modulesplus withdeclarationsyou will get TS types too 馃榾So @blakeembrey you were right:
node_modulesshould include only valid JS code +d.tsfiles
@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.
@blakeembrey Here is the scenario for you understanding.
Project A is ES6 based mocha tests(No typescript). Used Babel to transpile to es5 and ran tests. Works fine.
Project B is Typescript with ProjectA imported (import {lib} from ProjectA).
Tried using node-ts with all combination to tsconfig. Getting unexpected token import issue.
From your previous comments, understand that ts-node wont compile node modules, but what is the way/steps to get this working?
Most helpful comment
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:
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.