如何解决"Could not find a declaration file for module 'request-context'. "?

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

我目前正在处理三个文件,它们是 index.js、index.main.js 和 app.js。我正在使用请求上下文从 index.main.js 中获取一个变量并将其传递给 index.js.

在app.js(我在服务器文件夹中创建的文件)中,我有以下代码

    //full code in app.js

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

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

我已经尝试 运行 以下命令

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

npm install -D @types/request-context

并且还尝试在导入前使用

// @ts-ignore

无果。

当我检查我的 app.js 时,我注意到 "require" 这个词上的三个点显示:

找不到模块 'request-context' 的声明文件。 '/home/servertest/Desktop/folder/folder1/src/component_NodeJS/server/node_modules/request-context/lib/index.js' 隐式具有 'any' 类型。 如果存在,请尝试 npm install @types/request-context 或添加包含 declare module 'request-context';ts(7016)

的新声明 (.d.ts) 文件

在index.main.js中我有以下

 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;
    }

在index.js中我有以下代码

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

console.log(targetFiles) //output shows undefined

我怀疑请求上下文错误是我的输出未定义的原因。我的预期结果是在控制台日志上输出 targetFiles 的值。

希望对此有所了解。任何帮助将不胜感激!谢谢:)

已编辑:

根据要求,我已经包含了 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"
  }
}

可能是因为 listFilesInDepthasynchronous 并且您必须处理 promise 或对该方法使用 async await。

在执行 context.get 之前是否调用了方法 listFilesInDepth?

我以前遇到过这个问题,你声明输入的方式有问题。有几种方法可以解决这个问题,我将在下面包含更多 links 以获取带有 link 的信息到打字稿声明文档和其他几个 Whosebug 资源。如果您包括 package.json,它可能有助于确定您还需要做什么。您可能需要使用以下代码行

添加类型 folder/file
"typeRoots": [
  "./node_modules/@types",
  "./types"
]                       /* List of folders to include type definitions from. */

然后创建 *.d.ts 格式的第二个文件以列出您需要的错误模块。所以像

declare module 'request-context';

问题也可能出在您的 package.js 文件中。您可能需要像这样声明您的打字位置

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

资源不同

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