node.jstypescriptnode-modulesrequestcontext

How to resolve "Could not find a declaration file for module 'request-context'. "?


I am working on three files at the moment which are index.js , index.main.js and app.js. I'm using request-context to grab a variable from index.main.js and pass it to index.js.

In app.js (A file I created in my server folder) I have the following code

    //full code in app.js

    const contextService = require("request-context");
    const app = express();

    app.use(contextService.middleware("request"));

I have tried running these following commands

npm install --save typescript @types/node @types/react @types/react-dom @types/jest

npm install -D @types/request-context

and also tried using before import

// @ts-ignore

To no avail.

When I check my app.js I notice three dots on the word "require" which shows:

Could not find a declaration file for module 'request-context'. '/home/servertest/Desktop/folder/folder1/src/component_NodeJS/server/node_modules/request-context/lib/index.js' implicitly has an 'any' type. Try npm install @types/request-context if it exists or add a new declaration (.d.ts) file containing declare module 'request-context';ts(7016)

In index.main.js I have the following

 async function listFilesInDepth()

    {
      const {Storage} = require('@google-cloud/storage');

      const storage = new Storage();

      const bucketName = 'probizmy';

      const [files] = await storage.bucket(bucketName).getFiles();

      const contextService = require("request-context");

      console.log('List Of Files Available:'); 

        files.forEach(file =>

          {

            targetFiles = file.name;

            console.log(`-----`+file.name);
          });

          contextService.set("request:targetFileKey", targetFiles);

          return targetFiles;
    }

and in index.js I have the following code

const contextService = require("request-context");
const targetFiles = contextService.get("request:targetFileKey");

console.log(targetFiles) //output shows undefined

I'm suspecting the request-context error is why I'm getting undefined as the output. My expected result is for the value of targetFiles to be output on the console log.

Hoping to get some insight on this. Any help would be greatly appreciated! Thank you :)

Edited:

As requested I have included package.json

{
  "name": "server",
  "version": "0.1.81",
  "description": "Server NodeJS For Internel Process",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "@google-cloud/storage": "^2.4.2",
    "@google-cloud/vision": "^0.25.0",
    "@types/jest": "^24.0.15",
    "@types/node": "^12.0.12",
    "@types/react": "^16.8.23",
    "@types/react-dom": "^16.8.4",
    "alphabet-generator": "^1.0.1",
    "body-parser": "^1.18.3",
    "cheerio": "^1.0.0-rc.2",
    "cors": "^2.8.5",
    "express": "^4.16.4",
    "format": "^0.2.2",
    "grpc": "^1.19.0",
    "multer": "^1.4.1",
    "natural": "^0.6.3",
    "path": "^0.12.7",
    "request": "^2.88.0",
    "request-context": "^2.0.0",
    "require-all": "^3.0.0",
    "require-dir": "^1.2.0",
    "string-similarity": "^3.0.0",
    "typescript": "^3.5.2"
  },
  "devDependencies": {
    "babel-plugin-root-import": "^6.2.0"
  }
}

Solution

  • I have had this issue before and there is a problem with how you are declaring your typings. There are a few ways to solve this I will include below with more links for information with a link to the typescript declaration documentation and several other StackOverflow resources. If you include your package.json it may help determine what you specifically still need to do. You likely need to add in a types folder/file with the following lines of code

    "typeRoots": [
      "./node_modules/@types",
      "./types"
    ]                       /* List of folders to include type definitions from. */
    

    And then create a second file of the form *.d.ts to list your needed modules that are erroring. So something like

    declare module 'request-context';
    

    The issue could also be in your package.js file. You may need to declare where your typings are like so

    // package.json
    {
      "typings": "dist/src/*.d.ts"
    }
    

    Different resources

    https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html

    Could not find a declaration file for module 'module-name'. '/path/to/module-name.js' implicitly has an 'any' type

    Typescript react - Could not find a declaration file for module ''react-materialize'. 'path/to/module-name.js' implicitly has an any type

    Could not find a declaration file for module

    TypeScript: Could not find a declaration file for module in unit tests, only