关于cannot find module 'xxxx’的一个可能解决方法。
由于学习angular2,想单独学习一下typescript下angular2使用的‘rxjs’是怎么使用的,我用npm自己安装了rxjs,并使用了如下语句
import { Observable } from 'rxjs';
报错如下:
cannot find module 'rxjs',
但是同样的语句在angular/cli生成的angular项目下是不报错的,我找了半天,各种解决方法都不适用于我遇到的情况。由于这个是typescript import时报的错误,我查看了typescript module对应的讲解:
Module Resolution Strategies
There are two possible module resolution strategies: Node and Classic. You can use the --moduleResolution flag to specify the module resolution strategy. If not specified, the default is Classic for --module AMD | System | ES2015or Node otherwise.
也就是说typescript是有两种module管理的方式的,默认的不是node,是classic,我们看看node和classic分别是怎么进行import时的module查找的,我只说non-relative的情况,因为我这里要找node_modules下的第三方包,属于nonrelative:
先说classic:
Classic
This used to be TypeScript’s default resolution strategy. Nowadays, this strategy is mainly present for backward compatibility.

For non-relative module imports, however, the compiler walks up the directory tree starting with the directory containing the importing file, trying to locate a matching definition file.

For example:

A non-relative import to moduleB such as import { b } from "moduleB", in a source file /root/src/folder/A.ts, would result in attempting the following locations for locating "moduleB":

  1. /root/src/folder/moduleB.ts
  2. /root/src/folder/moduleB.d.ts
  3. /root/src/moduleB.ts
  4. /root/src/moduleB.d.ts
  5. /root/moduleB.ts
  6. /root/moduleB.d.ts
  7. /moduleB.ts
  8. /moduleB.d.ts
找的可以看出其并不会找到nodejs的node_modules中,node的方式则会找到nodejs中,类似上面的方式,如果我也是import{b} from "moduleB", 逐级向上查找,代码如下所示:
 
  1. /root/src/node_modules/moduleB.js
  2. /root/src/node_modules/moduleB/package.json (if it specifies a "main" property)
  3. /root/src/node_modules/moduleB/index.js
  4. /root/node_modules/moduleB.js
  5. /root/node_modules/moduleB/package.json (if it specifies a "main" property)
  6. /root/node_modules/moduleB/index.js
  7. /node_modules/moduleB.js
  8. /node_modules/moduleB/package.json (if it specifies a "main" property)
  9. /node_modules/moduleB/index.js
所以在项目tsconfig.json中添加一句话

"moduleResolution": "node",
覆盖掉默认配置classic,将能按node方式查找,问题解决。

关于cannot find module 'xxxx’的一个可能解决方法。的更多相关文章

  1. 输入指令npx webpack-dev-server报错:Error: Cannot find module ‘webpack-cli/bin/config-yargs‘的解决方法

    输入指令npx webpack-dev-server报错:Error: Cannot find module 'webpack-cli/bin/config-yargs'的解决方法 输入指令:npx ...

  2. Qt applendPlainText()/append() 多添加一个换行解决方法

    Qt applendPlainText()/append() 多添加一个换行解决方法 void ConsoleDialog::appendMessageToEditor(const QString & ...

  3. js闭包for循环总是只执行最后一个值得解决方法

    <style> li{ list-style: none;width:40px;height: 40px;text-align:center;line-height: 40px;curso ...

  4. 在android源码环境下写上层应用的一个初步解决方法

    在android源码环境下编写上层应用,一直以来我都觉得很麻烦.因为如果单纯将应用导入eclipse,那么framework层一些定制的API无法自动提示和补全,使用起来不太方便:如果将整个andro ...

  5. the import XXXX cannot be resolved 解决方法

    明明XXX类完全没问题 突然就报错了 解决方法: 原因一:一个项目引用了兄弟项目的类,报错The import XXX cannot be resolved 解决办法:需要在引用的兄弟项目右键选择Ma ...

  6. Unknown module(s) in QT: xlsx解决方法

    解决方法在此: https://github.com/dbzhang800/QtXlsxWriter Documentation: http://qtxlsx.debao.me QtXlsx is a ...

  7. Python学习:ModuleNotFoundError: No module named 'pygal.i18n' 的解决方法

    最近在学<Python编程:从入门到实践>,16.2小结中 from pygal.i18n import COUNTRIES 获取两个字母的国别码,我用的pygal的版本是2.4.0(终端 ...

  8. 数据库查询语句遇到:Unknown column 'XXXX' in 'where clause'解决方法

    数据库查询语句遇到:Unknown colunm 'XXX' in 'where clause'解决方法 根本原因:可能是sql语句所用到的数据类型错误(int与String)弄错- 我的情况: 在网 ...

  9. 虚拟机中安装完Lunix系统后,开机黑屏,只显示一个-,解决方法

    1,查看设置->硬盘是不是SCSI,如果是,先关闭虚拟机,移除该硬盘(实际数据不会删除) 2,添加一个新的虚拟硬盘,最后位置选IDE设备 3,确定,重启虚拟机即可

随机推荐

  1. C语言的布尔类型(_Bool)

    也许很多人都和我一样,不知道现在的C语言已经有了布尔型:从C99标准开始,类型名字为"_Bool". 在此之前的C语言中,使用整型int来表示真假.在输入时:使用非零值表示真:零值 ...

  2. Socket编程实践(1) --TCP/IP简述

    ISO的OSI OSI(open system interconnection)开放系统互联模型是由ISO国际标准化组织定义的网络分层模型,共七层, 从下往上为: OSI七层参考模型 物理层(Phys ...

  3. Socket编程实践(11) --epoll原理与封装

    常用模型的特点 Linux 下设计并发网络程序,有典型的Apache模型(Process Per Connection,PPC), TPC(Thread Per Connection)模型,以及 se ...

  4. Google浏览器设置搜索打开新的标签页

    每次用google搜索的时候,点击进去原来的搜索页面就不见了 ,其实可以设置打开新的标签页 1.登陆Chrome 2.登陆这个网址https://www.google.com/preferences ...

  5. Net和Java基于zipkin的全链路追踪

    在各大厂分布式链路跟踪系统架构对比 中已经介绍了几大框架的对比,如果想用免费的可以用zipkin和pinpoint还有一个忘了介绍:SkyWalking,具体介绍可参考:https://github. ...

  6. (NO.00001)iOS游戏SpeedBoy Lite成形记(十二)

    如果选手能在加速的时候屁股产生推进器效果就跟好了,仿佛选手腾云驾雾的感觉.我们可以用Cocos2D中的CCMotionStreak类来轻松实现.下面在Player.h接口文件添加以下代码: @prop ...

  7. 《java入门第一季》之Arrays类

    前面介绍了排序问题(见博客http://blog.csdn.net/qq_32059827/article/details/51362390):二分查找问题(见博客http://blog.csdn.n ...

  8. Ceres-Solver库入门

    示例1:求极值 首先我们以Ceres库官网中的Hello World例子来进行说明.这里例子的目的是为了计算方程取得最小值时x的值.从这个方程很容易看出来当x=10时,f(x)取得最小值0.这个方程虽 ...

  9. ffdshow 源代码分析 8: 视频解码器类(TvideoCodecDec)

    ===================================================== ffdshow源代码分析系列文章列表: ffdshow 源代码分析 1: 整体结构 ffds ...

  10. Android Studio JNI javah遇到的问题

    好久没写博客了.持之以恒的勋章也被收回了.以后要好好坚持.. 最近在学习jni,但是遇到了一点麻烦的问题.好在终于解决了,便记下来解决一下. 其他入门的jni文章有很多,这里便不在累赘,直接上我遇到的 ...