npm 包有依赖错误,即使在我手动安装它们之后
Posted
技术标签:
【中文标题】npm 包有依赖错误,即使在我手动安装它们之后【英文标题】:npm package has dependency errors, even after I manually install them 【发布时间】:2019-06-13 14:52:07 【问题描述】:我正在尝试在我的 Angular Web 应用程序中使用来自 NPM 的 Imgur package。安装后,一切似乎都很好。即使在导入之后。但是,一旦使用包初始化变量,我就会收到编译错误,声称我的代码中存在解析错误,例如“加密”。 NPM 版本:5.6.0 角度版本:6.2.9
您知道是什么原因导致了这个问题吗?关于如何修复它的任何想法?
我尝试安装看似丢失的包,但当我再次尝试编译时,它们仍然被视为丢失。 我还尝试将我的节点版本降级到旧版本,但无济于事。 也没有删除我的 node_modules 并重新安装它们。
这是我在打字稿中用来初始化模块的代码:
import Component, OnInit from '@angular/core';
import * as Convert from 'color-convert';
import * as Hex from 'hex2dec';
import * as ClarifaiSVC from '../clarifai_service/clarifai.service';
import * as Imgur from 'imgur';
@Component(
selector: 'app-imagery',
templateUrl: './imagery.component.html',
styleUrls: ['./imagery.component.css']
)
export class ImageryComponent implements OnInit
constructor(private Clarifai: ClarifaiSVC.ClarifaiService, private Imgur: Imgur)
这是服务后 npm 日志中的内容:
ERROR in ./node_modules/aws-sign2/index.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\aws-sign2'ERROR in ./node_modules/aws4/aws4.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\aws4'
ERROR in ./node_modules/ecc-jsbn/index.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\ecc-jsbn'
ERROR in ./node_modules/http-signature/lib/verify.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\http-signature\lib'
ERROR in ./node_modules/http-signature/lib/signer.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\http-signature\lib'
ERROR in ./node_modules/oauth-sign/index.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\oauth-sign'
ERROR in ./node_modules/request/lib/oauth.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\request\lib'
ERROR in ./node_modules/request/lib/helpers.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\request\lib'
ERROR in ./node_modules/request/lib/hawk.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\request\lib'
ERROR in ./node_modules/sshpk/lib/identity.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\sshpk\lib'ERROR in ./node_modules/sshpk/lib/utils.js
ERROR in ./node_modules/sshpk/lib/formats/openssh-cert.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\sshpk\lib\formats'
ERROR in ./node_modules/sshpk/lib/formats/ssh-private.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\sshpk\lib\formats'
ERROR in ./node_modules/fs.realpath/old.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\fs.realpath'
ERROR in ./node_modules/request/lib/har.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\request\lib'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'http' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\tunnel-agent'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'https' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\forever-agent'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'https' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\request'
--------------------------------------------
--------------------------------------------
This just goes on for a while
--------------------------------------------
--------------------------------------------
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\request'
ERROR in ./node_modules/sshpk/lib/ed-compat.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard\node_modules\sshpk\lib'ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'zlib' in 'C:\Users\Gilian\Documents\School\3EA2_18-19\Internet Of Things\src\dashboard\dashboard
【问题讨论】:
您可以尝试删除您的 node_modules 并尝试再次运行npm install
。
@Abhishek 我刚试过,还是一样
我相应地编辑了问题
您使用的是哪个版本的 Node.js?
@laptou 我在节点版本 8.11.2
【参考方案1】:
Crypto is a built-in Node.js module。它不是普通的 javascript。
imgur
模块不是为在 Web 浏览器中运行而设计的,因此它不适用于 Angular。它依赖于很多 Node.js 特定的 API(例如文件写入)。
【讨论】:
这似乎很奇怪,因为它专门提供了使用模块编写 javascript 应用程序的示例。 @MyNameIsGuzse — 为什么 Node.js 的模块使用 JavaScript 很奇怪? Node.js 的全部意义在于它运行用 JavaScript 编写的程序。 我想我几乎没见过 javascript 在 Web 开发之外使用。这确实意味着我有一个问题,因为我不知道如何单独通过打字稿连接到 imgur API... 我用谷歌搜索了 imgur api,this 是第一个点击。那里有一个指向 Example HTML5/JavaScript app 的链接。【参考方案2】:您的安装似乎已损坏。最好的方法是手动删除node_modules
文件夹并重新运行 npm install。这应该在全新安装后工作。
【讨论】:
我刚刚做了,但我仍然得到同样的错误。我应该将此添加到原始帖子中。【参考方案3】:请删除node_modules
并再次运行npm install
...
除了imgur,你还必须安装它的类型。
npm install @types/imgur
【讨论】:
并且不要忘记在删除node_modules
目录后运行npm cache clean -f
。
我刚试过,重新安装 node_modules 没有帮助。尝试安装@types/imgur 时,我得到404 Not Found: @types/imgur@latest
@kav 清理缓存后,我根本无法重新安装 node_modules。现在我收到一个错误,上面写着npm ERR! cb() never called! npm ERR! This is an error with npm itself. Please report this error at: npm ERR! <https://github.com/npm/npm/issues>
以上是关于npm 包有依赖错误,即使在我手动安装它们之后的主要内容,如果未能解决你的问题,请参考以下文章