Skip to content

Cannot find module 'cheerio' from 'node_modules/enzyme/build/Utils.js' #2518

Open
@mmarshad

Description

I just start receiving this message to my unit tests today, although it was working fine before

Cannot find module 'cheerio' from 'node_modules/enzyme/build/Utils.js'

Require stack:
  node_modules/enzyme/build/Utils.js
  node_modules/enzyme/build/ShallowWrapper.js
  node_modules/enzyme-to-json/utils.js
  node_modules/enzyme-to-json/createSerializer.js
  node_modules/enzyme-to-json/serializer.js

  at Resolver.resolveModule (node_modules/jest-runtime/node_modules/jest-resolve/build/index.js:306:11)
  at Object.<anonymous> (node_modules/enzyme/src/Utils.js:9:1)

Is there anything changed in serlizer.js?
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.6",
"enzyme-to-json": "^3.6.1",

Activity

ljharb

ljharb commented on May 6, 2021

@ljharb
Member

enzyme-to-json is not maintained by the enzyme team, so you'd have to ask them.

Mahdiyeh

Mahdiyeh commented on May 7, 2021

@Mahdiyeh

Same here for me:

`
Cannot find module 'cheerio' from 'node_modules/enzyme/build/Utils.js'

Require stack:
  node_modules/enzyme/build/Utils.js
  node_modules/enzyme/build/ReactWrapper.js
  node_modules/enzyme/build/index.js

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:306:11)
  at Object.<anonymous> (node_modules/enzyme/build/Utils.js:80:16)
  `

My dependencies:
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.5"

ljharb

ljharb commented on May 7, 2021

@ljharb
Member

Please try pinning cheerio to =1.0.0.rc-3, ensure it's installed, and let me know if the issue persists.

rxgx

rxgx commented on May 20, 2022

@rxgx

Adding an override doesn't fix the issue with enzyme incorrectly using a loose RC pattern for a hard dependency.

  "overrides": {
    "enzyme": {
      "cheerio": "1.0.0.rc-3"
    }
  },

Cheerio is now a dual CommonJS and ESM module. That means that deep imports will now fail in newer versions of Node.
https://github.com/cheeriojs/cheerio/releases/tag/v1.0.0-rc.11

ljharb

ljharb commented on May 20, 2022

@ljharb
Member

@rxgx while that should work - file an issue with npm if it doesn't - simply adding "cheerio": "=1.0.0.rc-3" to your devDependencies should be sufficient, no overrides required.

rxgx

rxgx commented on May 20, 2022

@rxgx

@ljharb what old version of npm are you using?

npm ERR! code EINVALIDTAGNAME
npm ERR! Invalid tag name "=1.0.0.rc-3" of package "cheerio@=1.0.0.rc-3": Tags may not have any characters that encodeURIComponent encodes.
ljharb

ljharb commented on May 20, 2022

@ljharb
Member

works fine with every version of npm, through the latest. That's a version specifier, not a tag name.

It's what the next release of enzyme will use: https://github.com/enzymejs/enzyme/blob/master/packages/enzyme/package.json#L42

rxgx

rxgx commented on May 20, 2022

@rxgx

Is it possible that this version is missing from the registry? I get these errors even with rm node_modules and npm cache verify. OR something up with npm itself

lerna notice cli v4.0.0
lerna ERR! cheerio: No matching version found for cheerio@1.0.0.rc-3.
lerna ERR!     at module.exports (/Users/rgasparini/Code/monorepo/node_modules/npm-pick-manifest/index.js:209:23)
lerna ERR! lerna No matching version found for cheerio@1.0.0.rc-3.
$ npm i -D cheerio@1.0.0.rc-3 
npm ERR! code ETARGET
npm ERR! notarget No matching version found for cheerio@1.0.0.rc-3.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.
$ npm i -D cheerio@=1.0.0.rc-3
npm ERR! code EINVALIDTAGNAME
npm ERR! Invalid tag name "=1.0.0.rc-3" of package "cheerio@=1.0.0.rc-3": Tags may not have any characters that encodeURIComponent encodes.
ljharb

ljharb commented on May 20, 2022

@ljharb
Member

Is it possible you're not using the public registry? It's quite there: https://unpkg.com/browse/cheerio@1.0.0-rc.3/

rxgx

rxgx commented on May 20, 2022

@rxgx

Here's what I originally thought was the resolution before being derailed by the invalid = syntax. I got the hyphen and dots mixed up.

  "overrides": {
    "enzyme": {
      "cheerio": "1.0.0-rc.3"
    }
  },

If you manually change the version of the dependencies or devDependencies, your lock file is unfortunately going to be out of sync.

ljharb

ljharb commented on May 20, 2022

@ljharb
Member

The = syntax is a range; overrides takes a specific version - it’s not invalid in general, i just wasn’t providing an override. Overrides should be used only as a last resort.

nspector

nspector commented on May 20, 2022

@nspector

Looks like the change to pin it here: cafdb2b
was never published: https://unpkg.com/browse/enzyme@3.11.0/package.json. Can somebody publish it? Thank you!

ljharb

ljharb commented on May 20, 2022

@ljharb
Member

@nspector correct; it hasn't been published yet. it'll be in the next release.

Again, you can get the benefit now merely by doing npm install --save-dev cheerio@=1.0.0-rc.3.

tonnyskk

tonnyskk commented on May 21, 2022

@tonnyskk

Try lock your cheerio version to 1.0.0-rc.10 E.g. by yarn resolutions. 1.0.0-rc.11 will throw the error.

billnbell

billnbell commented on May 22, 2022

@billnbell

This worked for me

   "resolutions": {
+    "cheerio": "1.0.0-rc.10"
   },

25 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Cannot find module 'cheerio' from 'node_modules/enzyme/build/Utils.js' · Issue #2518 · enzymejs/enzyme