Vau*_*ent 5 requestcontext node.js node-modules typescript
我目前正在处理三个文件,分别是 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"));
Run Code Online (Sandbox Code Playgroud)
我试过运行以下命令
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
npm install -D @types/request-context
Run Code Online (Sandbox Code Playgroud)
并在导入前尝试使用
// @ts-ignore
Run Code Online (Sandbox Code Playgroud)
无济于事。
当我检查我的 app.js 时,我注意到“require”这个词上有三个点,它显示:
找不到模块“请求上下文”的声明文件。'/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;
}
Run Code Online (Sandbox Code Playgroud)
在 index.js 中我有以下代码
const contextService = require("request-context");
const targetFiles = contextService.get("request:targetFileKey");
console.log(targetFiles) //output shows undefined
Run Code Online (Sandbox Code Playgroud)
我怀疑请求上下文错误是为什么我将未定义为输出的原因。我的预期结果是在控制台日志上输出 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"
}
}
Run Code Online (Sandbox Code Playgroud)
我以前遇到过这个问题,并且您声明输入的方式存在问题。有几种方法可以解决这个问题,我将在下面提供更多信息链接,其中包含打字稿声明文档和其他几个 StackOverflow 资源的链接。如果您包含 package.json,它可能有助于确定您具体还需要做什么。您可能需要使用以下代码行添加类型文件夹/文件
"typeRoots": [
"./node_modules/@types",
"./types"
] /* List of folders to include type definitions from. */
Run Code Online (Sandbox Code Playgroud)
然后创建 *.d.ts 形式的第二个文件来列出出现错误的所需模块。所以像
declare module 'request-context';
Run Code Online (Sandbox Code Playgroud)
该问题也可能出现在您的 package.js 文件中。您可能需要声明您的打字位置,如下所示
// package.json
{
"typings": "dist/src/*.d.ts"
}
Run Code Online (Sandbox Code Playgroud)
不同的资源
https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
找不到模块“模块名称”的声明文件。'/path/to/module-name.js' 隐式具有 'any' 类型
Typescript React - 找不到模块“react-materialize”的声明文件。'path/to/module-name.js' 隐式具有任何类型
TypeScript:无法在单元测试中找到模块的声明文件,仅
| 归档时间: |
|
| 查看次数: |
18016 次 |
| 最近记录: |