thelinmichael / spotify-web-api-node

A Node.js wrapper for Spotify's Web API.
http://thelinmichael.github.io/spotify-web-api-node/
MIT License
3.08k stars 496 forks source link

spotify_web_api_node_1.default is not a constructor #457

Open Arkmind opened 2 years ago

Arkmind commented 2 years ago

Versions :

{
  "spotify-web-api-node": "^5.0.2",
  "@types/spotify-web-api-node": "^5.0.7",
}

Environment : NestJS (https://docs.nestjs.com/)

Code of the Service :

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import SpotifyWebApi from 'spotify-web-api-node';
import { PrismaService } from 'src/prisma.service';

@Injectable()
export class OAuthService {
  public client: SpotifyWebApi;

  constructor(prismaService: PrismaService, configService: ConfigService) {
    this.client = new SpotifyWebApi({
      clientId: configService.get<string>('SPOTIFY_CLIENT_ID'),
      clientSecret: configService.get<string>('SPOTIFY_CLIENT_SECRET'),
      redirectUri: configService.get<string>('SPOTIFY_REDIRECT_URI'),
    });
  }
}

Error :

spotify_web_api_node_1.default is not a constructor
TypeError: spotify_web_api_node_1.default is not a constructor
    at new OAuthService (/src/oauth/oauth.service.ts:11:19)
    at Injector.instantiateClass (/node_modules/@nestjs/core/injector/injector.js:330:19)
    at callback (/node_modules/@nestjs/core/injector/injector.js:48:41)
    at Injector.resolveConstructorParams (/node_modules/@nestjs/core/injector/injector.js:122:24)
    at Injector.loadInstance (/node_modules/@nestjs/core/injector/injector.js:52:9)
    at Injector.loadProvider (/node_modules/@nestjs/core/injector/injector.js:74:9)
    at async Promise.all (index 5)
    at InstanceLoader.createInstancesOfProviders (/node_modules/@nestjs/core/injector/instance-loader.js:44:9)
    at /node_modules/@nestjs/core/injector/instance-loader.js:29:13
    at async Promise.all (index 1)

How to reproduce : Create new service containing SpotifyWebApi and launch the app

Arkmind commented 2 years ago

In order to fix it temporarly you can use

"esModuleInterop": true

in your tsconfig.json

alexhito commented 2 years ago

I fixed this like so: import SpotifyWebApi = require('spotify-web-api-node');

nodgear commented 1 year ago

I fixed this like so: import SpotifyWebApi = require('spotify-web-api-node');

this syntax doesn't even make sense

you either import it with ES6 (import {} from '') or ESM (require())