Definitelytyped: @types/chai-http – Can't use ES6 import

Created on 31 Aug 2017  Â·  10Comments  Â·  Source: DefinitelyTyped/DefinitelyTyped

  • [x] I tried using the @types/chai-http package and had problems.
  • [x] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [x] I mentioned the authors (see Definitions by: in index.d.ts) so they can respond.

    • Authors: @Nemo157, @G1itcher, @CaselIT


While I'm able to import chai-http using const chaiHttp = require('chai-http'), I can't do the same thing with ES6 imports.

import chaiHttp from 'chai-http'

Module '"/mnt/data/Projects/Node.js/express-decorators/node_modules/@types/chai-http/index"' has no default export.

import * as chaiHttp from 'chai-http'

Module '"/mnt/data/Projects/Node.js/express-decorators/node_modules/@types/chai-http/index"' resolves to a non-module entity and cannot be imported using this construct.

import { chaiHttp } as chaiHttp from 'chai-http'

Module '"/mnt/data/Projects/Node.js/express-decorators/node_modules/@types/chai-http/index"' has no exported member 'chaiHttp'.

Module '"/mnt/data/Projects/Node.js/express-decorators/node_modules/@types/chai-http/index"' resolves to a non-module entity and cannot be imported using this construct.


I found this in chai-http's code:

module.exports = function (chai, _) { ... }

Isn't this a default export and shouldn't a default import (my first attempt) work then?

Most helpful comment

My two cents:

import * as chai from 'chai';
import chaiHttp = require('chai-http');

chai.use(chaiHttp);

This abomination seems to work for me for code completion and does not trigger any warnings or errors.

All 10 comments

No,

module.exports = function (chai, _) { ... }

is not a default export since it does not export a default property.

A fake namespace could be added in the definition, so you could use

import * as chaiHttp from 'chai-http'

but I'm not sure if there are some guidelines about it.

@CaselIT That would be great, because that's also the way I import chai for example.

Any progress on this yet? I'm getting typescript errors above ...resolves to a non-module entity and cannot be imported using this construct using import and if I do let chai = require('chai-http') I get an error property chai.request does not exists on ChaiStatic. Kind of a catch-22.

anything I can do to help on this one?

I guess until chai-http is not updated to support the es6 style import this issue will not be fixed
https://github.com/DefinitelyTyped/DefinitelyTyped/pull/22052#issuecomment-351538321

The way I currently use chai-http is:

chai.use(require('chai-http'));

I know it's not hugely pretty, but it does the job and is readable

The @G1itcher solution allows ts to transpile but I could not have (vscode) completion without adding import 'chai-http', so I currently have something like:

import 'chai-http';
import * as chai from 'chai';
chai.use(require('chai-http'));

My two cents:

import * as chai from 'chai';
import chaiHttp = require('chai-http');

chai.use(chaiHttp);

This abomination seems to work for me for code completion and does not trigger any warnings or errors.

My two cents:

import * as chai from 'chai';
import chaiHttp = require('chai-http');

chai.use(chaiHttp);

This abomination seems to work for me for code completion and does not trigger any warnings or errors.

Just for documentation purpose since google send me here.
The solution by @MIrinkov is correct [1], see this typescript issue/comment.

In-depth explanations can be found in this blog post and in the typescript 2.7 release notes (see support for commonjs module section).

The TL;DR version:

Normally you would use import * as chaiHttp from 'chai-http' to import commonjs/nodejs modules with typescript. However this does not work if the exported content is a function, since this violates ES6 module specification and thus fails with a warning from typescript:

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


1: The alternative is to enable the esModuleInterop in typescript (through cli or tsconfig.json), which is the default for new typescript (3.0.0+) projects.
This allows you to use the ES6 spec complient syntax (just like with babel):
import chaiHttp from 'chai-http'

The only issue I'm currently having with this is that the Atom Typescript plugin complains about node_modules/chai-http/types/index having no default export.
Transpiling with _tsc_ on the other hand works without errors and tests seem to execute.

I have "esModuleInterp" set to true and calling
import * as chaiHttp from 'chai-http';

VScode show following error

TSError: ⨯ Unable to compile TypeScript:
test/xxx.tests.ts:5:27 - error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.

The above error goes away when I do import chaiHttp from 'chai-http' but then I get error - TypeError: chai.request is not a function

Fixed by doing following

import chai from 'chai';
import chaiHttp from 'chai-http';
Was this page helpful?
0 / 5 - 0 ratings