Javascript Node.js 错误错误:找不到模块“猫鼬”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8623205/
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
Node.js error Error: Cannot find module 'mongoose'
提问by Nick Goad
C:\Users\Nick\Desktop\turntablefm\Bots\Super Bot>node bot.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'mongoose'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Object.<anonymous> (C:\Users\Nick\Desktop\turntablefm\Bots\Super Bot\db.j
s:1:78)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Module.require (module.js:357:17)
I already installed it using npm install and I tried reinstalling but that didn't work any ideas?
我已经使用 npm install 安装了它,我尝试重新安装,但没有任何想法?
回答by Prashant Jain
trying installing using this command :
尝试使用此命令安装:
npm install mongoose
do not use the -g switch.
不要使用 -g 开关。
BTW: I ran command prompt in admin mode.
顺便说一句:我在管理员模式下运行命令提示符。
http://prazjain.wordpress.com/2012/04/24/node-js-error-error-cannot-find-module-xyz/
http://prazjain.wordpress.com/2012/04/24/node-js-error-error-cannot-find-module-xyz/
回答by satyam kumar
You can do either of two things to make it run :-
您可以执行以下两项操作中的任一项来使其运行:-
1) Install mongoose globallywith below steps :-
1)通过以下步骤全局安装猫鼬:-
a)npm install mongoose -g
一种)npm install mongoose -g
b) Go to your app directory, where bot.js is located and then run
b) 进入你的 app 目录,bot.js 所在的目录,然后运行
npm link mongoose
npm link mongoose
Explanation :- When you install a package globally via npm, it is downloaded to global node_module folder. For me(Mac user), it's under /usr/local/lib/node_modules/mongoose. We link this to that directory from where you are trying to run module.js.
说明:- 当您通过 npm 全局安装包时,它会下载到全局 node_module 文件夹。对于我(Mac 用户),它位于/usr/local/lib/node_modules/mongoose 下。我们将它链接到您尝试运行 module.js 的那个目录。
2) Another approach is to install mongoose locally, not globallyvia
2)另一种方法是在本地安装猫鼬,而不是通过全局安装
npm install mongoose
npm install mongoose
After following either of these, you will be seeing node_modules --> mongoose folder under the 'bot.js' directory, which means mongoose has been successfully installed.
完成其中任何一项后,您将在“bot.js”目录下看到 node_modules --> mongoose 文件夹,这意味着 mongoose 已成功安装。
Now, run node bot.js , it will work .
现在,运行 node bot.js ,它会工作。
回答by Tim Scott
Install with the --save
option:
使用以下--save
选项安装:
npm install mongoose --save
This adds mongoose it to package.json
which Heroku uses to build your app.
这将添加 mongoose it package.json
,Heroku 使用它来构建您的应用程序。
回答by Angelo Nodari
You have to call in the command line
你必须在命令行中调用
npm install mongoose
npm install mongoose
remember to call this command from the root path of your project
记得从项目的根路径调用这个命令
回答by Gaurav Paliwal
npm install mongoose
it will work and if everything is alright then you will get following in your terminal
它会起作用,如果一切正常,那么您将在终端中得到关注
[email protected] node_modules/mongoose
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected])
回答by SheshPai
I got the same problem in my Mac and did a search in spotlight and found that mongoose is installed in /usr/local/node_modules (when I ran 'npm install mongoose'). Moving the mongoose folder to ~(home) node_modules where npm is supposted to actually install fixed my issue.
我在 Mac 上遇到了同样的问题,并在聚光灯下进行了搜索,发现 mongoose 安装在 /usr/local/node_modules 中(当我运行“npm install mongoose”时)。将 mongoose 文件夹移动到 ~(home) node_modules,其中 npm 实际安装修复了我的问题。
回答by user832147
npm install creates "node_modules" in the pwd(present working directory)
npm install 在 pwd(当前工作目录)中创建“node_modules”
as your application grows, the number of required modules grow and the better approach is to maintain a package.json (reference: https://stackoverflow.com/a/14226133/832147) and then issue just "npm install" instead of installing each.
随着应用程序的增长,所需模块的数量也会增加,更好的方法是维护 package.json(参考:https: //stackoverflow.com/a/14226133/832147)然后只发出“npm install”而不是安装每个。
As an extension when deploying your app on platforms like Heroku, you can ignore (git ignore) your huge node_modules directory of your project. Heroku installs your dependent modules by reading your package.json
作为在 Heroku 等平台上部署应用程序时的扩展,您可以忽略(git ignore)项目中庞大的 node_modules 目录。Heroku 通过读取你的 package.json 来安装你的依赖模块
this approach makes us create the same required node modules for each of our node based projects but it is okay as we need to issue the "npm install" command only once per project
这种方法让我们为每个基于节点的项目创建相同的所需节点模块,但没关系,因为我们只需要为每个项目发出一次“npm install”命令
回答by hhzhu
If you already installed mongoose globally (npm install -g mongoose), then do
如果您已经全局安装了 mongoose (npm install -g mongoose),然后执行
% npm link mongoose
in the project directory. This worked for me.
在项目目录中。这对我有用。
回答by Kamlesh
Just open your project folder in command line and run below command so that mongoose dependency can be added in package.json file. I am 100% sure you will not get such error again.
只需在命令行中打开您的项目文件夹并运行以下命令,以便可以在 package.json 文件中添加 mongoose 依赖项。我 100% 确定您不会再遇到此类错误。
$ sudo npm install --save mongoose
$ sudo npm install --save 猫鼬
回答by Артур Гудиев
I had the same problem. But I just used mongose
instead of mongoose
.
The packages names are almost similar.
我有同样的问题。但我只是用mongose
而不是 mongoose
. 包名几乎相似。