【问题标题】:Angular 12 aws-sdk 2.910.0 Error: Module not found: Error: Can't resolve 'util' in '.\.\.\node_modules\aws-sdk\lib'Angular 12 aws-sdk 2.910.0 错误:找不到模块:错误:无法解析 '.\.\.\node_modules\aws-sdk\lib' 中的 'util'
【发布时间】:2022-02-13 20:08:14
【问题描述】:

在从 Angular 11 迁移到 Angular 12 之前,未发生以下错误,并且应用程序能够使用 aws-sdk 库访问我的 S3 存储桶:

./node_modules/aws-sdk/lib/event_listeners.js:572:21-44 - Error: Module not found: Error: Can't resolve 'util' in 'S:\Source\sttutter-ui\node_modules\aws-sdk\lib'
Did you mean './util'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (S:/Source/sttutter-ui, node_modules).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'
        - install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "util": false }

package.json

{
  "name": "x",
  "version": "0.0.0",
  "license": "commercial",
  "private": true,
  "engines": {
    "node": ">= 10.0.0"
  },
  "dependencies": {
    "@angular/animations": "~12.0.1",
    "@angular/common": "~12.0.1",
    "@angular/compiler": "~12.0.1",
    "@angular/core": "~12.0.1",
    "@angular/forms": "~12.0.1",
    "@angular/platform-browser": "~12.0.1",
    "@angular/platform-browser-dynamic": "~12.0.1",
    "@angular/router": "~12.0.1",
    "@ng-bootstrap/ng-bootstrap": "^9.1.1",
    "@ngrx/store": "^12.0.0",
    "aws-sdk": "^2.910.0",
    "ngrx-store-freeze": "^0.2.4",
    "ngrx-store-logger": "^0.2.4",
    "prando": "^6.0.1",
    "rxjs": "~6.6.0",
    "tslib": "^2.1.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~12.0.1",
    "@angular/cli": "~12.0.1",
    "@angular/compiler-cli": "~12.0.1",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "jasmine-core": "~3.7.0",
    "karma": "~6.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "typescript": "~4.2.3"
  },
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  }
}

我尝试删除node_modules 目录并运行npm install

在网上搜索有关此错误的信息似乎表明这是webpack 问题...根据这些对话,我不确定如何使其再次正常工作。

即使aws-sdk 一直工作到今天,我是否应该切换到aws-sdk-js

寻求有关如何解决此问题的建议

谢谢!

【问题讨论】:

  • 派对迟到了。对于面临同样问题的任何人,我在this post 中找到了@p4309027 的解决方案。

标签: angular aws-sdk


【解决方案1】:

我也遇到了这个问题,不确定官方路径应该是什么,但以下对我有用。

  1. npm install -S util
  2. (window as any).global.util = (window as any).global.util || require("util").util; 添加到您的polyfills.ts 文件中。

【讨论】:

  • 仅安装 util 为我解决了编译错误,但添加此 polyfill 会记录控制台错误:util.js:109 Uncaught ReferenceError: process is not defined
【解决方案2】:

Angular 12 移至 Webpack 5,它不再包含用于 node.js 的 polyfill。

为避免自己添加缺失的 polyfill,请使用此 Webpack 插件:https://www.npmjs.com/package/node-polyfill-webpack-plugin

它包括所有已被 Webpack 5 删除的 polyfill。

按照描述安装包并配置Webpack插件,问题应该得到解决。

【讨论】:

    【解决方案3】:

    问题与您的 polyfills.ts 中 zone.js 的导入有关

    以前的导入是:

    import 'zone.js/dist/zone';

    改为:

    import 'zone.js';

    你可能还需要在你的 polyfills.ts 底部添加

    (window as any).global = window;

    【讨论】:

    • 试过了,但我的机器上仍然存在问题。
    • 虚假信息,具有误导性。
    【解决方案4】:

    试试这个:

    rm node_modules 
    rm package-lock.json 
    npm install
    

    【讨论】:

      猜你喜欢
      • 2020-04-10
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 2019-10-16
      • 2021-01-30
      • 1970-01-01
      • 2022-11-24
      相关资源
      最近更新 更多