如何使用 webpack 捆绑器导入 firebase 分析而不出现错误?

问题描述 投票:0回答:1

我有一个导入 firebase 分析并使用 webpack 构建 js 的应用程序。
清除缓存并删除

node_modules
后出现错误(我遇到了另一个错误)。
所以,这样做之后,错误开始出现:

Module not found: Error: Can't resolve 'firebase/analytics' in 'src'
Did you mean 'index.esm.js'?
BREAKING CHANGE: The request 'firebase/analytics' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.
resolve 'firebase/analytics' in 'src'
  Parsed request is a module
  using description file: package.json (relative path: ./src)
    Field 'browser' doesn't contain a valid alias configuration
      looking for modules in node_modules
        existing directory node_modules\firebase
          using description file: node_modules\firebase\package.json (relative path: .)
            using description file: node_modules\firebase\analytics\package.json (relative path: ./dist)
              Field 'browser' doesn't contain a valid alias configuration
        ...

我尝试用

require
替换它,我尝试使用
firebase/analytics/dist
firebase/analytics/dist/index.esm.js
。 它也不起作用

我在

index.js
的进口是:

import initializeApp from "firebase/app";
import { getAnalytics, logEvent } from "firebase/analytics";
javascript webpack firebase-analytics
1个回答
0
投票

谷歌怎么能发布这样的包,真是可笑。

为了帮助 Webpack 解析路径,您必须转到

node_modules/firebase/analytics/package.json
并更改

"dist/index.cjs.js": "dist/esm/index.esm.js"

"dist/index.cjs.js": "./dist/esm/index.esm.js"

在我的例子中,我更新了

browser
下的值,如果您从事react-native项目,您应该更新
react-native
下的值。

© www.soinside.com 2019 - 2024. All rights reserved.