Search code examples
javascripttypescriptnpmchainode-modules

cannot find module chai though it exists in node modules folder


I tried install chai using the following command.

 npm install --save-dev chai

Then I ran my unit test class with the following imports.

import {assert} from 'chai';
import {expect} from 'chai';

It throws the below errors.

test\main\MessageBroker.spec.ts(3,22): error TS2307: Cannot find module 'chai'.
[05:38:45] [Typescript] TypeScript error: test\main\MessageBroker.spec.ts(3,22): error TS2307: Cannot find module 'chai'.
test\main\MessageBroker.spec.ts(4,22): error TS2307: Cannot find module 'chai'.
[05:38:45] [Typescript] TypeScript error: test\main\MessageBroker.spec.ts(4,22): error TS2307: Cannot find module 'chai'

What am I doing wrong here? I can see chai folder inside node_modules folder as well.

When I say var chai = require('chai'); it works! why doesn't import work?


Solution

  • I have not installed a typing for chai, that is I haven't referred chai from DefinitelyTyped but have installed it as a node module. Therefore I had to call it using a require statement in my typescript code.

     var chai = require('chai');