TypeError: Passport OAuth2 is not a constructor dependency error

23 Views Asked by At

I am getting an uncontrollable error that appears to be deeply embedded in the passport-oauth2 dependency of passport-google-oauth2. Here is the stack trace:

TypeError: OAuth2 is not a constructor
at Strategy.OAuth2Strategy (/Users/aristos/Documents/MySyllabi-Project/mysyllabi/server/node_modules/passport-oauth2/lib/strategy.js:96:18)
at new Strategy (/Users/aristos/Documents/MySyllabi-Project/mysyllabi/server/node_modules/passport-google-oauth20/lib/strategy.js:52:18)
at Object. (/Users/aristos/Documents/MySyllabi-Project/mysyllabi/server/src/oauth.ts:12:3)
at Module.\_compile (node:internal/modules/cjs/loader:1254:14)
at Module.m.\_compile (/Users/aristos/Documents/MySyllabi-Project/mysyllabi/server/node_modules/ts-node/src/index.ts:1618:23)
at Module.\_extensions..js (node:internal/modules/cjs/loader:1308:10)
at Object.require.extensions. \[as .ts\] (/Users/aristos/Documents/MySyllabi-Project/mysyllabi/server/node_modules/ts-node/src/index.ts:1621:12)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Function.Module.\_load (node:internal/modules/cjs/loader:958:12)
at Module.require (node:internal/modules/cjs/loader:1141:19)

This error doesn't allow the google strategy to be loaded into passport, so when I click on my sign in with google button, the server outputs a 500 error saying 'Unknown authentication strategy "google"'. I've looked at other projects which use the same package, and the passport-oauth2 dependency is the same as mine but it runs without issue.

My code follows the documentation. The error occurs on the new GoogleStrategy( line:

    // oauth.ts
    const passport = require("passport");
    const GoogleStrategy = require("passport-google-oauth20"); // I've also tried                             require("passport-google-auth20).Strategy and it still gives the same error.
    const envconfig = require("../envConfig");


    try {
      passport.use(
      new GoogleStrategy(
      {
        clientID: envconfig.default.GOOGLE_CLIENT_ID,
        clientSecret: envconfig.default.GOOGLE_CLIENT_SECRET,
        callbackURL: `${envconfig.default.GOOGLE_REDIRECT_URI_FIRST}/api/v1/user/google/callback`,
        passReqToCallback: true,
      },
      async function (request, accessToken, refreshToken, profile, done) {
      // implementation
      }
      )
    } catch (e) {
      console.log("in error");
      console.log(e);
    }

Another strange behavior is that this error only appears when I'm running the server in dev mode. When I build the dist folder and run in production mode, the error does not show and I can log in just fine with google. My scripts in my package.json are:


"scripts": {     
  "start": "nodemon ./src/index.ts",
     "test": "vitest",
     "tsc": "tsc",
     "build": "tsc && babel ./src -d dist",
     "production": "DOTENV_CONFIG_PATH=.env.production nodemon -r dotenv/config ./dist/src/index.js --exec babel-node -e js"   

}

Steps to reproduce

I can't reproduce this error in other simple apps I create. I'm posting this issue here so I can get feedback on the potential reasons this is happening or if anyone has seen this before and have a solution.

I cloned my repo on Mac and Windows machines w/ various node versions and the error remains. Environment

Operating System: Mac OS Sonoma 14.2.1 Node version: 18.14.1 (though it also doesn't work in later versions) passport version: 0.6.0 passport-google-oauth20 version: 2.0.0 (the same behavior happens with passport-google-oauth2 package).

0

There are 0 best solutions below