auth0/node-auth0

Getting TypeError upon initilizing ManagementClient

neeraj87 opened this issue · 13 comments

I am trying to initialize Auth0 ManagementClient in my Lambda function (Node.js + Typescript)

import { ManagementClient } from "auth0";

private getAuth0Client() {
    const auth0Domain = process.env.AUTH0_DOMAIN;
    const auth0ClientId = process.env.AUTH0_CLIENT_ID;
    const auth0ClientSecret = process.env.AUTH0_CLIENT_SECRET;
    
    if(auth0Domain && auth0ClientId && auth0ClientSecret) {
        return new ManagementClient({
            domain: auth0Domain,
            clientId: auth0ClientId,
            clientSecret: auth0ClientSecret,
            scope: "read:users update:users",
        });
    } else {
        throw new Error("Could not initialize Auth0 Management Client");
    }
}

public async getUser(userId: string): Promise<any> {
    try {
        const auth0Client = this.getAuth0Client();
        return await auth0Client.getUser({
            id: userId
        });
    } catch (error) {
        console.log(error);
    }
}

and it gives me the following error

{
    "errorType": "TypeError",
    "errorMessage": "E is not a function",
    "stack": [
        "TypeError: E is not a function",
        "    at Object.91738 (/var/task/src/sns-lambda.js:94:5226)",
        "    at ds (/var/task/src/sns-lambda.js:2333:1904531)",
        "    at Object.59458 (/var/task/src/sns-lambda.js:94:15606)",
        "    at ds (/var/task/src/sns-lambda.js:2333:1904531)",
        "    at Object.16135 (/var/task/src/sns-lambda.js:2220:1079)",
        "    at ds (/var/task/src/sns-lambda.js:2333:1904531)",
        "    at Object.81557 (/var/task/src/sns-lambda.js:2173:10664)",
        "    at ds (/var/task/src/sns-lambda.js:2333:1904531)",
        "    at Object.96781 (/var/task/src/sns-lambda.js:2173:15330)",
        "    at ds (/var/task/src/sns-lambda.js:2333:1904531)"
    ]
}

I am using the following versions for auth0

auth0: 3.3.0 and @types/auth0: 2.35.9

I am not sure what I am doing wrong here.

Hi @neeraj87 - thanks for raising this

It's hard to tell from the stacktrace you've shared and I'm not familiar with how lambda supports ESM.

You could try commonjs (require('auth0')), esModuleInterop or import * as auth0 from 'auth0'

If you can provide a repo that demonstrates the issue I could debug it for you.

the problem is webpack finds the module .mjs file and tries to load that.

this is the actual error:

#783 (comment)

this seems to be the fix:

#783 (comment)

@mribichich so the solution is to add .js to my auth0 import?

when I do this > import * as auth0 from 'auth0.js'; it gives me the following error

Cannot find module 'auth0.js' or its corresponding type declarations.

@adamjmcgrath I tried using both import * as auth0 from 'auth0' and import auth0 = require('auth0'); but its still not working.
For more clarity I am using serverless-bundle for pack

@neeraj87 is your project in ESM? eg in package.json you have "type": "module"? also what moduleResolution are you uisng?

I wasn't able to fix it, so went out to use axios with the api definitions...
I kept getting other module import errors

👋 Have got a branch that should fix this here https://github.com/auth0/node-auth0/tree/fix-esm

Have tested it on serverless-bundle's Serverless Node.js Starter

You can see the modifications I had to do to get the serverless-bundle application to work with node-auth0 here AnomalyInnovations/serverless-nodejs-starter@master...adamjmcgrath:serverless-nodejs-starter:master

  • npm i https://github.com/auth0/node-auth0/tree/fix-esm
  • Also apply a workaround for the Formidable dependency here #798 (comment)

Could someone test out the branch and let me know if ti works for them? Then I'll go ahead and ship a patch

Hey, is there a way I can npm install it? or do I just have to clone and link the branch?

Hi @PSoltes

Yes, either:

  • npm i https://github.com/auth0/node-auth0/tree/fix-esm
  • or, add "auth0": "github:auth0/node-auth0#fix-esm" to your package dependencies and run npm i

Tested with serverless-webpack deployed via seed using esbuild-loader and typescript. Works fine. Related webpack config:

externals: {
    ...(config.externals || {}),
    formidable: "formidable",
  },
  resolve: {
   ...
    alias: {
      "superagent-proxy": false,
    },
  },
  output: {
    path: path.join(__dirname, ".webpack"),
    library: {
      type: "module",
    },
  },
experiments: {
    outputModule: true,
  },

Thanks @PSoltes - I'll raise a PR

@adamjmcgrath so do I just reinstall auth0 and will it start working?

@adamjmcgrath I updated auth0 and @types/auth0 package on my project but I still keep getting this error

2023-05-17T10:16:31.760Z	undefined	ERROR	Uncaught Exception 	{
    "errorType": "TypeError",
    "errorMessage": "M is not a function",
    "stack": [
        "TypeError: M is not a function",
        "    at Object.91738 (/var/task/src/auth-lambda.js:540:5226)",
        "    at __webpack_require__ (/var/task/src/auth-lambda.js:3143:2090854)",
        "    at Object.59458 (/var/task/src/auth-lambda.js:540:15606)",
        "    at __webpack_require__ (/var/task/src/auth-lambda.js:3143:2090854)",
        "    at Object.16135 (/var/task/src/auth-lambda.js:2839:1079)",
        "    at __webpack_require__ (/var/task/src/auth-lambda.js:3143:2090854)",
        "    at Object.81557 (/var/task/src/auth-lambda.js:2726:36831)",
        "    at __webpack_require__ (/var/task/src/auth-lambda.js:3143:2090854)",
        "    at Object.96781 (/var/task/src/auth-lambda.js:2726:41526)",
        "    at __webpack_require__ (/var/task/src/auth-lambda.js:3143:2090854)"
    ]
}