auth0 / node-auth0

Node.js client library for the Auth0 platform.
MIT License
637 stars 308 forks source link

Getting TypeError upon initilizing ManagementClient #815

Closed neeraj87 closed 1 year ago

neeraj87 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

this is the actual error:

https://github.com/auth0/node-auth0/issues/783#issuecomment-1500973549

this seems to be the fix:

https://github.com/auth0/node-auth0/issues/783#issuecomment-1501110011

neeraj87 commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

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

mribichich commented 1 year ago

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 commented 1 year ago

đź‘‹ 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 https://github.com/AnomalyInnovations/serverless-nodejs-starter/compare/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 commented 1 year ago

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

adamjmcgrath commented 1 year ago

Hi @PSoltes

Yes, either:

PSoltes commented 1 year ago

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 commented 1 year ago

Thanks @PSoltes - I'll raise a PR

neeraj87 commented 1 year ago

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

neeraj87 commented 1 year ago

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