helmetjs / helmet

Help secure Express apps with various HTTP headers

Home Page:https://helmetjs.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Helmet 6.1.2: Type 'typeof import("/srv/www/cz.vegilove.api/node_modules/helmet/index")' has no call signatures.

rostalevicek opened this issue · comments

import helmet from 'helmet';
app.use(helmet())

Leads to error:

error TS2349: This expression is not callable.
Type 'typeof import("/srv/www/cz.vegilove.api/node_modules/helmet/index")' has no call signatures.
app.use( helmet() )

The probelm is default export. It exports the object of named functions, therefore it is possible to use helmet.xxxx() but it does not include "function helmet()".

One possible solution is to include "helmet" in named exports... i guess

In the short term, you can fix this by installing helmet@6.0.1.

What does your tsconfig.json file look like?

Having the same error with v6.1.2

My tsconfig:

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "Node16",
    "moduleResolution":"Node16",
    "outDir": "./dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "sourceMap": true,
    "resolveJsonModule": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "skipLibCheck": true,
    "rootDir": "./src",
    "baseUrl": ".",
    "paths": {
      "#api/*": ["./src/*"]
    }
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts"]
}

I made the following app and could not reproduce your issue:

package.json
{
  "scripts": {
    "build": "tsc"
  },
  "dependencies": {
    "helmet": "^6.1.2",
    "typescript": "^5.0.4"
  }
}
tsconfig.json
{
  "compilerOptions": {
    "target": "ESNext",
    "module": "Node16",
    "moduleResolution":"Node16",
    "outDir": "./dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "sourceMap": true,
    "resolveJsonModule": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "skipLibCheck": true,
    "baseUrl": "."
  },
  "include": ["app.ts"]
}
app.ts
import helmet from "helmet";

helmet();

Everything seemed to work fine when I run npm run build.

What version of TypeScript are you using? What about the version of Node.js? Is anything special happening in your package.json?

I just published helmet@6.1.3 to fix a similar issue. Is it possible that upgrading fixes your problem too? (I doubt it, but I'm hopeful!)

What version of TypeScript are you using? What about the version of Node.js? Is anything special happening in your package.json?

Typescript v5.0.4
package.json with "type": "module", maybe that's the issue?

I just published helmet@6.1.3 to fix a similar issue. Is it possible that upgrading fixes your problem too? (I doubt it, but I'm hopeful!)

Just tried with v6.1.3 still same issue unfortunately. I'll setup a repro.

@EvanHahn OK from what I've seem the problem is type: module

repro: https://github.com/rafaelpimpa/helmet-issue-414

I think it's easily fixable with "exports" key in package.json: https://nodejs.org/api/packages.html#main-entry-point-export

@rafaelpimpa Thanks, this is very helpful. I'll deploy a fix ASAP. (If you know exactly how to fix this, let me know. Otherwise I'll start digging.) Update: I believe I have a fix.

This should be fixed in helmet@6.1.4. Let me know if you run into any other problems. Sorry for the trouble!

I also have still the problem. When launched using ts-node with some specail params, i can use it in dev, but to build production code with tsc (typescript 5.0.4), will fail with error:

node_modules/helmet/index.d.ts:120:1 - error TS2309: An export assignment cannot be used in a module with other exported elements.

120 export = helmet
~~~~~~~~~~~~~~~

My tsconfig looks like:
{ "compileOnSave": false, "compilerOptions": { "outDir": "./dist", "target": "ESNext", "module": "ESNext", "moduleResolution": "NodeNext", "jsx":"react-jsx", "allowSyntheticDefaultImports": true, "preserveSymlinks": true, "resolveJsonModule": true, "esModuleInterop": true, "declaration": false, "noImplicitThis": true, "strictNullChecks": true, "lib": [ "ESNext", "DOM" ], "typeRoots": [ "src/@types", "node_modules/@types" ], "sourceMap": true, "plugins": [ { "transform": "@formatjs/ts-transformer", "import": "transform", "type": "config", "overrideIdFn": "[sha512:contenthash:base64:6]", "ast": true } ] }, "include": [ "src/**/*", "utils/**/*" ], "exclude": [ "node_modules" ] }

And i am using esm ("type":"module" in package.json).

@rostalevicek Sorry about this. Let's move discussion to #419.