node.js 错误:找不到模块“cors”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47706022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:37:41  来源:igfitidea点击:

Error: Cannot find module 'cors'

node.jscors

提问by erickimme

I keep getting this error message below: I tried to find other stackoverflow post and articles but couldn't really sovle it. do any of you guys know what the problem is it?

我一直在下面收到此错误消息:我试图找到其他 stackoverflow 帖子和文章,但无法真正解决它。你们中有人知道是什么问题吗?

`kimeric@pal-nat186-87-17  ~/Desktop/cs390/Assignment9/backEnd  鉳aster ? ● ?  node server9.js
module.js:472
    throw err;
    ^

Error: Cannot find module 'cors'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/kimeric/Desktop/cs390/Assignment9/backEnd/server9.js:11:12)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)`

回答by Ido.Co

Run npm install cors --savefrom the command line in the main project directory to install it and add it to your package.json

npm install cors --save从主项目目录中的命令行运行以安装它并将其添加到您的 package.json

It is possible that the cors module was separated from the main express package a long time ago, and the code you are using was written before that. (Or never was a part of it, to begin with)

可能cors模块很久以前就和主快递包分离了,你用的代码是在这之前写的。(或者从来没有参与其中,一开始)

回答by rohit13807

Need to install cors library. Use terminal and run "npm install cors" then use below in server.js file:

需要安装cors库。使用终端并运行“ npm install cors”,然后在 server.js 文件中使用以下内容:

var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())

Or, refer link: https://www.npmjs.com/package/cors

或者,参考链接:https: //www.npmjs.com/package/cors

Thanks,

谢谢,

回答by Pugmark

Try running npm link corsand then run npm install

尝试运行npm link cors然后运行npm install

回答by Basil Victor

You can simply open the package.json file in any text editor and add cors under dependencies like this: "cors": "2.8.5"

你可以简单地在任何文本编辑器中打开 package.json 文件并在依赖项下添加 cors 如下: "cors": "2.8.5"

Since the latest version of cors is 2.8.5

由于cors的最新版本是2.8.5