【问题标题】:How to resolve "Could not find a declaration file for module 'request-context'. "?如何解决“找不到模块'request-context'的声明文件。”?
【发布时间】:2019-11-17 12:48:38
【问题描述】:

我目前正在处理三个文件,它们是 index.js 、 index.main.js 和 app.js。我正在使用 request-context 从 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"
  }
}

【问题讨论】:

    标签: node.js typescript node-modules requestcontext


    【解决方案1】:

    可能是因为 listFilesInDepthasynchronous 并且您必须处理承诺或在该方法上使用异步等待。

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

    【讨论】:

    • 我一开始也相信是这样。但是在收到来自stackoverflow.com/questions/56895611/… 的回复后,我现在可以通过 obj.function() 调用 targetFiles 的值,但是我无法将其变为可以在控制台日志中显示的变量,例如console.log("文件是:" + 不管变量);
    【解决方案2】:

    我之前也遇到过这个问题,你如何声明你的类型有问题。有几种方法可以解决这个问题,我将在下面提供更多信息链接,其中包含指向 typescript 声明文档和其他几个 StackOverflow 资源的链接。如果您包含您的 package.json,它可能有助于确定您具体还需要做什么。您可能需要使用以下代码行添加类型文件夹/文件

    "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

    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

    【讨论】:

    • 非常感谢,我会查看包含的资源。我也在问题中包含了我的 package.json。
    • 查看您的 package.json 后,问题是您需要正确声明您的类型,这些资源应该有助于更具体的演练
    • 谢谢!根据您的消息来源:stackoverflow.com/a/54417184/11117237 似乎可以解决问题。设法声明模块,现在错误消失了。不能感谢你!感谢您的帮助。
    • 我有同样的问题,我不明白在哪里编辑它。我只是在使用第三方 mongoose-encrypt 模块
    猜你喜欢
    • 2020-07-30
    • 2018-10-25
    • 2017-08-13
    • 1970-01-01
    • 2021-01-02
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多