Module not found: Error: Can't resolve 'http' in FaunaDB JavaScript Driver

Module not found: Error: Can't resolve 'http' in FaunaDB JavaScript Driver

我正在尝试在 Ionic 项目中使用 FaunaDB,但出现了一个我不知道如何解决的问题。

我简单的新建了一个工程,安装了驱动:

ionic start --type=angular my-app blank
npm i faunadb

然后,我实例化FaunaDB.Cliente:

import * as FaunaDB from 'faunadb';
const client = new FaunaDB.Client({
  secret: environment.faunadb.secret,
  keepAlive: false,
});

在开发中效果很好!但是当我为生产构建时,使用这个命令:

ionic build --prod

并且出现以下错误:

ERROR in ../node_modules/faunadb/src/Client.js
Module not found: Error: Can't resolve 'http' in 'D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src'
resolve 'http' in 'D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src'
  Parsed request is a module
  using description file: D:\Workspace\www\fanstore\workspace\node_modules\faunadb\package.json (relative path: ./src)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      looking for modules in D:/Workspace/www/fanstore/workspace/my-app/
        using description file: D:\Workspace\www\fanstore\workspace\my-app\package.json (relative path: .)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: D:\Workspace\www\fanstore\workspace\my-app\package.json (relative path: ./http)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\http doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\http.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\http.tsx doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\http.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\http.js doesn't exist
            as directory
              D:\Workspace\www\fanstore\workspace\my-app\http doesn't exist
      D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src\node_modules doesn't exist or is not a directory
      D:\Workspace\www\fanstore\workspace\node_modules\faunadb\node_modules doesn't exist or is not a directory
      D:\Workspace\www\fanstore\workspace\node_modules\node_modules doesn't exist or is not a directory
      D:\Workspace\www\fanstore\node_modules doesn't exist or is not a directory
      D:\Workspace\www\node_modules doesn't exist or is not a directory
      D:\Workspace\node_modules doesn't exist or is not a directory
      D:\node_modules doesn't exist or is not a directory
      looking for modules in D:\Workspace\www\fanstore\workspace\node_modules
        No description file found
        Field 'browser' doesn't contain a valid alias configuration
        No description file found
        no extension
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\http doesn't exist
        .ts
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\http.ts doesn't exist
        .tsx
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\http.tsx doesn't exist
        .mjs
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\http.mjs doesn't exist
        .js
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\http.js doesn't exist
        as directory
          D:\Workspace\www\fanstore\workspace\node_modules\http doesn't exist
[D:\Workspace\www\fanstore\workspace\my-app\http]
[D:\Workspace\www\fanstore\workspace\my-app\http.ts]
[D:\Workspace\www\fanstore\workspace\my-app\http.tsx]
[D:\Workspace\www\fanstore\workspace\my-app\http.mjs]
[D:\Workspace\www\fanstore\workspace\my-app\http.js]
[D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src\node_modules]
[D:\Workspace\www\fanstore\workspace\node_modules\faunadb\node_modules]
[D:\Workspace\www\fanstore\workspace\node_modules\node_modules]
[D:\Workspace\www\fanstore\node_modules]
[D:\Workspace\www\node_modules]
[D:\Workspace\node_modules]
[D:\node_modules]
[D:\Workspace\www\fanstore\workspace\node_modules\package.json]
[D:\Workspace\www\fanstore\workspace\node_modules\http\package.json]
[D:\Workspace\www\fanstore\workspace\node_modules\http]
[D:\Workspace\www\fanstore\workspace\node_modules\http.ts]
[D:\Workspace\www\fanstore\workspace\node_modules\http.tsx]
[D:\Workspace\www\fanstore\workspace\node_modules\http.mjs]
[D:\Workspace\www\fanstore\workspace\node_modules\http.js]
 @ ../node_modules/faunadb/src/Client.js 87:8-23
 @ ../node_modules/faunadb/index.js
 @ ./src/app/app.component.ts
 @ ./src/app/app.module.ngfactory.js
 @ ./src/main.ts
 @ multi ./src/main.ts
ERROR in ../node_modules/faunadb/src/Client.js
Module not found: Error: Can't resolve 'https' in 'D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src'
resolve 'https' in 'D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src'
  Parsed request is a module
  using description file: D:\Workspace\www\fanstore\workspace\node_modules\faunadb\package.json (relative path: ./src)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      looking for modules in D:/Workspace/www/fanstore/workspace/my-app/
        using description file: D:\Workspace\www\fanstore\workspace\my-app\package.json (relative path: .)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: D:\Workspace\www\fanstore\workspace\my-app\package.json (relative path: ./https)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\https doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\https.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\https.tsx doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\https.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\https.js doesn't exist
            as directory
              D:\Workspace\www\fanstore\workspace\my-app\https doesn't exist
      D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src\node_modules doesn't exist or is not a directory
      D:\Workspace\www\fanstore\workspace\node_modules\faunadb\node_modules doesn't exist or is not a directory
      D:\Workspace\www\fanstore\workspace\node_modules\node_modules doesn't exist or is not a directory
      D:\Workspace\www\fanstore\node_modules doesn't exist or is not a directory
      D:\Workspace\www\node_modules doesn't exist or is not a directory
      D:\Workspace\node_modules doesn't exist or is not a directory
      D:\node_modules doesn't exist or is not a directory
      looking for modules in D:\Workspace\www\fanstore\workspace\node_modules
        No description file found
        Field 'browser' doesn't contain a valid alias configuration
        No description file found
        no extension
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\https doesn't exist
        .ts
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\https.ts doesn't exist
        .tsx
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\https.tsx doesn't exist
        .mjs
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\https.mjs doesn't exist
        .js
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\https.js doesn't exist
        as directory
          D:\Workspace\www\fanstore\workspace\node_modules\https doesn't exist
[D:\Workspace\www\fanstore\workspace\my-app\https]
[D:\Workspace\www\fanstore\workspace\my-app\https.ts]
[D:\Workspace\www\fanstore\workspace\my-app\https.tsx]
[D:\Workspace\www\fanstore\workspace\my-app\https.mjs]
[D:\Workspace\www\fanstore\workspace\my-app\https.js]
[D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src\node_modules]
[D:\Workspace\www\fanstore\workspace\node_modules\faunadb\node_modules]
[D:\Workspace\www\fanstore\workspace\node_modules\node_modules]
[D:\Workspace\www\fanstore\node_modules]
[D:\Workspace\www\node_modules]
[D:\Workspace\node_modules]
[D:\node_modules]
[D:\Workspace\www\fanstore\workspace\node_modules\package.json]
[D:\Workspace\www\fanstore\workspace\node_modules\https\package.json]
[D:\Workspace\www\fanstore\workspace\node_modules\https]
[D:\Workspace\www\fanstore\workspace\node_modules\https.ts]
[D:\Workspace\www\fanstore\workspace\node_modules\https.tsx]
[D:\Workspace\www\fanstore\workspace\node_modules\https.mjs]
[D:\Workspace\www\fanstore\workspace\node_modules\https.js]
 @ ../node_modules/faunadb/src/Client.js 86:8-24
 @ ../node_modules/faunadb/index.js
 @ ./src/app/app.component.ts
 @ ./src/app/app.module.ngfactory.js
 @ ./src/main.ts
 @ multi ./src/main.ts
ERROR in ../node_modules/faunadb/src/errors.js
Module not found: Error: Can't resolve 'util' in 'D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src'
resolve 'util' in 'D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src'
  Parsed request is a module
  using description file: D:\Workspace\www\fanstore\workspace\node_modules\faunadb\package.json (relative path: ./src)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      looking for modules in D:/Workspace/www/fanstore/workspace/my-app/
        using description file: D:\Workspace\www\fanstore\workspace\my-app\package.json (relative path: .)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: D:\Workspace\www\fanstore\workspace\my-app\package.json (relative path: ./util)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\util doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\util.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\util.tsx doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\util.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\util.js doesn't exist
            as directory
              D:\Workspace\www\fanstore\workspace\my-app\util doesn't exist
      D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src\node_modules doesn't exist or is not a directory
      D:\Workspace\www\fanstore\workspace\node_modules\faunadb\node_modules doesn't exist or is not a directory
      D:\Workspace\www\fanstore\workspace\node_modules\node_modules doesn't exist or is not a directory
      D:\Workspace\www\fanstore\node_modules doesn't exist or is not a directory
      D:\Workspace\www\node_modules doesn't exist or is not a directory
      D:\Workspace\node_modules doesn't exist or is not a directory
      D:\node_modules doesn't exist or is not a directory
      looking for modules in D:\Workspace\www\fanstore\workspace\node_modules
        No description file found
        Field 'browser' doesn't contain a valid alias configuration
        No description file found
        no extension
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\util doesn't exist
        .ts
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\util.ts doesn't exist
        .tsx
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\util.tsx doesn't exist
        .mjs
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\util.mjs doesn't exist
        .js
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\util.js doesn't exist
        as directory
          D:\Workspace\www\fanstore\workspace\node_modules\util doesn't exist
[D:\Workspace\www\fanstore\workspace\my-app\util]
[D:\Workspace\www\fanstore\workspace\my-app\util.ts]
[D:\Workspace\www\fanstore\workspace\my-app\util.tsx]
[D:\Workspace\www\fanstore\workspace\my-app\util.mjs]
[D:\Workspace\www\fanstore\workspace\my-app\util.js]
[D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src\node_modules]
[D:\Workspace\www\fanstore\workspace\node_modules\faunadb\node_modules]
[D:\Workspace\www\fanstore\workspace\node_modules\node_modules]
[D:\Workspace\www\fanstore\node_modules]
[D:\Workspace\www\node_modules]
[D:\Workspace\node_modules]
[D:\node_modules]
[D:\Workspace\www\fanstore\workspace\node_modules\package.json]
[D:\Workspace\www\fanstore\workspace\node_modules\util\package.json]
[D:\Workspace\www\fanstore\workspace\node_modules\util]
[D:\Workspace\www\fanstore\workspace\node_modules\util.ts]
[D:\Workspace\www\fanstore\workspace\node_modules\util.tsx]
[D:\Workspace\www\fanstore\workspace\node_modules\util.mjs]
[D:\Workspace\www\fanstore\workspace\node_modules\util.js]
 @ ../node_modules/faunadb/src/errors.js 3:11-26
 @ ../node_modules/faunadb/index.js
 @ ./src/app/app.component.ts
 @ ./src/app/app.module.ngfactory.js
 @ ./src/main.ts
 @ multi ./src/main.ts
ERROR in ../node_modules/faunadb/src/values.js
Module not found: Error: Can't resolve 'util' in 'D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src'
resolve 'util' in 'D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src'
  Parsed request is a module
  using description file: D:\Workspace\www\fanstore\workspace\node_modules\faunadb\package.json (relative path: ./src)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      looking for modules in D:/Workspace/www/fanstore/workspace/my-app/
        using description file: D:\Workspace\www\fanstore\workspace\my-app\package.json (relative path: .)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: D:\Workspace\www\fanstore\workspace\my-app\package.json (relative path: ./util)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\util doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\util.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\util.tsx doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\util.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              D:\Workspace\www\fanstore\workspace\my-app\util.js doesn't exist
            as directory
              D:\Workspace\www\fanstore\workspace\my-app\util doesn't exist
      D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src\node_modules doesn't exist or is not a directory
      D:\Workspace\www\fanstore\workspace\node_modules\faunadb\node_modules doesn't exist or is not a directory
      D:\Workspace\www\fanstore\workspace\node_modules\node_modules doesn't exist or is not a directory
      D:\Workspace\www\fanstore\node_modules doesn't exist or is not a directory
      D:\Workspace\www\node_modules doesn't exist or is not a directory
      D:\Workspace\node_modules doesn't exist or is not a directory
      D:\node_modules doesn't exist or is not a directory
      looking for modules in D:\Workspace\www\fanstore\workspace\node_modules
        No description file found
        Field 'browser' doesn't contain a valid alias configuration
        No description file found
        no extension
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\util doesn't exist
        .ts
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\util.ts doesn't exist
        .tsx
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\util.tsx doesn't exist
        .mjs
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\util.mjs doesn't exist
        .js
          Field 'browser' doesn't contain a valid alias configuration
          D:\Workspace\www\fanstore\workspace\node_modules\util.js doesn't exist
        as directory
          D:\Workspace\www\fanstore\workspace\node_modules\util doesn't exist
[D:\Workspace\www\fanstore\workspace\my-app\util]
[D:\Workspace\www\fanstore\workspace\my-app\util.ts]
[D:\Workspace\www\fanstore\workspace\my-app\util.tsx]
[D:\Workspace\www\fanstore\workspace\my-app\util.mjs]
[D:\Workspace\www\fanstore\workspace\my-app\util.js]
[D:\Workspace\www\fanstore\workspace\node_modules\faunadb\src\node_modules]
[D:\Workspace\www\fanstore\workspace\node_modules\faunadb\node_modules]
[D:\Workspace\www\fanstore\workspace\node_modules\node_modules]
[D:\Workspace\www\fanstore\node_modules]
[D:\Workspace\www\node_modules]
[D:\Workspace\node_modules]
[D:\node_modules]
[D:\Workspace\www\fanstore\workspace\node_modules\package.json]
[D:\Workspace\www\fanstore\workspace\node_modules\util\package.json]
[D:\Workspace\www\fanstore\workspace\node_modules\util]
[D:\Workspace\www\fanstore\workspace\node_modules\util.ts]
[D:\Workspace\www\fanstore\workspace\node_modules\util.tsx]
[D:\Workspace\www\fanstore\workspace\node_modules\util.mjs]
[D:\Workspace\www\fanstore\workspace\node_modules\util.js]
 @ ../node_modules/faunadb/src/values.js 7:11-26
 @ ../node_modules/faunadb/index.js
 @ ./src/app/app.component.ts
 @ ./src/app/app.module.ngfactory.js
 @ ./src/main.ts
 @ multi ./src/main.ts
[ERROR] An error occurred while running subprocess ng.

        ng.cmd run app:build:production exited with exit code 1.

        Re-running this command with the --verbose flag may provide more information.

我对 ionic 一无所知,但看起来您要从客户端而不是服务器端使用动物群? 在文档中,他们建议直接通过 CDN 包含客户端版本 https://docs.fauna.com/fauna/current/drivers/javascript#browsers 也许它只是无法通过您的捆绑器按预期工作。 我看到它抱怨 "Field 'browser' doesn't contain a valid alias configuration" 这意味着 package.json 没有按照捆绑器的预期进行配置。 实际上根本没有这样的字段: https://github.com/fauna/faunadb-js/blob/master/package.json 因此,如果 ionic 允许,您可以从 CDN 下载浏览器构建,将其放入 ionic 项目中的静态文件夹(我相信这应该是可能的),然后将此脚本直接包含在您的主 HTML 页面中或找到其他方法ionic 将如何让您做到这一点。

P.S。也许在动物群客户端回购中提出有关 package.json 未正确配置的问题是个好主意。