google-api-nodejs-client
google-api-nodejs-client copied to clipboard
Googleapis return "Module not found: can't resolve fs" when running a project
Thanks for stopping by to let us know something could be better!
PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.
-
Is this a client library issue or a product issue? This is the client library for . We will only be able to assist with issues that pertain to the behaviors of this library. If the issue you're experiencing is due to the behavior of the product itself, please visit the Support page to reach the most relevant engineers.
-
Did someone already solve this?
- Search the issues already opened: https://github.com/googleapis/google-api-nodejs-client/issues
- Search the issues on our "catch-all" repository: https://github.com/googleapis/google-cloud-node
- Search or ask on StackOverflow (engineers monitor these tags): http://stackoverflow.com/questions/tagged/google-cloud-platform+node.js
- Do you have a support contract? Please create an issue in the support console to ensure a timely response.
If the support paths suggested above still do not result in a resolution, please provide the following details.
Environment details
- OS:
- Node.js version: 19.5.0
- npm version: 9.4.0
googleapisversion: 112.0.0
Steps to reproduce
- install googleapis
- run project and I see the following error:
error - ./node_modules/gcp-metadata/build/src/gcp-residency.js:19:0
Module not found: Can't resolve 'fs'
Import trace for requested module:
./node_modules/gcp-metadata/build/src/index.js
./node_modules/google-auth-library/build/src/auth/googleauth.js
./node_modules/google-auth-library/build/src/index.js
./node_modules/googleapis/build/src/index.js
Making sure to follow these steps will guarantee the quickest resolution possible.
Thanks!
Hi @Geccles, could you provide the code you are attempting to run? Thanks in advance.
@sofisl
This is my function and then I just call it in another file. Let me know if there's anything else you may need. The credentials are in a json like so:
{
"type": "service_account",
"project_id": "our_project_id",
"private_key_id": "our_private_key",
"private_key": "-----BEGIN PRIVATE KEY-----"
"client_email": "our_client_email",
"client_id": "our_client_id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "our_cert_url"
}
export function getGoogleDocFiles() {
const { google } = require('googleapis')
const credentials = require('../../credentials.json')
const scopes = [
'https://www.googleapis.com/auth/drive'
]
const auth = new google.auth.JWT(
credentials.client_email, null,
credentials.private_key, scopes
)
console.log(auth)
const drive = google.drive({ version: "v3", auth })
console.log(drive)
drive.files.list({}, (err, res) => {
if (err) throw err
const files = res.data.files
if (files.length) {
files.map((file) => {
console.log(file)
})
} else {
console.log('No files found')
}
})
}
any updates on this happening to me too
The error you're seeing is common when trying to use certain Node.js modules, such as fs (file system), on the client side of a JavaScript application. This is because these modules are designed to be used in the Node.js environment on the server, not in the browser.
In your case, it appears that you're trying to use the googleapis module on the client side of your application. This module relies on other Node.js modules, such as fs, which are not available in the browser.
To fix this issue, you should move any code that uses googleapis to the server side of your application.
For me i removed "use client" where i call it on page.tsx and it works. Because my issue, what to call signout() function on page.tsx of nextAuth with google and when i remove there was not issue all works fine