关于cannot find module 'xxxx’的一个可能解决方法。

关于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方式查找,问题解决。

相关连接: https://www.typescriptlang.org/docs/handbook/module-resolution.html

关于cannot find module 'xxxx’的一个可能解决方法。

时间: 2024-08-24 18:30:41

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

ubuntu下安装nginx错误error: the HTTP rewrite module requires the PCRE library 解决方法

本文为大家讲解的是ubuntu下安装nginx错误error: the HTTP rewrite module requires the PCRE library 解决方法,感兴趣的同学参考下. 本文为大家讲解的是ubuntu下安装nginx错误error: the HTTP rewrite module requires the PCRE library 解决方法,感兴趣的同学参考下. 错误描述: ubuntu安装nginx时提示error: the HTTP rewrite module r

Intellij-出现Module ** must not contain source root **. The root already belongs to module **这类错误的解决方法

出现这个问题的原因是,多模块Maven项目,parent目录不能不能有src的code.找到parent 的 Source Folders看看是否把module的给加载进来了.删掉就可以了. 参考: [1]IDEA出现Module ** must not contain source root **. The root already belongs to module **这类错误的解决方法 原文地址:https://www.cnblogs.com/happyflyingpig/p/12267

Error loading MySQLdb module: No module named MySQLdb 错误的解决方法

具体报错如下: django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb 解法办法: 需要安装python连接mysql的模块 注意:本人使用yum install MySQL-pyton安装后依然报上错误,经测试必须要pip方式安装 pip install mysql-python 如果安装报以下错误: pip install pandas` gives

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

<style> li{ list-style: none;width:40px;height: 40px;text-align:center;line-height: 40px;cursor: pointer; } </style> html代码: <ul id="uls">    <li style="background:#aaa">0</li>    <li style="backgr

Hibernate createSQLQuery 查询char类型结果为一个字符解决方法汇总

在使用Hibernate的原生态SQL对Oracle进行查询时,碰到查询char类型的时候始终返回的是一个字符,开始认为应该是Hibernate在做映射的把数据类型给映射成char(1),在经过查找网上的一些资料,得知产生这个问题的主要原因确实是Hibernate再查询Oracle的时候,将char自动映射成character(varchar的子集)类型,oracle的char字段在hibernate里映射为character类型,是varchar的子集. 现有以下几种解决方法: 1:将你要查询

dpkg: 处理软件包 XXXX (--configure)时出错解决方法

Can't exec "md5sum": 没有那个文件或目录 at /usr/share/perl/5.24/IPC/Open3.pm line 178, <STDIN> line 11. open2: exec of md5sum failed: No such file or directory at /usr/sbin/pam-auth-update line 394. dpkg: 处理软件包 libpam-systemd:amd64 (--configure)时出错

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

解决方法在此: https://github.com/dbzhang800/QtXlsxWriter Documentation: http://qtxlsx.debao.me QtXlsx is a library that can read and write Excel files. It doesn't require Microsoft Excel and can be used in any platform that Qt5 supported. The library can b

the import XXXX cannot be resolved 解决方法

明明XXX类完全没问题 突然就报错了 解决方法: 原因一:一个项目引用了兄弟项目的类,报错The import XXX cannot be resolved 解决办法:需要在引用的兄弟项目右键选择Maven项,点击update ProjectConfiguration,ok! 原因二:重构一下jdk,使用自己安装的jdk! 出自:https://blog.csdn.net/xuxile/article/details/78053444 还有就是clean一下 原文地址:https://www.c

关闭虚拟机提示“正在处理另一个任务”解决方法

一. 错误提示:ESXI关闭虚拟机时提示"正在处理另一个任务",无法关闭二. 出现情况:虚拟机突然卡主,没有任何反应,就准备重置虚拟机,结果当重置进行到95%的时候,也卡住了,整整30分钟后提示操作超时,就打算关闭虚拟机,结果提示"正在处理另一个任务",无法关闭,无奈,百度了一下,确实有个好方法,但是呢网上提供的方法还有点小错误,特此整理一下,分享给大家.三. 解决方法:前提是可以不用重启ESXi Server,这样影响范围就小了. 开启esxi的ssh服务 Sec