gpt4 book ai didi

javascript - 获取 "Error: Cannot find module ' 请求'"即使我添加了请求模块

转载 作者:太空宇宙 更新时间:2023-11-03 22:56:51 25 4
gpt4 key购买 nike

我正在尝试对我的云功能使用request

我已经通过npm install request安装了request包。与其他包一样,它位于 node_modules 中:

enter image description here

但是,当我尝试上传我的云功能时,收到以下错误消息:

Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'request'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/srv/index.js:3:17)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)

这是我的 index.js:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const request = require('request');

admin.initializeApp();
const db = admin.firestore();

function getFoursquarePlace(midPoint){
let url = "https://api.foursquare.com/v2/venues/search?" +
"ll=" + midPoint.latitude + "," + midPoint.longitude + "&client_id=" +
client_id + "&client_secret=" + client_secret + "&v=" + version + "&radius=" + radius
+ "&limit=" + limit + "&categoryId=" + categorySet;

request(url, options, (error, res, body) => {
if (error) {
return console.log(error)
}

if (!error && res.statusCode === 200) {
// do something with JSON, using the 'body' variable
console.log("body: " + res.body);
}
});

有谁知道问题出在哪里吗?我尝试卸载并重新安装request,但仍然收到错误。

编辑:我已经检查了 package-lock.json 并且 request 包位于其中:

"request": {
"version": "2.88.0",
"resolved": "https://registry.npmjs.org/request/-/request-2.88.0.tgz",
"integrity": "sha512-NAqB3rk3LgxdsfHnTBNwN0E+lHDAJzu7m0328eY08z2/A0hg==",
"requires": {
"aws-sign2": "~0.7.0",
"aws4": "^1.8.0",
"caseless": "~0.12.0",
"combined-stream": "~1.0.6",
"extend": "~3.0.2",
"forever-agent": "~0.6.1",
"form-data": "~2.3.2",
"har-validator": "~5.1.0",
"http-signature": "~1.2.0",
"is-typedarray": "~1.0.0",
"isstream": "~0.1.2",
"json-stringify-safe": "~5.0.1",
"mime-types": "~2.1.19",
"oauth-sign": "~0.9.0",
"performance-now": "^2.1.0",
"qs": "~6.5.2",
"safe-buffer": "^5.1.2",
"tough-cookie": "~2.4.3",
"tunnel-agent": "^0.6.0",
"uuid": "^3.3.2"
}
},

package.json

{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"dependencies": {
"firebase-admin": "^8.0.0",
"firebase-functions": "^3.1.0",
"geofirestore": "3.3.1"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1",
"firebase-functions-test": "^0.1.6"
},
"private": true
}

最佳答案

当您“npm install”错误文件夹的依赖项时,可能会发生这种情况,正如我刚刚意识到的那样。我在“src”子目录中有一个 Vue 项目,在“functions”子目录中有一个 index.js(Firebase 云函数)。

Vue项目的依赖项必须使用“npm install”安装在主目录中。相反,Firebase 云函数的依赖项必须通过在“functions”子目录中运行“npm install”来安装。我不小心在主目录中安装了依赖项,并且在我意识到我做了什么之前试图理解为什么它提示它找不到依赖项(googleapis),这让我很头疼。解决方法当然是将其安装在正确的位置(因此它显示在正确的 package.json 中),并从错误的位置卸载它以保持整洁。

npm uninstall request //removes the module from node_modules, but not package.json or npm uninstall --save request
npm cache clear --force
npm install --save --save-exact request@2.88.0
firebase deploy --only functions

关于javascript - 获取 "Error: Cannot find module ' 请求'"即使我添加了请求模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59692263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com