8

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"
  }
}
Vaulient
  • 335
  • 1
  • 4
  • 14

2 Answers2

4

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

Lucas Hendren
  • 2,786
  • 2
  • 18
  • 33
  • 1
    Thank you kindly , I will look into the included resources. I have included my package.json in the question as well. – Vaulient Jul 08 '19 at 02:37
  • After viewing your package.json the issue is that you need to declare your typings correctly, Those resources should help with more specific walkthroughs – Lucas Hendren Jul 08 '19 at 02:41
  • This may also help https://www.typescriptlang.org/docs/handbook/tsconfig-json.html – Lucas Hendren Jul 08 '19 at 02:45
  • 1
    Thank you! Based on your source: https://stackoverflow.com/a/54417184/11117237 seemed to do the trick. Managed to declare the module and now the error is gone. Can't thank you enough! I appreciate your help. – Vaulient Jul 09 '19 at 03:14
  • 1
    I have the same problem, I don't understand where to edit it. I'm just using a third party mongoose-encrypt module – Akhila Jul 29 '20 at 22:22
0

Maybe it is because the listFilesInDepth is asynchronous and you have to handle the promise or use async await on that method.

Is the method listFilesInDepth called before doing the context.get?

Ion
  • 1,262
  • 12
  • 19
  • I believed that was the case at first too. But upon receiving the replies from here https://stackoverflow.com/questions/56895611/is-it-possible-to-module-exports-a-local-variable-from-async-function I am now able to call the value of targetFiles through obj.function(), however I'm not able to make it into a vraible where I could show it in a console log such as console.log("The file is:" + whateverVariable); – Vaulient Jul 08 '19 at 02:40