SyntaxError: Cannot use import statement outside a module
See original GitHub issueWhen using Create-react-app with Jest.
Any tests that ends up important a Component using jotai/utils
fail with the following error:
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/Users/mark/ypm/x/c1-frontend-v4/node_modules/jotai/utils.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { useMemo, useCallback } from 'react';
^^^^^^
SyntaxError: Cannot use import statement outside a module
1 | import React, { useState } from 'react'
2 | import { useAtom } from 'jotai'
> 3 | import { useUpdateAtom } from 'jotai/utils'
| ^
4 | import { ReactKeycloakProvider } from '@react-keycloak/web'
5 | import keycloak from 'src/lib/keycloak'
6 | import { IDP_HINT } from 'src/common/constants'
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (src/lib/keycloak/AuthProvider.tsx:3:1)
I am using “vanilla” create-react-app. There’s no custom babel configuration or anything just the default.
If I switch to just use useAtom
from ‘jotai’ it works fine. But I want to be able to use the jotai/utils.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
"Uncaught SyntaxError: Cannot use import statement outside ...
This means that you're using the native source code in an unaltered/unbundled state, leading to the following error: Uncaught SyntaxError: ...
Read more >How to fix "cannot use import statement outside a module"
I stumbled on this error: Uncaught SyntaxError: cannot use import statement outside a module while importing a function from a JavaScript file.
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 >Cannot use import statement outside a module - Future Studio
A common error with modules is the “Uncaught SyntaxError: Cannot use import statement outside a module”. This error means you must ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for reporting.
This sounds like one of the most popular use cases…
I know what is probably happening, but don’t know what’s best to fix. If anyone has any ideas, I would like to hear.
Meanwhile, if you changed like this, it would work.
We might change the default to be cjs and add mjs for module files. Yeah, that would solve the nextjs and ie11 issues too.
Let me draft a PR, but I’m not sure about the final decision.