GatsbyJS with Firebase - WebpackError: ReferenceError: IDBIndex is not defined

Ola*_*laf 2 firebase webpack react-redux gatsby google-cloud-firestore

我收到gatsby develop 的错误。它与这个非常相似:https : //github.com/firebase/firebase-js-sdk/issues/2222,但我收到了gatsby develop错误,而不是 gatsby build 错误。我做了很多研究,但找不到可行的解决方案。起初我在gatsby build 上遇到了问题,就像在这篇文章中一样:https : //github.com/firebase/firebase-js-sdk/issues/2222,但我用自定义的 onCreateWebpackConfig 解决了它(你可以在下面找到它)。

堆栈: - Gatsby - Firebase(可能是错误) - Redux

我也删除了 .cache 和 node_modules 并重新安装了所有东西,但它没有用。

错误

There was an error compiling the html.js component for the development server.

See our docs page on debugging HTML builds for help https://gatsby.dev/debug-html ReferenceError: IDBIndex is not defined
]);
  86 | 
> 87 | proxyRequestMethods(Index, '_index', IDBIndex, [
     | ^
  88 |   'get',
  89 |   'getKey',
  90 |   'getAll',

  WebpackError: ReferenceError: IDBIndex is not defined

  - idb.mjs:87 Module../node_modules/idb/lib/idb.mjs
    node_modules/idb/lib/idb.mjs:87:1

  - index.esm.js:1 Module../node_modules/@firebase/installations/dist/index.esm.js
    node_modules/@firebase/installations/dist/index.esm.js:1:1

  - index.esm.js:1 Module../node_modules/@firebase/analytics/dist/index.esm.js
    node_modules/@firebase/analytics/dist/index.esm.js:1:1

  - index.esm.js:1 Module../node_modules/firebase/analytics/dist/index.esm.js
    node_modules/firebase/analytics/dist/index.esm.js:1:1

  - index.ts:1 Module../src/firebase/index.ts
    src/firebase/index.ts:1:1

  - index.esm.js:32 emit
    node_modules/@firebase/analytics/dist/index.esm.js:32:1

Run Code Online (Sandbox Code Playgroud)

我的 gatsby 节点文件

exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
  if (stage === 'build-html') {
    actions.setWebpackConfig({
      externals: getConfig().externals.concat(function(context, request, callback) {
        const regex = /^@?firebase(\/(.+))?/;
        if (regex.test(request)) {
          return callback(null, `umd ${request}`);
        }
        callback();
      }),
    });
  }
};
Run Code Online (Sandbox Code Playgroud)

我的 Firebase 依赖项:

 "@firebase/firestore-types": "^1.10.1",
 "firebase": "^7.13.1",
 "firebase-admin": "^8.10.0",
 "firebase-functions": "^3.5.0",
 "firebase-tools": "^7.16.1",
Run Code Online (Sandbox Code Playgroud)

Firebase 索引文件:

import firebase from 'firebase/app';
import 'firebase/firestore';
import 'firebase/auth';
import 'firebase/storage';
import 'firebase/analytics';
const firebaseConfig = {...};
firebase.initializeApp(firebaseConfig);
export const firestore = firebase.firestore();
export const auth = firebase.auth();
export const storage = firebase.storage();

Run Code Online (Sandbox Code Playgroud)

项目仓库https : //github.com/olafsulich/Projecty

在 Github 问题上发帖:https : //github.com/firebase/firebase-js-sdk/issues/2946

提前致谢。

Fer*_*reu 6

build由于您的条件 ( stage === 'build-html') ,以下代码段仅适用于环境:

   exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
      if (stage === 'build-html') {
        actions.setWebpackConfig({
          externals: getConfig().externals.concat(function(context, request, callback) {
            const regex = /^@?firebase(\/(.+))?/;
            if (regex.test(request)) {
              return callback(null, `umd ${request}`);
            }
            callback();
          }),
        });
      }
    };
Run Code Online (Sandbox Code Playgroud)

删除它并像这样使用它:

   exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
        actions.setWebpackConfig({
          externals: getConfig().externals.concat(function(context, request, callback) {
            const regex = /^@?firebase(\/(.+))?/;
            if (regex.test(request)) {
              return callback(null, `umd ${request}`);
            }
            callback();
          }),
        });
    };
Run Code Online (Sandbox Code Playgroud)

非常感谢!它仅适用于 gatbsy 开发,但是现在当我想构建项目时,出现错误 - TypeError:无法读取未定义的属性“concat”。你知道怎么解决吗?

关于新问题,您可以遵循本主题中的解决方法,这是 Gatsby 中第三方模块在尝试访问window应用程序构建时尚未定义的 DOM 元素(通常)时的常见错误。所以,你需要等到window定义。您可以通过两种方式实现这一点:

  1. 使用以下条件实例化您的 Firebase:

     import firebase from '@firebase/app';
     import '@firebase/auth';
     import '@firebase/firestore';
     import '@firebase/functions';
    
     const config = {
        ... firebase config here
     };
    
     let instance;
    
     export default function getFirebase() {
       if (typeof window !== 'undefined') {
         if (instance) return instance;
         instance = firebase.initializeApp(config);
         return instance;
       }
    
       return null;
     }
    
    Run Code Online (Sandbox Code Playgroud)

    注意 if (typeof window !== 'undefined')声明

  2. 通过忽略 webpack 配置中的 firebase 模块,例如显示他们的文档。在您的gatsby-node.js

     exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
       if (stage === "build-html") {
         actions.setWebpackConfig({
           module: {
             rules: [
               {
                 test: /bad-module/,
                 use: loaders.null(),
               },
             ],
           },
         })
       }
     }
    
    Run Code Online (Sandbox Code Playgroud)

    替换bad modulefirebase(或 中的包/文件夹名称node_modules)。保留斜线,因为这test是正则表达式规则

    此代码段替换了您之前似乎在concat()函数中引发错误的代码段。