Unexpected token export with Lodash

See original GitHub issue

Minimal repo: https://github.com/epicfaace/jest-bug-1

  • Issue My tests fail:
   C:\Users\arama\Documents\My Web Sites\WordPress\wp-content\plugins\CFF\node_modules\lodash-es\lodash.js:10
    export { default as add } from './add.js';
    ^^^^^^

    SyntaxError: Unexpected token export

My code:

import {set, unset, get, isArray} from 'lodash-es';
  • My jest config:
  "jest": {
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  }

I tried changing my jest config to the following, but it still doesn’t work:

  "jest": {
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!lodash-es)"
    ]
  }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:23
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

12reactions
DorianGreycommented, Apr 23, 2018

lodash-es requires transpilation to work properly on Node with commonjs.

I.e. extend your transforms like this:

"transform": {
    "^.+\\.js$": "babel-jest"
}

And use a .babelrc in the root of your project with at least the transform-es2015-modules-commonjs plugin defined:

{
  "plugins": [
    "transform-es2015-modules-commonjs"
  ]
}

babel-jest should already be installed due to jest, I’m not sure about the plugin - might be required to install this manually.

3reactions
epicfaacecommented, Apr 22, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest setup "SyntaxError: Unexpected token export"
This seems like a hack because it takes over 1 minute to run a simple test that imports node_modules/lodash-es/lodash.js . reactjs · redux ......
Read more >
jest lodash-es SyntaxError - Unexpected token export
Answers related to “jest lodash-es SyntaxError: Unexpected token export” · parsing error unexpected token javascript · popper.min. · jest invalid or unexpected ...
Read more >
ts-jest syntaxerror: unexpected token 'export' - You.com | The search ...
I'm getting SyntaxError: Unexpected token 'export' on some node_modules , which are not transpiled (I guess?). I worked around this by adding these...
Read more >
Jest setup "SyntaxError: Unexpected token export"-Reactjs
In your case, lodash-es specifically exposes ES modules, which will need to be built by Jest via babel. You can try narrowing your...
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 >

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