How to us JS require() in Wappler NodeJS project

Hey All,

I’m trying to get some authorization set up to use the Google BigQuery API for my app. It needs oAuth2 or a Service Account authorization and it seems the easiest way to go is to use their Google Auth Library for NodeJS.

I’ve installed it via NPM, and just trying to run the basic code to access my JSON Web Token.

My question is just around using JS files that have the ‘require’ syntax, not sure how to use in wappler to require the google-auth-library installed in the node-modules folder.

Here’s a snippet i’m trying as per the google docs:

const { JWT } = require('google-auth-library');
const keys = require('./auth.json');

I’ve also tried:

const { JWT } = require('../node_modules/google-auth-library');
const keys = require('./auth.json');

But I’m getting this error message in the console:

auth.js:1 Uncaught ReferenceError: require is not defined at auth.js:1

Any thoughts or general direction on this? is ‘require’ ES6 syntax that isn’t supported? Is it just a matter of getting the right address in the require statement?

1 Like

For wnyone that reads this, it seems this is a common JS issue… or more a lack of understanding on my part.

the require() syntax that is usually used to reuire Node modules, doesn’t work in browsers/client-side and needs to be packaged by something like Babel, Browserify or WebPack to be used properly.

Let’s see if i can that to work.

Trying to do the same with parcel.js

In browsers you usually use a script that include tag to add a module.

In the latest browsers there is also an import statement for es6 compatible modules.

THanks @George. I did manage to download Browserify and get that to bundle a JS file to include the ‘required’ files.

Turns out that wasn’t really the solution to my problem. I’m trying to get Google authorization set up when i need to use a Service Account (which is outside of the current Wappler capabilities I believe)

Not sure what you want to do but if you use server connect oauth actions you can use there the google service accounts to get all kind of authorizations

Well if it could that would be amazing…I read an old post that said that service account authorisation wasn’t supported.

Hmm maybe I spoke too soon, now I recall that some JWT keys got involved.

But that is something we can add support for, we just weren’t aware people want to use service accounts that much - so it was a low priority.

Maybe @patrick can pick it up.

There was a big thread from @psweb a few months ago but not sure if it was solved.

The service accounts seem like they would be used in a B2B/SaaS setting as its the only way I can see that allows the app to use the Google APIs without the user involved, including CRON jobs.

I’d love it if it could be incorporated, as manually doing it is proving difficult :slightly_smiling_face:

JWT signing is implemented in Wappler 3.4.2

1 Like

This topic was automatically closed after 28 hours. New replies are no longer allowed.