我有一个导入 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";
谷歌怎么能发布这样的包,真是可笑。
为了帮助 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
下的值。