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.

Import statement in module error with Yarn 3.x

See original GitHub issue

Describe 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:)

  1. yarn create react-app my-app --template typescript && cd my-app
  2. yarn set version berry
  3. yarn dedupe
  4. 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:open
  • Created 2 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

3reactions
virtuoushubcommented, Dec 23, 2021

👍 I am having the same issue. Would love to know a workaround. Unable to run tests from CLI.

@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:

yarn add -D jest-watch-typeahead@0.6.4

I used this comment as the basis for this work around.


note: 0.6.5 also seems to work, it is when it is 1.0.0 that the issue arises.

yarn add -D jest-watch-typeahead@0.6.5
1reaction
virtuoushubcommented, Dec 24, 2021

Once again thanks for the hand up mate! This isn’t an issue with CRA per se, but now the only remaining issue I’m having is that VSCode ‘Cannot find module ‘react’ (or any other for that matter) or its corresponding type declarations.’

Real head scratcher this one.

@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

Read more comments on GitHub >

github_iconTop 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 >

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