node.js - 如何解析 "Could not find a declaration file for module ' 请求上下文'。 “?

标签 node.js typescript node-modules requestcontext

我目前正在处理三个文件,即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"
  }
}

最佳答案

我以前遇到过这个问题,并且您声明输入的方式存在问题。有几种方法可以解决这个问题,我将在下面提供更多信息链接,其中包含 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

关于node.js - 如何解析 "Could not find a declaration file for module ' 请求上下文'。 “?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56927551/

相关文章:

reactjs - 在 react redux 中分派(dispatch)对子组件的操作的惯用方法,而无需一直传递 props

jenkins - 在 Jenkins 中,有没有办法保留 npm 包,这样我就不必在每个构建中安装它们?

node.js - 如何在多个node.js文件中设置全局模块

node.js - Node.js 中的缓冲区比较

javascript - 防止 Websocket 向我的 console.log 发送垃圾邮件

javascript - 再次关于 javascript 中的 async/await

Angular react 形式错误 : Must supply a value for form control with name:

javascript - 使用 React/Nodejs 通过电子邮件发送生成的 PDF 文件

typescript - 为什么在用作函数参数之前需要将子类型分配给变量?

javascript - 如何通过Sequelize连接多个数据库?