The library is not compatible with webpack
It's not meant to be, as it's meant to be used on the server only. It doesn't need to be passed through any Webpack build.
@jmdobry I believe Webpack supports some Node packing - however, I agree with your statement.
@ddimitrioglo As @jmdobry mentioned, this (and Node.js) is designed for use on the server (or backend), whereas webpack is designed for use with client-side (or frontend) code.
What exactly are you trying to do?
In our project we are working with AWS.Lambda and we used to zip all the node_modules together with the handler.js (lambda code).
To reduce lamda.zip size we pass all the dependencies through the webpack and that is why we faced the issue with googleapis incompatibility.
P.S. As you can see it's still backend execution.
Can you provide more details beyond "The library is not compatible with webpack"? Exact reproduction steps will allow us to help you, thanks.
FWIW - it's not that uncommon to use webpack with Node.js for electron applications. I actually do this in github.com/JustinBeckwith/trebuchet.
Lambda code (in general):
import google from 'googleapis';
let OAuth2 = google.auth.OAuth2;
let sheets = google.sheets('v4');
export function handler(event, context) {
get auth() {
// returns OAuth2 client;
}
getRows(spreadsheetId, worksheetName) {
return new Promise((resolve, reject) => {
sheets.spreadsheets.values.get(
{auth: this.auth, spreadsheetId: spreadsheetId, range: `${worksheetName}!A:Z`},
(err, response) => {
if (err) {
return reject(`Spreadsheet read error: ${err}`);
}
resolve(response.values);
});
});
}
}
and when I'm trying to "pack" it with webpack it throws me an error the request of a dependency is an expression
Is it an error or a warning?
An error, because webpack can not resolve something like this:
require(someVar == true ? 'getSomething' : 'getOther')
There are options for this. Webpack will simply include all possible values of the expression.
http://webpack.github.io/docs/configuration.html#automatically-created-contexts-defaults-module-xxxcontextxxx
Anyone find a solution for this?
@yoyeung I needed spreadsheet API and I just used another library npm install google-spreadsheet
@ddimitrioglo Good idea...But in my case, i need to use GSuite api....lol
I have same trouble like @yoyeung,
Use externals to avoid to pack.
// webpack.config.js
externals: {
googleapis: "commonjs googleapis"
}
// mycode.js
const google = require("googleapis");
See why "commonjs googleapis" works:
https://webpack.js.org/configuration/externals/#function
(I honestly hope google-api-nodejs-client working under webpack!)
We bumped into the same issue. I think this should be fixed.
@jmdobry @ofrobots interested in your thoughts here. This could be fixed by generating an apis.ts
at generation time, and having that explicitly require each generated API. This would get rid of the weird fs.readdirsync
that happens on module load by making it a compile time operation.
Only negative is that you lose some flexibility in terms of running with a dynamic set of generated APIs in the apis/
folder. For example, today we support generating a single API at a time. Are there places where generating the require list at build time falls down?
I confirm that it's a pain to package a lambda with google apis for node.
My process was this:
Finally, i'll enumerate all package and sub-packages files but I feel like a cavemen.
EDIT: in the end I will be able to generate the list of prod packages with npm ls --prod --parseable
. Still not the best dev experience though.
Sucks this is still an issue 1.5 years after I first encountered it. Here is how I resolved for lambda use...
I have a top level dir in my project named lambda_node_modules. Install googleapis there.
For local dev, a simple symlink points node_modules/googleapis to lambda_node_modules/node_modules/googleapis
In Webpack Config (like I said 18 mths ago so this is Webpack 1):
plugins: [
// Moves files
new TransferWebpackPlugin([
{from: './lambda_node_modules/node_modules/', to: './dist/node_modules/'},
], __dirname),
],
externals: {
'aws-sdk': 'commonjs aws-sdk',
'googleapis': 'commonjs googleapis',
},
That's all it took for me. Webpack now thinks googleapis is externally available so it won't try to pack it, though Lambda needs it so I ship it to lambda in the deployment zip.
Guys, this is still an issue.
I'm using this library to implement OAuth 2 and Gmail API in Electron application, and I can't use it from a renderer process.
Versions:
Googleapis: 33.0.0
Webpack: 4.17.1
Electron: 3.0.0-beta.6
Backtrace:
ERROR in ./node_modules/mime/index.js
Module not found: Error: Can't resolve './types/other' in '/node_modules/mime'
@ ./node_modules/mime/index.js 4:55-79
@ ./node_modules/gtoken/build/src/index.js
@ ./node_modules/google-auth-library/build/src/auth/jwtclient.js
@ ./node_modules/google-auth-library/build/src/index.js
@ ./node_modules/googleapis/build/src/googleapis.js
@ ./node_modules/googleapis/build/src/index.js
@ ./src/containers/debug/debug.js
@ ./src/containers/debug/index.js
@ ./src/containers/index.js
@ ./src/index.js
@ multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./src/index.js
ERROR in ./node_modules/mime/index.js
Module not found: Error: Can't resolve './types/standard' in '/node_modules/mime'
@ ./node_modules/mime/index.js 4:26-53
@ ./node_modules/gtoken/build/src/index.js
@ ./node_modules/google-auth-library/build/src/auth/jwtclient.js
@ ./node_modules/google-auth-library/build/src/index.js
@ ./node_modules/googleapis/build/src/googleapis.js
@ ./node_modules/googleapis/build/src/index.js
@ ./src/containers/debug/debug.js
@ ./src/containers/debug/index.js
@ ./src/containers/index.js
@ ./src/index.js
@ multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./src/index.js
Oh hai! We're tracking this in #1286. We are super close :)
This is still broken.
By the way, is this is meant to be used server side, why is it labeled as client side? I just want to cloud-save some app data in a users G-Drive :/
For the folks who track this issue - can you give it a try now? We don't publish the webpacked minified files anywhere yet but you can webpack individual APIs and it should just work. Let us know if it does not (in a separate issue please :) )
Hi, I'm still having issues:
ERROR in ./node_modules/@google-cloud/storage/node_modules/mime/index.js
Module not found: Error: Can't resolve './types/other' in '/home/xxx/xxx/gcp-typescript-kata/node_modules/@google-cloud/storage/node_modules/mime'
ERROR in ./node_modules/gtoken/node_modules/mime/index.js
Module not found: Error: Can't resolve './types/other' in '/home/xxx/xxx/gcp-typescript-kata/node_modules/gtoken/node_modules/mime'
ERROR in ./node_modules/@google-cloud/storage/node_modules/mime/index.js
Module not found: Error: Can't resolve './types/standard' in '/home/xxx/xxx/gcp-typescript-kata/node_modules/@google-cloud/storage/node_modules/mime'
ERROR in ./node_modules/gtoken/node_modules/mime/index.js
Module not found: Error: Can't resolve './types/standard' in '/home/xxx/xxx/gcp-typescript-kata/node_modules/gtoken/node_modules/mime'
"@google-cloud/storage": "2.4.3",
[鈥
"webpack": "4.29.6",
"webpack-cli": "3.3.0",
"webpack-node-externals": "1.7.2"
I found my error. I had to include .json in the resolve
block of my webpack.config.js
. E.g.
module.exports = {
[鈥
resolve: {
extensions: ['.tsx', '.ts', '.js', '.json'],
},
[鈥
};
Most helpful comment
In our project we are working with AWS.Lambda and we used to zip all the node_modules together with the handler.js (lambda code).
To reduce lamda.zip size we pass all the dependencies through the webpack and that is why we faced the issue with googleapis incompatibility.
P.S. As you can see it's still backend execution.