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.

`SyntaxError: Cannot use import statement outside a module` in `@coreui/utils/src/index.js:1`

See original GitHub issue

When writing tests for views, I always get the above exception during import of coreui components.

I wrote a simple test for rendering of the dashboard component:

src/views/dashboard/Dashboard.test.js

import React from 'react';
import {shallow} from 'enzyme/build';
import Dashboard from './Dashboard';


it('mounts without crashing', () => {
  const wrapper = shallow(<Dashboard />);
  wrapper.unmount()
});
 ~/d/coreui-free-react-admin-template $ npm run test
 FAIL  src/views/dashboard/Dashboard.test.js
  ● Test suite failed to run

    /Users/sweh/dev/coreui-free-react-admin-template/node_modules/@coreui/utils/src/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import deepObjectsMerge from './deep-objects-merge'
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (node_modules/@coreui/react/lib/fade/CFade.js:12:12)

Can you please provide a working test example for a view?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
NDTChancommented, Nov 18, 2021

I’m getting this error too, plz help me:

/Users/macbook/WebstormProjects/coreui-free-react-admin-template/node_modules/@coreui/icons/js/index.js:1 ({“Object.<anonymous>”:function(module,exports,require,__dirname,__filename,global,jest){import { brandSet } from ‘./brand/brand-set.js’ ^^^^^^

SyntaxError: Cannot use import statement outside a module

  22 | import { getStyle, hexToRgba } from '@coreui/utils'
  23 | import CIcon from '@coreui/icons-react'
> 24 | import {
     | ^
  25 |   cibCcAmex,
  26 |   cibCcApplePay,
  27 |   cibCcMastercard,

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
  at Object.<anonymous> (src/views/dashboard/Dashboard.js:24:1)
3reactions
woothucommented, Jul 20, 2020

Another hotfix (tested on Dashboard)

in package.json jest section:

    "transformIgnorePatterns": [
      "node_modules/(?!(@coreui|tippy.js|perfect-scrollbar)/)"
    ]

We will probably fix it by default in the next @coreui/react minor release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Uncaught SyntaxError: Cannot use import statement outside ...
The possible reasons for the SyntaxError: Cannot use import statement outside a module error is you are trying to run the file independently....
Read more >
Cannot use import statement outside a module [React ...
When building a web application, you may encounter the SyntaxError: Cannot use import statement outside a module error.
Read more >
How to solve: cannot use import statement outside a module
When you see the error message Uncaught SyntaxError: cannot use import statement outside a module, it means you're using an import statement ......
Read more >
How to Fix "SyntaxError: Cannot use import statement outside ...
In this post, we'll learn more about the "SyntaxError: Cannot use import statement outside a module" error and how to fix it in...
Read more >
How to fix "cannot use import statement outside a module"
Here is how to fix the error Uncaught SyntaxError: cannot use import statement outside a module in JavaScript.
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