Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Cannot use import statement outside a module #4338

Open
gitneeraj opened this issue Jul 26, 2024 · 7 comments
Open

SyntaxError: Cannot use import statement outside a module #4338

gitneeraj opened this issue Jul 26, 2024 · 7 comments
Labels

Comments

@gitneeraj
Copy link

gitneeraj commented Jul 26, 2024

Please describe the a concise description and fill out the details below. It will help others efficiently understand your request and get to an answer instead of repeated back and forth. Providing a minimal, complete and verifiable example will further increase your chances that someone can help.

Steps for Reproduction

created a basic CRA app. Visit the bare minimum reproducible repo at https://github.com/gitneeraj/test-quill-rte

Expected behavior:
when running the tests yarn test should mount the component and test should pass

Actual behavior:
Throws below error -

/home/xx/personal/test-quill/node_modules/quill/quill.js:1
({"Object.":function(module,exports,require,__dirname,__filename,jest){import Quill, { Parchment, Range } from './core.js';
^^^^^^

SyntaxError: Cannot use import statement outside a module

> 1 | import RickTextEditor from "quill";
    | ^
  2 | import React, { forwardRef, useEffect, useLayoutEffect, useRef } from "react";
  3 | import "quill/dist/quill.snow.css";
  4 |

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
  at Object.<anonymous> (src/Editor.js:1:1)
  at Object.<anonymous> (src/App.js:2:1)
  at Object.<anonymous> (src/App.test.js:2:1)
  at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
  at runJest (node_modules/@jest/core/build/runJest.js:404:19)

Platforms:
node - 19 & 20
Include browser, operating system and respective versions

Version:

I have looked at issue #4038 but this does not work anymore

@cjnoname
Copy link

Same errror here

@luin
Copy link
Member

luin commented Jul 30, 2024

I believe this is a duplicate of facebook/create-react-app#9938.

@chrishoage
Copy link

@luin Not really - the issue appears to be that quill is defined as type: module but doesn't define a module

See: https://nodejs.org/api/packages.html#dual-commonjses-module-packages

main should still point to a commonJS build
module should point to an ESM build

Subpath exports can be used to define both require and module exports, and have types for them https://nodejs.org/api/packages.html#subpath-exports

As it stands today this package doesn't appear to fully support dual packages which makes integration with tools like jest challenging.

Please consider using one of the dual package approaches outlined in the link

@gitneeraj
Copy link
Author

gitneeraj commented Aug 8, 2024

@chrishoage - thank you for the input. Do you think there is any workaround getting the unit tests running with this package?
I tried to mock the entire package but it leads to whole lot of issues.

@luin - do you have any tentative date to fix this?

@chrishoage
Copy link

We used jest moduleNameMapper to point to the dist/quill.js file that is still in the package but not otherwise exposed via package.json

This allowed jest to use the "ADM" build of quill which jest can use with out any extra compilation on quill

@daiscog
Copy link

daiscog commented Aug 19, 2024

@chrishoage thanks for the hint, that's worked for us!

Specifically, in our jest.config.ts, we've added:

export default {
  //...
  moduleNameMapper: {
    '^quill$': 'node_modules/quill/dist/quill.js'
  },
}

For those using an nx monorepo with a root jest.preset.js file which all projects' jest configs inherit from, you can just pop the above config in that preset file instead of the individual jest.config.ts files.

@RichMatthews
Copy link

@luin please chime in if you have anything else to add

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants