Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting TypeError upon initilizing ManagementClient #815

Closed
neeraj87 opened this issue Apr 21, 2023 · 13 comments · Fixed by #828
Closed

Getting TypeError upon initilizing ManagementClient #815

neeraj87 opened this issue Apr 21, 2023 · 13 comments · Fixed by #828
Labels

Comments

@neeraj87
Copy link

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.

@adamjmcgrath
Copy link
Contributor

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.

@mribichich
Copy link

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)

@neeraj87
Copy link
Author

@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.

@neeraj87
Copy link
Author

neeraj87 commented May 1, 2023

@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

@PSoltes
Copy link

PSoltes commented May 2, 2023

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

@mribichich
Copy link

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

@adamjmcgrath
Copy link
Contributor

👋 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

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

@PSoltes
Copy link

PSoltes commented May 5, 2023

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

@adamjmcgrath
Copy link
Contributor

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

@PSoltes
Copy link

PSoltes commented May 5, 2023

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,
  },

@adamjmcgrath
Copy link
Contributor

Thanks @PSoltes - I'll raise a PR

@neeraj87
Copy link
Author

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

@neeraj87
Copy link
Author

@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)"
    ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants