简体   繁体   English

节点模块文件夹中存在模块柴,但找不到模块

[英]cannot find module chai though it exists in node modules folder

I tried install chai using the following command. 我尝试使用以下命令安装chai。

 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. 我也可以在node_modules文件夹中看到chai文件夹。

When I say var chai = require('chai'); 当我说var chai = require('chai'); it works! 有用! why doesn't import work? 为什么不导入工作?

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. 我尚未安装chai的类型,即我尚未从DefinitelyTyped引用chai,但已将其安装为节点模块。 Therefore I had to call it using a require statement in my typescript code. 因此,我必须在我的打字稿代码中使用require语句来调用它。

 var chai = require('chai');

You are probably using something like Babel behind the scenes to transpile to ES5. 您可能在后台使用Babel之类的东西来移植到ES5。

If that is the case it's actually executing require over Node, CommonJS style. 如果是这种情况,它实际上是在CommonJS风格的Node上执行require

This way you may succeed if you just append the path with ./ just like we do in CommonJS. 这样,如果您像在CommonJS中那样在路径./加上./就可以成功。

Please give it a try, I hope this fix your issue: 请尝试一下,希望这可以解决您的问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM