TypeScript: how to fix error: Could not find a declaration file for module


TS7016: Could not find a declaration file for module 'cookieparser'. 'D:/Nuxt/demo/node_modules/cookieparser/js/cookieparser.js' implicitly has an 'any' type.
  Try `npm install @types/cookieparser` if it exists or add a new declaration (.d.ts) file containing `declare module 'cookieparser';`
  > 1 | import * as cookieparser from 'cookieparser'
      |                               ^^^^^^^^^^^^^^
    2 | 
    3 | export const state= () => ({
    4 |       auth: null

Step 1: try to install package follow the error message

Example:

D:\Nuxt\demo>npm install @types/cookieparser
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@types%2fcookieparser - Not found
npm ERR! 404
npm ERR! 404  '@types/cookieparser@latest' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2020-12-03T02_50_55_164Z-debug.log

If you got error, you must do the second step

Step 2:

Make file @types/cookieparser.d.ts with content follow the error message

declare module 'cookieparser';

Step 2.a

edit file tsconfig.json:

{
  "compilerOptions": {
    ...
    "baseUrl": ".",
    "paths": {
      "~/*": [
        "./*"
      ],
      "@/*": [
        "./*"
      ],
      "*" : ["@types/*"]
    },
    "types": [
      "@types/node",
      "@nuxt/types",
      "@nuxtjs/axios"
    ]
  },
  "exclude": [
    "node_modules",
    ".nuxt",
    "dist",
    "@types",  
  ]
}

Leave a Reply