Integrating Webpack with Auth0 or how to add additional Node.js modules to IBM Cloud Functions

227 views Asked by At

I'm trying to set up my nodejs backend functions to use Auth0 for IAM services.

I can successfully deploy functions to my IBM Cloud account that don't uses auth0 library, but if I try to upload a function that uses auth0 library I receive the following error:

error: Unable to create action 'function_name': The request content was malformed: Unexpected end-of-input at input index 1394034 (line 1, position 1394035), expected '"':

I've tried to use externals in my webpack.config.js file:

// webpack.config.js
externals: {
    auth0: "commonjs auth0"
}

// mycode.js
const ManagementClient = require('auth0').ManagementClient;

and I can successfully deploy the function but when I call the endpoint I receive this error:

(0 , r(...).ManagementClient) is not a constructor

1

There are 1 answers

0
habercde On BEST ANSWER

I found this article: Adding extra npm modules to IBM Cloud Functions with Docker

Basically if I understand your question right, you need an additional package that is not included in the IBM Cloud Functions base image. The packages that are included by default are listed here. But auth0 is not part of the list.

So following the blog post, you can create a Dockerfile.

FROM ibmfunctions/action-nodejs-v10 
RUN npm install auth0

Build the image (assuming you have a docker build environment and a working Docker account) and push it to Dockerhub:

docker build -t your_docker_username/action-nodejs-v10-auth0 .
docker tag your_docker_username/action-nodejs-v10-auth0:latest your_docker_username/action-nodejs-v10-auth0:0.0.1
docker push your_docker_username/action-nodejs-v10-auth0:0.0.1

Now you can create a source.js file containing

var ManagementClient = require('auth0').ManagementClient;

and the rest of your code.

ibmcloud fn action update myAuth0Function --docker your_docker_username/action-nodejs-v10-auth0:0.0.1 source.js

I hope this works for you. I have corrected some typos in this post and tested it. It worked so far that I was able to use the auth0 module in the action code.