This took me some time to figure out, a documentation update might help.
TLDR:
You need to change
<script src="../node_modules/chai/chai.js"></script>
to
<script src="../node_modules/chai/chai.js" type="module"></script>
If you are using imperative Chai API calls like chai.should() you must use it in your own ESMs like this:
import {should} from '../node_modules/chai/chai.js';
should(); // was: chai.should()
The rest is documenting my journey to get it working again (I thought I couldn't fix this and wanted to file a bug for that):
Chai stopped working in the browser.
Old setup:
<script src="../node_modules/chai/chai.js"></script>
This now fails with
Uncaught SyntaxError: Unexpected token 'export' (at chai.js:3719:1)
Making it a module:
<script src="../node_modules/chai/chai.js" type="module"></script>
Fails with
Uncaught ReferenceError: chai is not defined
because it does no longer define the global "chai" (im using chai.should(), otherwise this might work)
Next try:
import {chai} from '../node_modules/chai/chai.js';
->
Uncaught SyntaxError: The requested module '../node_modules/chai/chai.js' does not provide an export named 'chai'
Aaaah, got it:
import {should} from '../node_modules/chai/chai.js';
should(); // was: chai.should()
This took me some time to figure out, a documentation update might help.
TLDR:
You need to change
to
If you are using imperative Chai API calls like
chai.should()you must use it in your own ESMs like this:The rest is documenting my journey to get it working again (I thought I couldn't fix this and wanted to file a bug for that):
Chai stopped working in the browser.
Old setup:
This now fails with
Making it a module:
Fails with
because it does no longer define the global "chai" (im using
chai.should(), otherwise this might work)Next try:
Aaaah, got it: