Error: Cannot find module '/xxxxxxx/node_modules/chai/register-should'
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 https://github.com/chaijs/chai/commit/5276af683ca4f6699768b4401dcf4c7c331e83f7. 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 https://github.com/chaijs/chai/commit/022c2faefc9a86b478392594c9015c3b942892e2. So, how to solve it?
Thank you :)
I'm having the same issue
Another workaround would be:
- import 'chai/register-should'
+ import { should } from 'chai'
+ should()
We ran into the same problem with register-expect. With [email protected] it's working fine, with [email protected] 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.
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
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.
As a workaround for globally registering expect:
// mocha.setup.mjs
import { expect } from 'chai';
global.expect = expect;
mocha -r ./mocha.setup.mjs …
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
Probably because that's what Chai's README says to do:
If packageJson.exports was configured, the README's instructions would work :)
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)
good catch, we should update that 👍
I can send a PR tonight
if you don't mind, that would be much appreciated 🙏
feel free to ping me for review
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.
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
Oh! Sorry i missed your response. Looks like it's been handled though.