Open dhjnam opened 7 months ago
This is because the project is esm only now.
As the error message says, you would have to use a dynamic import to import it since you're in a commonjs project.
Your package.json doesn't have type: "module"
, so typescript (with node16
/nodenext
resolution) will detect your project as commonjs and parse it as such.
I have an example here: https://gist.github.com/43081j/78ce1392abb5043b02a29355006880a5
if you're unable to make your project ESM-only, you will have to use a dynamic import (const chai = await import('chai')
), or stick to 4.x until you're able to migrate forward.
We're still publishing 4.x versions for security etc.
With mocha I did someting like so:
const chaiHttp = require("chai-http");
let chai;
before(async () => {
await import("chai").then((result) => {
chai = result;
// Configure chai
chai.use(chaiHttp);
chai.should();
});
});
issue is that somehow chai.request is failing...
TypeError: chai.request is not a function
is chai-http 4.4.0 not compatible with chai 5.x?
Okay it seems with the new version the use functionality is broken... https://github.com/chaijs/chai/issues/1569 interesting
for now (maybe permanently, depending on the outcome of 1569), you can do:
await import("chai").then((result) =>
chai = result.use(chaiHttp);
});
You can try import just methods from 'chai' like this:
import { use, expect } from 'chai';
import chaiHttp from 'chai-http';
const chai = use(chaiHttp);
I can't take it anymore with Chai + Cha-http with ESM (even when not using TS, but just JS).
It just impossible to import those two libs. I get SyntaxError: The requested module 'chai' does not provide an export named 'request'
and if I use import * as chai from 'chai
to allow me to use chai.request
I now get: TypeError: chai.request is not a function
I can't take it anymore with Chai + Cha-http with ESM (even when not using TS, but just JS).
It just impossible to import those two libs. I get
SyntaxError: The requested module 'chai' does not provide an export named 'request'
and if I useimport * as chai from 'chai
to allow me to usechai.request
I now get:TypeError: chai.request is not a function
You can try this way:
import chaiHttp from 'chai-http';
const chai = use(chaiHttp);
and then call:
chai.request()
Ow so use
returns a new object?.. interesting! Thanks. I still believe chai should improve this ESM import / usage.
yes, use
returns an extended chai
import {use} from 'chai';
import chaiHttp from 'chai-http';
const chai = use(chaiHttp);
we have an issue open to investigate if there's any way we can improve this pattern
import chai from 'chai'
does not work with chai version 5.0.0.and executing
Raises the following error
Other imports work fine.
tsconfig.json
:package.json
: