关于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++实现

    今天面试一个实习生,就想既然是未出校园,那就出一个比较基础的题吧,没想到答的并不如人意,对于树的操作完全不熟悉,因此此题算是未作答.原来我想看一下他分析问题的思路,优化代码的能力.接下来会把最近半年我 ...

  2. Android View事件机制一些事

    本文主要讲述: 自己对View事件机制的一些理解 在项目中遇到的一些坑,解决方案 收集了一些View的事件机制问题 事件的分发原理图 对于一个root viewgroup来说,如果接受了一个点击事件, ...

  3. JSP编译成Servlet(五)JDT Compiler编译器

    通过JSP编译器编译后生成了对应的java文件,接下去要把Java文件编译成class文件.对于这部分完全没有必要重新造轮子,常见的优秀编译工具有Eclipse JDT Java编译器和Ant编译器. ...

  4. Mahout 系列之----共轭梯度

    无预处理共轭梯度 要求解线性方程组 ,稳定双共轭梯度法从初始解 开始按以下步骤迭代: 任意选择向量 使得 ,例如, 对 若 足够精确则退出 预处理共轭梯度 预处理通常被用来加速迭代方法的收敛.要使用预 ...

  5. Visual Studio 2010利用libxl读写excel表格数据

    C++读写数据,一般通过txt文件,但是随着数据量的增大,采集数据时运用excel表格的优势得以逐步体现.本文主要介绍一下运用第三方库libxl,对excel表格数据进行读写.分为三个部分,第一部分是 ...

  6. 漫谈程序员(十一)老鸟程序员知道而新手不知道的小技巧之Web 前端篇

    老鸟程序员知道而新手不知道的小技巧 Web 前端篇 常充电!程序员只有一种死法:土死的. 函数不要超过50行. 不要一次性写太多来不及测的代码,而是要写一段调试一段. UI和编码要同步做. 多写注释方 ...

  7. shell脚本处理长参数的模板

    shell脚本处理长参数的模板 一个shell模板,处理命令行参数,支持长短参数: #!/bin/bash # # FILE: kvm-clone-v2.sh # # DESCRIPTION: Clo ...

  8. android驱动例子(LED灯控制)

    本例子,讲述在android2.1上完全自已开发一个驱动去控制硬件口并写应用测试该驱动,通过这样一个例子,解析android下的驱动开发流程的应用调用流程,可以说是很好的入门引导 要达到的效果:通过a ...

  9. Java进阶(十二)JDK版本错误之Unsupported major.minor version 51.0(jdk版本错误)

    错误:Unsupported major.minor version 51.0(jdk版本错误) 如果在win7下开发项目是使用的jdk版本和项目运行服务器jdk版本不同就会出现上面的问题. 用jdk ...

  10. didLoadFromCCB方法的调用顺序

    该方法运行顺序和其(包含)继承体系顺序的逆序相同. 这意味着孩子的didLoadFromCCB将总是在其父的didLoadFromCCB之前调用. 比如GameScene.ccb中含有GameMenu ...