Import statement in module error with Yarn 3.x
See original GitHub issueDescribe the bug
I can’t run Jest with Yarn 3 and CRA 5
Did you try recovering your dependencies?
yes; yarn --version
is 3.1.1
Which terms did you search for in User Guide?
n/a
Environment
yarn dlx create-react-app --info
:
Environment Info:
current version of create-react-app: 5.0.0
running from /Users/suman/.yarn/berry/cache/create-react-app-npm-5.0.0-afe57ebee6-8.zip/node_modules/create-react-app
System:
OS: macOS 12.1
CPU: (4) x64 Intel(R) Core(TM) i5-5350U CPU @ 1.80GHz
Binaries:
Node: 17.2.0 - /private/var/folders/j8/_wc022w91rvctlq5_t8xqcvc0000gn/T/xfs-bcd1d987/node
Yarn: 3.1.1 - /private/var/folders/j8/_wc022w91rvctlq5_t8xqcvc0000gn/T/xfs-bcd1d987/yarn
npm: 8.1.4 - /usr/local/bin/npm
Browsers:
Chrome: 96.0.4664.93
Edge: Not Found
Firefox: Not Found
Safari: 15.1
npmPackages:
react: Not Found
react-dom: Not Found
react-scripts: Not Found
npmGlobalPackages:
create-react-app: Not Found
Steps to reproduce
(Write your steps here:)
yarn create react-app my-app --template typescript && cd my-app
yarn set version berry
yarn dedupe
yarn run test
outputs the error below
Expected behavior
I expect jest to run normally
Actual behavior
yarn run etst
outputs this:
Error: Failed to initialize watch plugin ".yarn/__virtual__/jest-watch-typeahead-virtual-7373570042/0/cache/jest-watch-typeahead-npm-1.0.0-0c228b0e38-388d518974.zip/node_modules/jest-watch-typeahead/build/file_name_plugin/plugin.js":
● Test suite failed to run
.../tmp/my-app/.yarn/__virtual__/jest-watch-typeahead-virtual-7373570042/0/cache/jest-watch-typeahead-npm-1.0.0-0c228b0e38-388d518974.zip/node_modules/jest-watch-typeahead/build/file_name_plugin/plugin.js:1
import { Prompt } from 'jest-watcher';
^^^^^^
SyntaxError: Cannot use import statement outside a module
25811 | try {
25812 | module.isLoading = true;
> 25813 | module.load(modulePath);
| ^
25814 | hasThrown = false;
25815 | } finally {
25816 | module.isLoading = false;
at Function.external_module_.Module._load (.pnp.cjs:25813:14)
Reproducible demo
repro steps above—no other code or files required
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:7
Top Results From Across the Web
After migrating to yarn 3 workspace "Cannot use import ...
I have a repository with yarn 3 workspaces and a CRA front end located ... SyntaxError: Cannot use import statement outside a module...
Read more >JS Fix For "SyntaxError: Cannot Use Import Statement Outside ...
"SyntaxError: Cannot use import statement outside a module" is a common Node.js error when working with import to include modules or packages.
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 >Use ES Modules in Node.js & Fix "cannot use import ...
Fix cannot use import statement outside a module error in Node.js development ... ECMAScript modules (or ES modules) change the way we import...
Read more >Modules: Packages | Node.js v19.3.0 Documentation
js ) but also to files referenced by import statements and import() expressions. // my-app.js, treated as an ES module because there is...
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 FreeTop 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
Top GitHub Comments
@jasongonzales23 (cc @sumanthratna)
I do not think this is a long term fix; however here is what I did to get this to work, as I was also running into the same issue:
I used this comment as the basis for this work around.
note:
0.6.5
also seems to work, it is when it is1.0.0
that the issue arises.@jasongonzales23 - please try
yarn dlx @yarnpkg/sdks vscode
. This should allow you to use Workspace’s TypeScript Version in VS Code which should get rid of those “'Cannot find module ‘react’” and similar issues.see also: https://stackoverflow.com/a/56831926