Skip to content
This repository was archived by the owner on Jul 21, 2020. It is now read-only.

Unexpected token import: ES2015 module support without transpiling to CommonJS #5

Closed
CKGrafico opened this issue Dec 5, 2017 · 17 comments
Labels

Comments

@CKGrafico
Copy link

I've made a simple test like your example and I've this error:

(function (exports, require, module, __filename, __dirname) { import test from 'ava';
                                                              ^^^^^^

SyntaxError: Unexpected token import

the test:

import test from 'ava';

test('one plus two equals three', t => {
  t.is(1 + 2, 3);
});
@andywer
Copy link
Owner

andywer commented Dec 5, 2017

Hey @CKGrafico!

It's a bit too little information to debug. What does your tsconfig.json look like? I guess target or module is not set correctly.

You can also have a look at andywer/key-store.

@CKGrafico
Copy link
Author

Ups sorry, the tsconfig is the default tsconfig of nuxt typescript example https://github.com/nuxt/nuxt.js/blob/dev/examples/typescript/tsconfig.json

@andywer
Copy link
Owner

andywer commented Dec 7, 2017

Can't really help here, I fear, since I would need to have access to a project that reproduces the issue.

I can only suggest you have a look at andywer/key-store and compare, since it uses es2015 modules and works just fine.

@CKGrafico
Copy link
Author

You can clone this repo http://bit.ly/2BHkpRV is public :) by the moment I've added mocha for the tests but the community of nuxt is using ava and we want to try

@andywer
Copy link
Owner

andywer commented Dec 12, 2017

Update: I think you have to set "module": "commonjs" instead of "module": "es2015" in your tsconfig.json. The setting describes what to compile it to.

If it's set to es2015, it will output ES2015 modules and node will choke upon it 😉

@CKGrafico
Copy link
Author

Ok in this case I can't use ava thanks

@mastilver
Copy link

Hi

I'm having the same issue

Is there any way to override the tsconfig used by ts-ava like ava is doing?
https://github.com/avajs/ava#es2017-support

@andywer
Copy link
Owner

andywer commented Dec 13, 2017

Hey @mastilver. Yeah, just add a tsconfig.json file to your project and set the appropriate settings ("module": "commonjs" should be the solution here).

Should probably add a FAQ section to the readme.

@mastilver
Copy link

Thank you for the quick answer!! :)

I already have module set to es2015 and I need it that way, I was wondering if it could be overwritten only when using ts-ava

@andywer
Copy link
Owner

andywer commented Dec 13, 2017

Hmmm... Maybe conditionally loading https://github.com/standard-things/esm together with ts-node would solve the issue.

Will re-open the issue and change the title.

Can you check if monkey-patching api.js to load esm works?

@andywer andywer reopened this Dec 13, 2017
@andywer andywer changed the title Unexpected token import Unexpected token import: ES2015 module support without transpiling to CommonJS Dec 13, 2017
@mastilver
Copy link

Thank you so much

This worked for me:

"ava": {
    "require": ["ts-node/register", "@std/esm"]
  }

@andywer
Copy link
Owner

andywer commented Dec 13, 2017

Awesome! Will add a FAQ section later. That should do for the moment :)

@CKGrafico That sounds like good news for you, too.

@mastilver
Copy link

Sounds like I was too quick to shoot victory :/

I still had module set to commonjs and I only realise it later on

I've switch back to normal ava with compile in another process

Thank you for your time anyway :)

@CKGrafico
Copy link
Author

I can't invest time on this for now, but yes if you need ts node probably a config like the project mentioned before will works http://bit.ly/2BHkpRV by the moment I will use mocha because I've all working from other projects but change to ava with this config have to be easy

@andywer
Copy link
Owner

andywer commented Jan 10, 2018

@ALL Better late than never... There is an easy solution!

You can just set the TS_NODE_COMPILER_OPTIONS env variable to set module to commonjs. Will update the README.

  "scripts": {
-   "test": "ava-ts"
+   "test": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' ava-ts"
  }

We should probably have a command line parameter for that, though.

@seiyria
Copy link

seiyria commented Jan 24, 2019

this definitely needs a CLI parameter IMO.

@andywer
Copy link
Owner

andywer commented Jan 28, 2019

@seiyria Fair enough, but #24 needs to be decided on first and that's a tricky one. Will be easier once I found the time to use AVA v1.0 with TypeScript a couple more times.

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

No branches or pull requests

4 participants