Cannot find chai module, although it exists in node modules folder

I tried installing chai using the following command.

npm install --save-dev chai 

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

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

It produces the following 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 see the chai folder inside the node_modules folder.

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

+7
javascript npm typescript node-modules chai
source share
2 answers

I did not set typing for chai, that is, I did not pass chai from DefinitelyTyped, but set it as a node module. So I had to call it using the require statement in my typescript code.

  var chai = require('chai'); 
+1
source share

You are probably using something like Babel backstage to migrate to ES5.

If so, it actually runs require on top of Node, a CommonJS style.

This way you can succeed if you just add the path with ./ just like in CommonJS.

Please try, I hope this fixes your problem:

 import {assert} from './chai'; import {expect} from './chai'; 
0
source share

All Articles