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

helmet() is not a function #348

Closed
JeffreyArt1 opened this issue Jan 14, 2022 · 9 comments
Closed

helmet() is not a function #348

JeffreyArt1 opened this issue Jan 14, 2022 · 9 comments

Comments

@JeffreyArt1
Copy link

I'm currently using Nest.js 8.1.5.

I have tested helmet 4.6.0 and 5.0.1 with the same results.

This is my server file:

import { NestFactory } from '@nestjs/core'
import helmet from 'helmet';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.enableCors();
  app.use(helmet());

  await app.listen(AppModule.applicationPort);
}

bootstrap()

When I try to run it, it prints this to console.

TypeError: (0 , helmet_1.default) is not a function
    at bootstrap (/Users/jeffreymesa/Desktop/dev.nosync/yp-micro/yp-lottery-api-gateway/src/main.ts:14:17)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
@EvanHahn
Copy link
Member

Sorry you're running into this. What does your tsconfig.json look like?

@daniel-ackermann
Copy link

daniel-ackermann commented Jan 15, 2022

I have the same error. I use Nestjs 8.2.5 with this tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  }
}

@EvanHahn
Copy link
Member

In the short term, you can do one of the following:

  1. Try import helmet = require('helmet')
  2. Enable esModuleInterop in your tsconfig
  3. Downgrade to Helmet v4

In the long term, I'll look into what's causing this. I suspect it will be fixed in an upcoming version (see #345), but I have been busy with other things and haven't had time to look yet. Apologies!

@daniel-ackermann
Copy link

Thank you for your fast solution! That works fine for me!

And sorry for the duplication of #344

@JeffreyArt1
Copy link
Author

Sorry you're running into this. What does your tsconfig.json look like?

mine looks like this:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  }
}

I recently added allowSyntheticDefaultImports to see if that could be a solution.

@JeffreyArt1
Copy link
Author

In the short term, you can do one of the following:

  1. Try import helmet = require('helmet')
  2. Enable esModuleInterop in your tsconfig
  3. Downgrade to Helmet v4

In the long term, I'll look into what's causing this. I suspect it will be fixed in an upcoming version (see #345), but I have been busy with other things and haven't had time to look yet. Apologies!

These solutions worked for me with helmet 4.6.0:

  • import helmet = require('helmet')
  • import * as helmet from 'helmet'

Tried using esModuleInterop in my tsconfig (with version 5) with no luck :(

@EvanHahn
Copy link
Member

This should be fixed in an upcoming version. Please stay tuned!

@EvanHahn
Copy link
Member

I just released helmet@5.0.2 which should fix this problem. Please try updating to see if that fixes things for you!

@JeffreyArt1
Copy link
Author

Upgraded and tested, so far so good. Thanks!

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

No branches or pull requests

3 participants