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

Error: Cannot find module '/xxxxxxx/node_modules/chai/register-should' #1455

Closed
yoshinorin opened this issue Feb 12, 2022 · 13 comments · Fixed by #1611
Closed

Error: Cannot find module '/xxxxxxx/node_modules/chai/register-should' #1455

yoshinorin opened this issue Feb 12, 2022 · 13 comments · Fixed by #1611

Comments

@yoshinorin
Copy link

yoshinorin commented Feb 12, 2022

Hi :)

I'm facing as is the title issue.

Env & Phenomenon

I confirmed this problem happened in chai 4.2.0, 4.6.3 and Node.js 12.x, 14.x, 16.x. I do not check other chai and node.js versions.

I'm using chai with mocha and I specified chai/register-should in mocha.rc.yml.

// mocha.rc.yml
require: 
  - "chai/register-should"

Also this problem occur with mocha spec.js -r chai/register-should command.

Error log

 ERROR: Error: Cannot find module '/home/runner/work/hexo/hexo/node_modules/chai/register-should'
    at createEsmNotFoundErr (internal/modules/cjs/loader.js:929:15)
    at finalizeEsmResolution (internal/modules/cjs/loader.js:922:15)
    at resolveExports (internal/modules/cjs/loader.js:450:[14](https://github.com/hexojs/hexo/runs/5167084831?check_suite_focus=true#step:5:14))
    at Function.Module._findPath (internal/modules/cjs/loader.js:490:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:888:27)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:101:[18](https://github.com/hexojs/hexo/runs/5167084831?check_suite_focus=true#step:5:18))
    at exports.requireOrImport (/home/runner/work/hexo/hexo/node_modules/mocha/lib/nodejs/esm-utils.js:60:[20](https://github.com/hexojs/hexo/runs/5167084831?check_suite_focus=true#step:5:20))
    at async exports.handleRequires (/home/runner/work/hexo/hexo/node_modules/mocha/lib/cli/run-helpers.js:94:28)
    at async /home/runner/work/hexo/hexo/node_modules/mocha/lib/cli/run.js:353:[25](https://github.com/hexojs/hexo/runs/5167084831?check_suite_focus=true#step:5:25) {
  code: 'MODULE_NOT_FOUND',
  path: '/home/runner/work/hexo/hexo/node_modules/chai/package.json'
}
npm ERR! Test failed.  See above for more details.

Others

This issue seems fixed in 5276af6. Because, I specify the commit in package.json like below this problem is solved.

"devDependencies": {
  "chai": "git+https://github.com/chaijs/chai.git#5276af683ca4f6699768b4401dcf4c7c331e83f7",

But, it commit reverted in 022c2fa. So, how to solve it?

Thank you :)

@GCastilho
Copy link

I'm having the same issue

Another workaround would be:

- import 'chai/register-should'
+ import { should } from 'chai'
+ should()

@mbargiel
Copy link

mbargiel commented Apr 6, 2022

We ran into the same problem with register-expect. With chai@4.2.0 it's working fine, with chai@4.3.6 we have a similar error about Node being unable to resolve .../chai/register-expect.

We found a workaround: add .js, ie replace require('chai/register-expect') with require('chai/register-expect.js').

The issue seems to be that an exports property added to the chai package.json, which might cause the Node module resolver to switch to ESM mode, which is strict about extensions (it doesn't try to guess if you don't provide one)... Or something like that.

@KyleMit
Copy link

KyleMit commented Jun 23, 2022

I get this issue when running the following CLI command:

npx mocha --no-deprecation --require chai/register-expect --require ts-node/register "src/**/*.ts"
  • 4.3.4 ✔ works fine
  • 4.3.5 ❌ broken

Ideally, in my case, I'm including chai expect by default on all TS files, so I don't have an easy way to do the workaround identified above by re-writing my imports

@aristotelos
Copy link

aristotelos commented Nov 9, 2023

Experiencing the same problem on "chai": "^4.3.10" with chai/register-should.

Both import 'chai/register-should'; and "require": ["ts-node/register", "chai/register-should"] in Mocha configuration do not work.

However, using "require": ["ts-node/register", "chai/register-should.js"] in Mocha configuration does work!

Using

import { should } from "chai";
should();

does work.

@JakobJingleheimer
Copy link

As a workaround for globally registering expect:

// mocha.setup.mjs

import { expect } from 'chai';

global.expect = expect;
mocha -r ./mocha.setup.mjs …

@43081j
Copy link
Contributor

43081j commented Jan 10, 2024

all of the above are wrongly using the extensionless import, it seems most or all of you will resolve your issues by using the exact path: chai/register-should.js

this works:

import 'chai/register-should.js';

similarly, this works:

mocha --require chai/register-should.js some-test.js

in both 5.x and 4.x these work

@jacob-orbiit
Copy link

Probably because that's what Chai's README says to do:

image

If packageJson.exports was configured, the README's instructions would work :)

@43081j
Copy link
Contributor

43081j commented Jan 10, 2024

good catch, we should update that 👍

fwiw, if we had exports, it is unlikely we'd alias the extensionless paths anyway as the "proper" import going forward is the one with an extension (in node and browsers)

@JakobJingleheimer
Copy link

good catch, we should update that 👍

I can send a PR tonight

@43081j
Copy link
Contributor

43081j commented Jan 10, 2024

if you don't mind, that would be much appreciated 🙏

feel free to ping me for review

@JakobJingleheimer
Copy link

I just tried including the .js in the import specifier, and it still didn't work:

ERROR: /[…]/node_modules/chai/register-expect.js:1
import {expect} from './index.js';
        ^^^^^^

SyntaxError: The requested module './index.js' does not provide an export named 'expect'

There is a series/daisy-chain of barrel files, which make debugging this rather difficult (and probably has unintended consequences like causing all modules to get evaluated, regardless of whether they're used). I think you would be much better off using packageJson.exports like so:

{
  "exports": {
    "./": "./chai.js",
    "./package.json": "./package.json",
    "./register-assert": "",
    // …
  }
}

PS sorry for the delay—I had the wrong laptop with me last night.

@43081j
Copy link
Contributor

43081j commented Jan 11, 2024

if we introduce an exports map, it will still be of the JS files most likely rather than aliases.

could you possibly make a reproduction in a repo somewhere, or a gist? just so i can see the setup as a whole

would help me understand as i'm not seeing the same behaviour

@JakobJingleheimer
Copy link

Oh! Sorry i missed your response. Looks like it's been handled though.

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

Successfully merging a pull request may close this issue.

8 participants