gpt4 book ai didi

javascript - Node - 需要 ('crypto' ) 解析为 { }

转载 作者:行者123 更新时间:2023-11-30 11:26:56 25 4
gpt4 key购买 nike

我使用 NPM 进行依赖管理,并使用 Angular5 和 TypeScript。

编辑 我没有从 npm 安装加密包,我引用的是 Node 内置包。

无论我做什么,“crypto”包都会解析为一个空对象。我已经安装了 node @typings 包,可以在 node_modules/@typings 文件夹中看到 @typings definition crypto 的定义。

我尝试过的:

从“crypto”导入 * 作为 cryp

声明 var crypto:any

声明模块加密

var cryp = require('crypto')

我尝试删除模块文件夹并重新安装。

我尝试将所有内容更新到最新版本。

我尝试创建一个新项目,并且只使用没有其他依赖项的加密。运气不好。

这里最大的问题是我们正在尝试使用依赖于这个“加密”模块的库,没有它它们就无法运行。

最令人困惑的是它不会抛出任何错误,而且它不是未定义的,而是一个空对象。它在项目内或项目外都不起作用。其他 Node JS/TS 模块工作正常,它实际上(到目前为止)只是有这个问题的“加密”。这里的任何指导或想法都会很棒。

主要使用 ng serve 来运行应用程序,但也尝试过直接使用 node 打包和运行。

根据@kendor 的建议,我运行了 tsc --traceResolution。这是关于加密的部分。有趣的是,它说“node_modules”不存在,但它显然存在,并且其他包正在正确解析它。

======== Resolving module 'crypto' from '[...]/src/app/app.component.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
Loading module 'crypto' from 'node_modules' folder, target file type 'TypeScript'.
Directory '[...]/src/app/node_modules' does not exist, skipping all lookups in it.
Directory '[...]/src/node_modules' does not exist, skipping all lookups in it.
File '[...]/node_modules/crypto.ts' does not exist.
File '[...]/node_modules/crypto.tsx' does not exist.
File '[...]/node_modules/crypto.d.ts' does not exist.
File '[...]/node_modules/@types/crypto.d.ts' does not exist.
Directory '[...]/node_modules' does not exist, skipping all lookups in it.
Directory '[...]node_modules' does not exist, skipping all lookups in it.
Directory '/home/node_modules' does not exist, skipping all lookups in it.
Directory '/node_modules' does not exist, skipping all lookups in it.
Loading module 'crypto' from 'node_modules' folder, target file type 'JavaScript'.
Directory '[...]/src/app/node_modules' does not exist, skipping all lookups in it.
Directory '[...]/src/node_modules' does not exist, skipping all lookups in it.
File '[...]/node_modules/crypto.js' does not exist.
File '[...]/node_modules/crypto.jsx' does not exist.
Directory '[...]/node_modules' does not exist, skipping all lookups in it.
Directory '[...]node_modules' does not exist, skipping all lookups in it.
Directory '/home/node_modules' does not exist, skipping all lookups in it.
Directory '/node_modules' does not exist, skipping all lookups in it.
======== Module name 'crypto' was not resolved. ========

最佳答案

为了回答您的问题,我试图找到库代码。在搜索它时,我注意到这个库已被弃用,因此你很可能没有任何运气。看这里:https://www.npmjs.com/package/crypto

如果您想确保是这种情况,只需检查 node_modules/crypto 文件夹及其中的代码。如果可用,请与我们分享 :-)。

编辑:正如@Jaromanda X 在评论中指出的(并写在 npm 页面上),它已被弃用为 npm 包并添加到 nodejs 本身 ( https://nodejs.org/docs/latest-v7.x/api/crypto.html#crypto_crypto )。但是他们仍然阻止它的名称以避免恶意使用。

编辑 2:这个答案假设他安装了 crypto npm 包(在工作区或全局),这可能会干扰内置的 crypto 包。

关于javascript - Node - 需要 ('crypto' ) 解析为 { },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47664471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com