为啥引用的模块没有解析?

Posted

技术标签:

【中文标题】为啥引用的模块没有解析?【英文标题】:why aren't referenced modules resolving?为什么引用的模块没有解析? 【发布时间】:2019-09-30 17:57:03 【问题描述】:

我有两个电子角度应用程序。一个是来自 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\node_modules\opener\lib”中的“child_process” ./node_modules/@openid/appauth/built/node_support/crypto_utils.js 中的错误 找不到模块:错误:无法解析“Z:\TheApp\angular-electron\node_modules@openid\appauth\built\node_support”中的“crypto” ./node_modules/@openid/appauth/built/node_support/node_request_handler.js 中的错误 未找到模块:错误:无法解析“Z:\TheApp\angular-electron\node_modules@openid\appauth\built\node_support”中的“http” ./node_modules/@openid/appauth/node_modules/follow-redirects/index.js 中的错误 未找到模块:错误:无法解析“Z:\TheApp\angular-electron\node_modules@openid\appauth\node_modules\follow-redirects”中的“http” ./node_modules/@openid/appauth/node_modules/follow-redirects/index.js 中的错误 未找到模块:错误:无法解析“TheApp\angular-electron\node_modules@openid\appauth\node_modules\follow-redirects”中的“https” ./node_modules/@openid/appauth/node_modules/opener/lib/opener.js 中的错误 未找到模块:错误:无法解析“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

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

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

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

不断出现。

所以现在我正在查看...以及此链接。

Field 'browser' doesn't contain a valid alias configuration

与构建(角电子主)详细输出的项目包括

[child_process] 外部“child_process” 42 字节 main [内置] cjs 需要 child_process [./node_modules/opener/lib/opener.js] 2:19-43 [crypto] 外部“crypto”42 字节 main [内置] 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"

谢谢 马丁

【问题讨论】:

【参考方案1】:

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

的返回语句中
function getBrowserConfig(wco)

包括此设置...

 target: "electron-renderer"

【讨论】:

以上是关于为啥引用的模块没有解析?的主要内容,如果未能解决你的问题,请参考以下文章

为啥在 Visual Studio 中构建失败,因为它无法从引用的程序集中解析命名空间?

为啥我在 Kotlin 中键入视图名称/ID 时会出现“未解析的引用”错误?

链接器如何使用静态库解析引用

使用 kotlin-android-extensions 从子模块导入视图时未解析的引用

未引用函数的“未解析的外部符号”

为啥我的 graphql apollo 解析器没有被调用?