l2beat / earl

☕ Ergonomic, modern and type-safe assertion library for TypeScript
https://earl.fun/
MIT License
256 stars 19 forks source link

ESM does not work #275

Closed m-ronchi closed 1 year ago

m-ronchi commented 1 year ago

if I try to use earl from inside an ESM module, mocha fails with the fllowing error:

import { expect } from "earl";
         ^^^^^^
SyntaxError: Named export 'expect' not found. The requested module 'earl' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'earl';
const { expect } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

thinking it is a commonjs because of the missing "type": "module" in package.json.

if I modify the import as suggested, I get a different error:

(node:40437) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

/Users/*****/node_modules/earl/dist/esm/index.js:1
export * from './Control';
^^^^^^

SyntaxError: Unexpected token 'export'
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1176:20)
    at Module._compile (node:internal/modules/cjs/loader:1218:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Object.require.extensions.<computed> [as .js] (/Users/*****/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
sz-piotr commented 1 year ago

That's probably related to the fact that transpilation to esm doesn't add .js into extensions. This will definitely need to be fixed.

sz-piotr commented 1 year ago

Fixed in v1.0.1.

Please reopen if the issue persists, but I hope that I've tested it enough so that it works :P