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.

[Bug] SyntaxError: Unexpected token 'export'

See original GitHub issue
  • I’d be willing to submit the fix

Describe the bug

I have a Nuxt application with a library of @adonisjs/websocket-client, when I compile it with Nuxt it works but when I use to test my application using mochapack it runs into an error of: SyntaxError: Unexpected token ‘export’ here is the full error:

node_modules/@adonisjs/websocket-client/dist/Ws.es.js:2507
export default index;
^^^^^^

SyntaxError: Unexpected token 'export'

To Reproduce

  1. Create a nuxt app using npx create-nuxt-app <app-name> or yarn create nuxt-app <app-name>
  2. Then install @adonisjs/websocket-client
  3. Import Ws from @adonisjs/websocket-client in pages/index.vue
  4. Configure mochapack to be able to test Nuxt app here is a link to set it up: Setup for Vue and Mochapack
  5. Run test and the error will occur

Here is a more details result of the error:

➜ npm run test

> nuxt-mochapack@1.0.0 test .../nuxt-mochapack
> mochapack

 WEBPACK  Compiling...

  [=========================] 100% (completed)

 WEBPACK  Compiled successfully in 756ms

 MOCHA  Testing...

 RUNTIME EXCEPTION  Exception occurred while loading your tests

.../nuxt-mochapack/node_modules/@adonisjs/websocket-client/dist/Ws.es.js:2507
export default index;
^^^^^^

SyntaxError: Unexpected token 'export'
    at wrapSafe (internal/modules/cjs/loader.js:891:16)
    at Module._compile (internal/modules/cjs/loader.js:941:27)
    at Module._extensions..js (internal/modules/cjs/loader.js:1011:10)
    at Object.module_1.default._extensions.<computed> [as .js] (.../nuxt-mochapack/node_modules/mochapack/src/util/registerRequireHook.ts:99:53)
    at Module.load (internal/modules/cjs/loader.js:822:32)
    at Function.Module._load (internal/modules/cjs/loader.js:730:14)
    at Module.require (internal/modules/cjs/loader.js:864:19)
    at require (internal/modules/cjs/helpers.js:74:18)

Environment if relevant (please complete the following information):

  • OS: Ubuntu 20.04
  • Node version: 13.0.1
  • Mochapack version: ^1.1.13
  • Webpack version: (Not sure what version Nuxt is using)

Additional context

Remove some information replaced them with ... for brevity. I also tried the configuring my mochapack.config.js with esModule: false options for file-loader but that did not do the trick. Just some more information Nuxt uses webpack as well and it compiles and run the application without any issues, only when I try to run the test with mochapack.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
quangkevincommented, May 12, 2021

Hi @larixer The change should be done outside of mochapack.

mochapack --require setup.js ...

setup.js:

require("@babel/register")({
  plugins: ["@babel/plugin-transform-modules-commonjs"],
});

Alternative solution is to use esm:

mochapack --require esm ...
1reaction
larixercommented, May 12, 2021

@quangkevin This software is an open-source community project and comes without any warranties, please read the last paragraph of its license: https://github.com/sysgears/mochapack/blob/master/LICENSE.md For the issue to be fixed someone, who is interested, should submit a pull request with the fix first, it is a first step that should be done, but not the last, the pull request should have satisfactory quality and pass all the tests, it should have good code quality, etc, etc

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Unexpected Token Export - javascript - Stack Overflow
When you include the script like this: <script src="module.js"></script>. You will get the error: Uncaught SyntaxError: Unexpected token export.
Read more >
How to fix SyntaxError: Unexpected token 'export' in JavaScript?
In this article, we are going to see a simple yet confusing error that many new developers face when working with ES6 modules....
Read more >
SyntaxError: Unexpected token 'export' in JavaScript
To solve the "Uncaught SyntaxError Unexpected token 'export'" error, set the type property to module in your package.json file.
Read more >
SyntaxError: Unexpected token 'export' - Abhishek Kumar
In case you are getting error like 'Unexpected token export' while starting the server, then export like below in schema.js
Read more >
How to Fix „Uncaught SyntaxError: Unexpected token 'export
Using type="module" on the <script> tag solved the „Unexpected token export ” error. The browser runs the imported JavaScript without issues.
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