Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export

I'm creating my API test framework with TypeScript and I'm new to it.

I created this helper file assertions.ts

import * as chai from 'chai';

interface response {
    status: number
    body: object
}

const assertResponseStatusCode =
    (response:response, statusCode:number) =>
        chai.expect(response.status).to.deep.equals(statusCode);

const assertSuccessResponseStatusCode = (response:response) =>
    assertResponseStatusCode(response, 201)

export = {assertSuccessResponseStatusCode}

Then I started using them on my spec file as bellow

import * as auth from '../Helpers/auth';
import * as assert from '../Helpers/assertions';
import { user } from '../Samples/user';

describe('login', () => {
    it('should return access token with valid credentials', async() => {
        const response = await auth.login(user);
        console.log(response.body);
        assert.assertSuccessResponseStatusCode(response);
    });
});

I'm getting this error This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.. for this line import * as assert from '../Helpers/assertions'; although esModuleInterop is set as true in tsconfig.json.

like image 256
Mona101ma Avatar asked Nov 21 '25 03:11

Mona101ma


1 Answers

I just ran into this too and the other answer gives one solution but I think the better solution in this case is to change:

export = {assertSuccessResponseStatusCode}

To:

export {assertSuccessResponseStatusCode}

The first method is TypeScript's interop with CommonJS modules, but you can only import those like import assertions from './assertions'. The second is an ESM export and can imported as either import { assertSuccessResponseStatusCode } from './assertions' or import * as assertions from './assertions'. It also allows you to have default exports along with the other exports.

like image 135
Brendan Long Avatar answered Nov 22 '25 15:11

Brendan Long



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!