nestjs / javascript-starter

Nest framework JavaScript (ES6, ES7, ES8) + Babel starter 🍰

Home Page:https://nestjs.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: Class constructor MixinStrategy cannot be invoked without 'new'

erikash opened this issue · comments

Bug Report

I've started a new Node.js project and followed the passport authentication tutorial until:
https://docs.nestjs.com/techniques/authentication#jwt-functionality

Current behavior

Expected behavior

Applications should start.

Environment

My jsconfig.json file (I'v changed to esnext as suggested by nestjs/passport#27:

{
  "compilerOptions": {
      "target": "esnext",
      "experimentalDecorators": true
  },
  "exclude": [
      "node_modules"
  ]
}

Nest version: 6.7.2

 
For Tooling issues:
- Node version: v12.6.0  
- Platform:  Mac 

Others:

I've also edited local.strategy.js, i've switched the super(); and this.authService = authService; lines as I was getting an error:
TypeError: Cannot set property 'authService' of undefined

local.strategy.js

import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException, Dependencies } from '@nestjs/common';
import { AuthService } from './auth.service';

@Injectable()
@Dependencies(AuthService)
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(authService) {
    super();
    this.authService = authService;
  }

  async validate(username, password) {
    const user = await this.authService.validateUser(username, password);
    if (!user) {
      throw new UnauthorizedException();
    }
    return user;
  }
}

I've started a project using the typescript-starter and didn't encounter this issue.

Same issue here with babel... any update?

Please, provide a minimal repository which reproduces your issue.

@kamilmysliwiec please take a look on this repo:
https://github.com/sebas-deedee/project-mercury-api

This repo only have the code example for authentication and the mentioned problem.

Nestjs no longer supports JS. TS only
Please correct the support information on the official website

@erikash @kamilmysliwiec @sebas-deedee, I found temporary solution: In .babelrc (or babel.config.js (or .json)) need to change "presets": ["@babel/preset-env"] to "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]

The point is that JS, unlike TS, does not perceive "compilerOptions", which leads to an attempt of interaction between ES5 and ES6 code

Hi, I'm running into the exact same error but my project was set up differently so not sure if relevant but I'm hoping there is a workaround.

I set up a NativeScript shared project with Angular and then used the NextJS schematic to convert the project into an Angular Universal project. After adding the passport dependency and a local strategy described in the docs I ran into this error.

My tsconfig.json shows the target is set to es5 and changing it to es6 or others seems to cause other compilation issues. Not even sure if NativeScript/Angular is compatible with NextJS though. Any tips would be much appreciated 🙏