首页 > 解决方案 > 为什么引用的模块没有解析?

问题描述

我有两个电子角度应用程序。一个是来自 GitHub 的 angular-electron-master,另一个是我继承的遗留应用程序,基于 material-dashboard-angular2-master。这两个项目都是以电子形式托管的 Angular 7。

我已经尝试过 angular-electron-master 并添加了 @openid/appauth 。它构建良好。当我进行更改并将它们添加到遗留应用程序时,它不会构建。我收到这些错误:

./node_modules/@openid/appauth/node_modules/opener/lib/opener.js 中的错误 找不到模块:错误:无法解析 'Z:\TheApp\angular-electron\node_modules@openid\appauth\ 中的 'child_process' node_modules\opener\lib' ./node_modules/@openid/appauth/built/node_support/crypto_utils.js 中的错误模块未找到:错误:无法解析 'Z:\TheApp\angular-electron\node_modules@ 中的'crypto' ./node_modules/@openid/appauth/built/node_support/node_request_handler.js 中的 openid\appauth\built\node_support' 错误 找不到模块:错误:无法解析 'Z:\TheApp\angular-electron\ 中的 'http' node_modules@openid\appauth\built\node_support' ./node_modules/@openid/appauth/node_modules/follow-redirects/index.js 中的错误 找不到模块:错误:无法解析'Z中的'http':\TheApp\angular-electron\node_modules@openid\appauth\node_modules\follow-redirects' ./node_modules/@openid/appauth/node_modules/follow-redirects/index.js 中的错误模块未找到:错误:无法解析' https' in 'TheApp\angular-electron\node_modules@openid\appauth\node_modules\follow-redirects' ./node_modules/@openid/appauth/node_modules/opener/lib/opener.js 中的错误 找不到模块:错误:可以t 解析 'Z:\TheApp\angular-electron\node_modules@openid\appauth\node_modules\opener\lib' 中的 'os' ./src/app/services/auth.service.ts 中的错误 找不到模块:错误:可以'Z:\TheApp\angular-electron\src\app\services' 中的'不解析'os' ./node_modules/@openid/appauth/node_modules/follow-redirects/index.js 中的错误 找不到模块:错误:可以'不解决'流'在 'Z:\TheApp\angular-electron\node_modules@openid\appauth\node_modules\follow-redirects'

我可以通过 npm install os 解决 os 依赖项 我可以通过编辑 node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js 来解决其他依赖项,如建议的那样Angular 6 many Can't resolve errors (crypto, fs, http, https, net, path, stream, tls, zlib)

我对修复感到不舒服,因为 angular-electron-master 项目没有它们并且构建成功。另外我不确定 browser.js 是否合适,因为我正在打包为电子独立应用程序。

即使我包含了这些修复程序,我仍然留下了“child_process”。

寻找缺失的模块,这两个项目都包括 node_modules@types\node\index.d.ts ,其中包含声明

declare module "http" {…}
declare module "https" {…}
declare module "stream" {…}
declare module "crypto" {…}
declare module "os" {…}
declare module "child_process" {…}

我不知道为什么这在解决 http 模块时没有用

我对节点模块解析的复杂性很陌生。似乎有很多设置可能会产生影响。

有什么方法可以知道模块在哪里以及为什么被解析?有点像用于 .net 程序集解析的 fusionlog。看起来 tsc 有一个选项 --traceResolution 可能有用。我只需要找到等效的 tsconfig.json 设置。添加

 "traceResolution": true,

到 tsconfig.json 的 compilerOptions 部分会产生大量输出。我将它传送到一个日志文件。搜索该文件(76,000 行),我看到这样的条目

模块“http”被解析为文件“Z:/Application/MDBApp/angular-electron/node_modules/@types/node/index.d.ts”中本地声明的环境模块。

没有我希望的那么有用。除了它在一个层面上告诉我模块正在解决。

现在我已经在全球范围内安装了 angular-cli。

如果我跑

ng build --verbose

我得到有用的信息,我明天会看....

明天:详细信息详细说明了考虑解决的不同文件。消息

字段“浏览器”不包含有效的别名配置

不断出现。

所以现在我正在看那个......还有这个链接。

字段“浏览器”不包含有效的别名配置

使用构建(angular-electron-master)详细输出的项目包括

[child_process] external "child_process" 42 bytes {main} [built] cjs require child_process [./node_modules/opener/lib/opener.js] 2:19-43 [crypto] external "crypto" 42 bytes {main} [built ] cjs 需要加密 [./node_modules/@openid/appauth/built/node_support/crypto_utils.js] 16:13-30

我在失败项目的输出中找不到这样的行。

所以,我稍微简化了失败的项目 - 不完全。并阅读更多内容。我目前认为这个问题是一个 webpack 解决方案,所以接下来看看......

我很欣赏这不是最清楚的情况。希望我的思考过程能帮助一些人。

失望的人投票否决了这个问题。我认为多一点宽容会很好。当您知道如何操作时,一切都很容易。;-)

我尝试使用 nvm 安装 nodejs lte(版本 10.15.3) 我尝试删除 node_modules 目录和 package-lock.json 文件并重新运行 npm install

一些开发依赖项包括

"@angular-devkit/build-angular": "^0.13.8",
"@angular/cli": "7.3.3",
"@angular/compiler-cli": "7.2.15",
"typescript": "3.2.4"

谢谢马丁

标签: angularbuildelectronnode-modules

解决方案


经过大量研究,我发现答案是......编辑 node_modules@angular-devkit\build-angular\src\angular-cli-files\models\webpack-configs\browser.js

在返回语句中

function getBrowserConfig(wco)

包括这个设置...

 target: "electron-renderer"

推荐阅读