我使用 NPM 进行依赖管理,使用 Angular5 和 TypeScript。
编辑我还没有从 npm 安装加密包,我正在引用节点内置包。
无论我做什么,“crypto”包都会解析为一个空对象。我已经安装了节点 @typings 包,并且可以在 node_modules/@typings 文件夹中看到 @typings 定义加密的定义。
我试过的:
import * as cryp from 'crypto'
declare var crypto:any
declare module crypto
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. ========