当我尝试使用firebase云功能
import * as functions from 'firebase-functions';
exports.userDeleted = functions.auth.user().onDelete(event => {
console.log("user deleted ")
});
我得到这个错误:
[webpack-dev-server] ERROR in ./node_modules/verror/lib/verror.js 6:15-30
Module not found: Error: Can't resolve 'util' in 'C:UsersMyPCDocumentsAIC-partageworkspacemyprojectnode_modulesverrorlib'
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 }
有人知道如何修复这个错误吗?
从错误中可以看出,您正在使用Webpack 5来捆绑您的代码。快速简短回答直到Webpack 4, Webpack只会为本地node.js包含polyfillsfs
、util
、path
等模块。在Webpack 5中不再是这样了。您需要显式地告诉Webpack填充它或不提供它。点击这里阅读更多内容:Webpack Resolve Fallback。
module.exports = {
//... Webpack configuration
resolve: {
fallback: {
assert: require.resolve('assert'),
buffer: require.resolve('buffer'),
console: require.resolve('console-browserify'),
constants: require.resolve('constants-browserify'),
crypto: require.resolve('crypto-browserify'),
domain: require.resolve('domain-browser'),
events: require.resolve('events'),
http: require.resolve('stream-http'),
https: require.resolve('https-browserify'),
os: require.resolve('os-browserify/browser'),
path: require.resolve('path-browserify'),
punycode: require.resolve('punycode'),
process: require.resolve('process/browser'),
querystring: require.resolve('querystring-es3'),
stream: require.resolve('stream-browserify'),
string_decoder: require.resolve('string_decoder'),
sys: require.resolve('util'),
timers: require.resolve('timers-browserify'),
tty: require.resolve('tty-browserify'),
url: require.resolve('url'),
util: require.resolve('util'),
vm: require.resolve('vm-browserify'),
zlib: require.resolve('browserify-zlib'),
},
},
};
长回答
如果我们从字面上看错误,那么这意味着上面的答案是正确的。但这可能解决不了你的问题。看起来Webpack正在尝试为浏览器运行时捆绑代码。因此,上面建议的答案可能有效,但实际上,您正在捆绑代码以在Firebase Cloud Functions上运行。. 你必须配置Webpack来为Node.js环境绑定你的代码,并指定它忽略你的bundle中的本地Node.js模块。您需要使用target: 'node'
和(可选)webpack-node-externals
包忽略任何第三方模块。您的配置应该是:
const nodeExternals = require('webpack-node-externals');
module.exports = {
// ... Webpack configuration
// in order to ignore built-in modules like path, fs, etc.
target: 'node',
// [OPTIONAL] - Depending on your use case
// In order to ignore all modules in node_modules folder.
externals: [nodeExternals()],
};
这只是函数的语法
exports.userDidDeleted = functions.auth.user().onDelete((event) => {
const userSearchLocationModelPath = "/users/" + event.uid;
admin.database().ref(userSearchLocationModelPath).remove();
});