[英]Module not found: Can't resolve 'fs' on @google-cloud/storage
Getting the Module not found: Can't resolve 'fs'
error when trying to list out buckets from GCP Storage.获取
Module not found: Can't resolve 'fs'
错误。
import { Storage } from '@google-cloud/storage';
const googleCloud = new Storage({
keyFilename: '../../my-project-c1a44bf80be3.json',
projectId: 'my-project',
});
googleCloud.getBuckets().then((x: any) => console.log(x));
my-project-c1a44bf80be3.json
(Download from GCP) exists and is the project level my-project-c1a44bf80be3.json
(从GCP下载)存在并且是项目级别
Error:错误:
event - compiled successfully
event - build page: /
wait - compiling...
error - ./node_modules/@google-cloud/storage/build/src/file.js:24:0
Module not found: Can't resolve 'fs'
null
Could not find files for / in .next/build-manifest.json
event - compiled successfully
The same error appears when using googleapis .使用googleapis时出现相同的错误。 However, instead of
./node_modules/@google-cloud/storage/build/src/file.js:24:0
it's in the google-auth-library
module that comes with the googleapis
.但是,它不是
./node_modules/@google-cloud/storage/build/src/file.js:24:0
它位于googleapis
附带的google-auth-library
模块中。
Added using yarn
.使用
yarn
添加。
This usually happens when you import or reference firebase-admin in the client side, check your code for wrong imports where you might be calling这通常发生在您在客户端导入或引用 firebase-admin 时,请检查您的代码是否存在您可能调用的错误导入
import * as firebase from "firebase-admin";
instead of代替
import firebase from "firebase";
Posting the solution provided in the comments for visibility.发布评论中提供的解决方案以获得可见性。
In your webpack configuration, you can indicate that certain modules like fs should be stubbed out;
在你的 webpack 配置中,你可以指明某些模块如 fs 应该被剔除; at which point you should be able to run this library client-side.
此时您应该能够在客户端运行这个库。 For example:
例如:
node: {
// Some libraries import Node modules but don't use them in the browser.
// Tell Webpack to provide empty mocks for them so importing them works.
...config.node,
fs: 'empty',
child_process : 'empty',
net : 'empty',
tls: 'empty',
}
I faced the same problem but this guide was the solution in my case.我遇到了同样的问题,但本指南是我的解决方案。 As mentioned before, the problem is that the
firebase-admin
package is designed to run in a NodeJs environment and not on the client.如前所述,问题在于
firebase-admin
包被设计为在 NodeJs 环境中运行,而不是在客户端上运行。 Extending webpack.config.js
or next.config.js
with certain entries did not solve the problem for me.使用某些条目扩展
webpack.config.js
或next.config.js
并没有解决我的问题。
The solution is to move the firebase-admin
calls to the NextJS API which you can create within the /pages/api
folder.解决方案是将 firebase
firebase-admin
调用移动到您可以在/pages/api
文件夹中创建的NextJS API 。
Here is an example to fetch a single users data from the client.这是一个从客户端获取单个用户数据的示例。
You need:你需要:
/src/lib/firebase.ts
: firebase initialization /src/lib/firebase.ts
: firebase 初始化/src/pages/api/user.ts
: NextJS API handler /src/pages/api/user.ts
:NextJS API 处理程序/src/components/xyz.tsx
: component on client, which calls the API /src/components/xyz.tsx
: 客户端组件,调用API// /src/lib/firebase.ts
import admin from "firebase-admin";
import { cert } from "firebase-admin/app";
import certConfig from "./firebase-adminsdk-config.json"
admin.initializeApp({
credential: cert(certConfig)
});
console.log("Firebase initialized.");
export default admin;
// /src/pages/api/user.ts
import admin from 'lib/firebase'
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const firebase = admin.firestore()
const collection = firebase.collection('users')
// get any data from requests body
const { user } = req.body
// perform your firebase collection call
const data = await collection.get(user.id)
// return data to client
res.status(200)
res.json({
success: true,
result: data
});
return res
}
On the client side then you just have to perform a GET to you own API在客户端,您只需对自己的 API 执行 GET
// /src/components/xyz.tsx
...
const onLogin = async (user) => {
const response = await fetch(`/api/users`, {
method: "GET",
body: JSON.stringify({ user }),
});
// data => { success: boolean, result: any }
const data = await response.json();
return data;
}
...
Getting the Module not found: Can't resolve 'fs'
error when trying to list out buckets from GCP Storage. Module not found: Can't resolve 'fs'
尝试从 GCP 存储中列出存储桶时, Module not found: Can't resolve 'fs'
错误。
import { Storage } from '@google-cloud/storage';
const googleCloud = new Storage({
keyFilename: '../../my-project-c1a44bf80be3.json',
projectId: 'my-project',
});
googleCloud.getBuckets().then((x: any) => console.log(x));
my-project-c1a44bf80be3.json
(Download from GCP) exists and is the project level my-project-c1a44bf80be3.json
(从 GCP 下载)存在并且是项目级别
Error:错误:
event - compiled successfully
event - build page: /
wait - compiling...
error - ./node_modules/@google-cloud/storage/build/src/file.js:24:0
Module not found: Can't resolve 'fs'
null
Could not find files for / in .next/build-manifest.json
event - compiled successfully
The same error appears when using googleapis
( https://www.npmjs.com/package/googleapis ).使用
googleapis
( https://www.npmjs.com/package/googleapis ) 时会出现相同的错误。 However, instead of ./node_modules/@google-cloud/storage/build/src/file.js:24:0
it's in the google-auth-library
module that comes with the googleapis
.但是,它位于
googleapis
附带的google-auth-library
模块中,而不是./node_modules/@google-cloud/storage/build/src/file.js:24:0
。
Added using yarn
使用
yarn
添加
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.