node.js 找不到模块“mongodb”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14226410/
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 16:54:18  来源:igfitidea点击:

node.js cannot find module 'mongodb'

node.jsmongodb

提问by Eddie

I am going through my first node.js project. I've installed mongodb, have a server.js file, and when I try to run it I get this error

我正在完成我的第一个 node.js 项目。我已经安装了 mongodb,有一个 server.js 文件,当我尝试运行它时出现此错误

module.js:340
    throw err;
         ^
Error: Cannot find module 'mongodb'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)

I am quite certain I have mongodb installed, I am new to unix coming from a C# windows background, but I think this is a path not being configured properly?

我很确定我已经安装了 mongodb,我是来自 C# windows 背景的 unix 新手,但我认为这是一个没有正确配置的路径?

回答by Gates VP

The error you are getting indicates that the NPM package for MongoDB is not correctly installed.

您收到的错误表明 MongoDB 的 NPM 包未正确安装。

The fix here depends on how you plan to leverage NPM. The NPM package manager operates has two different modes of operation: local and global.

此处的修复取决于您计划如何利用 NPM。NPM 包管理器操作有两种不同的操作模式:本地和全局。

The first (and default) mode is "local".

第一个(也是默认的)模式是“本地”。

If you go to the folder with server.jsyou will see a sub-folder named node_modules. Under that folder will be a mongodbfolder. If that folder is not present, then the mongodbmodule is not installed on that path.

如果您转到文件夹,server.js您将看到一个名为node_modules. 在该文件夹下将是一个mongodb文件夹。如果该文件夹不存在,则该mongodb模块未安装在该路径上。

To correct this, cdto that folder and type npm install mongodb. When the process is done you should have the node_modules/mongodbfolder available.

要更正此问题,请cd转到该文件夹​​并键入npm install mongodb. 完成此过程后,您应该可以使用该node_modules/mongodb文件夹。

You can also install MongoDB package globally using npm install -g mongodb. This is useful if you are using lots of node.js command-line stuff, but less useful if you are deploying the whole thing.

您还可以使用npm install -g mongodb. 如果您使用大量 node.js 命令行内容,这很有用,但如果您要部署整个东西,则不太有用。

Side Note: there is an evolving standard around package.json. The package.jsonis a standardized way of including all dependencies for a given module. This allows you to run npm updateor npm installat the root of a project / package and effectively "pull in" all of the dependencies. This greatly simplifies the deployment process and the process of keeping your dependencies in-line.

旁注:有一个不断发展的标准package.json。这package.json是包含给定模块的所有依赖项的标准化方法。这允许您在项目/包的根目录运行npm update或运行npm install并有效地“拉入”所有依赖项。这大大简化了部署过程和保持依赖关系的过程。

回答by Jair Reina

After trying for some time to install it without success (since I'm new to mongo and node), I was missing the npm link step indeed. So just to summarize, I did this:

在尝试安装它一段时间后没有成功(因为我是 mongo 和 node 的新手),我确实错过了 npm 链接步骤。所以总结一下,我是这样做的:

  1. npm install mongodb -g
  2. cd /path/to/my/app/folder
  3. npm link mongodb
  1. npm 安装 mongodb -g
  2. cd /path/to/my/app/文件夹
  3. npm 链接 mongodb

With that in place, I could do this in my application file: require('mongodb').

有了它,我可以在我的应用程序文件中执行此操作:require('mongodb')。

Here are some references, in case you need them:

以下是一些参考资料,以防您需要它们:

回答by Vishwanath gowda k

This Problem would have caused by one among the below reasons.

此问题可能是由以下原因之一引起的。

1) You have not installed mongodb module.

1) 您还没有安装 mongodb 模块。

2) You have installed mongodb in global context but not linked to current application.

2) 您已在全局上下文中安装了 mongodb,但未链接到当前应用程序。

Solution

解决方案

1) Traverse to application top directory and execute npm install mongodb, this will install mongodb module, and your project will automatically detect it.

1) 遍历到应用顶层目录,执行npm install mongodb,这会安装 mongodb 模块,你的项目会自动检测到它。

or

或者

2) Execute npm install mongodb -gto install mongo DB module globally and then Ttaverse to application top directory and link using command npm link mongodbcommand.

2) 执行npm install mongodb -g全局安装 mongo DB 模块,然后 Ttaverse 到应用程序顶级目录并使用命令npm link mongodb命令链接

Usefull Links

有用的链接

http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/

http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/

https://npmjs.org/doc/link.html

https://npmjs.org/doc/link.html

回答by satyam kumar

You can do either of two things to make it run :-

您可以执行以下两项操作中的任一项来使其运行:-

1) Install mongodb globallywith below steps :-

1)使用以下步骤全局安装 mongodb:-

a)npm install mongodb -g

一种)npm install mongodb -g

b) Go to your app directory, where module.js is located and then run

b) 进入你的 app 目录,module.js 所在的目录,然后运行

npm link mongodb

npm link mongodb

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/mongodb. We link this to that directory from where you are trying to run module.js.

说明:- 当您通过 npm 全局安装包时,它会下载到全局 node_module 文件夹。对于我(Mac 用户),它位于/usr/local/lib/node_modules/mongodb 下。我们将它链接到您尝试运行 module.js 的那个目录。

2) Another approach is to install mongodb locally, not globallyvia

2)另一种方法是在本地安装mongodb,而不是通过全局安装

npm install mongodb

npm install mongodb

After following either of these, you will be seeing node_modules --> mongodb folder under the 'module.js' directory, which means mongodb has been successfully installed.

完成其中任何一项后,您将在“module.js”目录下看到 node_modules --> mongodb 文件夹,这意味着 mongodb 已成功安装。

回答by Puneet Kumar

Install mongodb globally with below steps :-

使用以下步骤全局安装 mongodb:-

a) npm install mongodb -g

a) npm install mongodb -g

b) Go to your app directory, where module.js is located and then run

b) 进入你的 app 目录,module.js 所在的目录,然后运行

npm link mongodb

npm 链接 mongodb

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/mongodb. We link this to that directory from where you are trying to run module.js.

说明:- 当您通过 npm 全局安装包时,它会下载到全局 node_module 文件夹。对于我(Mac 用户),它在 /usr/local/lib/node_modules/mongodb 下。我们将它链接到您尝试运行 module.js 的那个目录。

回答by jamsheed

Error

错误

jamsheed-h110m-s2ph:~/project/jamsheed/meanStack/mean$ node server.js
module.js:538
    throw err;
    ^
Error: Cannot find module 'mongodb'
    at Function.Module._resolveFilename (module.js:536:15)
    at Function.Module._load (module.js:466:25)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/haseeb/project/jamsheed/meanStack/mean/server.js:3:13)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)

Solved

解决了

npm install mongodb

Edit package.json = "mongodb": "^3.0.1", change "mongodb": "3.0.1",

npm 安装 mongodb

编辑 package.json = "mongodb": "^3.0.1", 更改 "mongodb": "3.0.1",

回答by Ankush Chaudhary

May be you are trying to run other then project path.I have faced the same problem and got resolved by following few steps given below.

可能是您正在尝试运行其他项目路径。我遇到了同样的问题,并通过以下几个步骤得到了解决。

1.) Go to your project directory using command shell "cd /project path".

1.) 使用命令外壳“cd /project path”转到您的项目目录

2.) Now Run your project.

2.) 现在运行你的项目。

回答by CopyLeft

My problem was that when I originally created my package.json file, I had made the "version" key correspond to a value of "1.0.0". When I changed that to version value to reflect the version I wanted to use under my "dependencies" key, I got rid of the error. There is a picture below of the potential fix. Notice how the "version" and "mongodb" under "dependencies" match. enter image description here

我的问题是,当我最初创建我的 package.json 文件时,我已经将“版本”键对应于“1.0.0”的值。当我将其更改为版本值以反映我想在“依赖项”键下使用的版本时,我摆脱了错误。下面是潜在修复的图片。注意“dependencies”下的“version”和“mongodb”是如何匹配的。 在此处输入图片说明

回答by Daniel C. Deng

I am new to MongoDB. After spending hours of installing mongodb through npm, I finally got the picture. See, there are actually three "mongodb" you need to deal with (I am using OSX):

我是 MongoDB 的新手。在花了几个小时通过 npm 安装 mongodb 之后,我终于得到了图片。看,实际上您需要处理三个“mongodb”(我使用的是 OSX):

1. The driver used in NodeJS: That is: var mongo = require('/usr/local/lib/node_modules/mongodb'). Or, you may use "npm link" as mentioned by earlier post in order to avoid the long path. Command "npm install -g mongodb" installs the mongodb driver under /usr/local/lib (this is what "-g" means). This guy took hours and hours to install, don't know why, so be patient!

1、NodeJS中使用的驱动:即:var mongo = require('/usr/local/lib/node_modules/mongodb')。或者,您可以使用之前帖子中提到的“npm 链接”以避免长路径。命令“npm install -g mongodb”在 /usr/local/lib 下安装 mongodb 驱动程序(这就是“-g”的意思)。这家伙花了好几个小时来安装,不知道为什么,所以要有耐心!

2. The mongodb utilities (i.e., the UNIX executable commands). You download them from http://www.mongodb.org/downloads. It contains the mongod command that allows you to start the mongodb database. So in my /usr/local/bin, I created a symbolic link, mongod, pointing to /usr/local/lib/node_modules/mongodb-osx-x86_64-2.6.7/bin/mongod, so I can execute mongod from anywhere.

2. mongodb 实用程序(即 UNIX 可执行命令)。您可以从http://www.mongodb.org/downloads下载它们。它包含允许您启动 mongodb 数据库的 mongod 命令。所以在我的/usr/local/bin中,我创建了一个符号链接mongod,指向/usr/local/lib/node_modules/mongodb-osx-x86_64-2.6.7/bin/mongod,这样我就可以从任何地方执行mongod .

3. The mongodb database pointed by /data/db. So under /data, I created a symbolic link, db, pointing to /Users/your_user_id/Database/mongodb (or anywhere you like to put it), in which mongodb is an empty directory you may create via mkdir.

3、/data/db指向的mongodb数据库。所以在 /data 下,我创建了一个符号链接 db,指向 /Users/your_user_id/Database/mongodb(或任何你喜欢放置的地方),其中 mongodb 是一个空目录,你可以通过 mkdir 创建。

Because you have installed the #2 mongodb above, so you can execute mongod at the command line, which will create and start the database under mongodb #3.

因为你已经安装了上面的#2 mongodb,所以你可以在命令行执行mongod,它会在mongodb #3下创建并启动数据库。

Because you have mongodb #1 installed by npm, so now you can require it in your NodeJS program.

因为你已经通过 npm 安装了 mongodb #1,所以现在你可以在你的 NodeJS 程序中使用它。

Job done!

任务完成!

回答by mangesh

I got the same Problem and I solve by these steps.

我遇到了同样的问题,我通过这些步骤解决了。

1.Go to Your Project Dir.(cd your Project Path)

1.转到您的项目目录(cd您的项目路径)

2.Run this command in command shell( install npm)

2.在命令shell中运行此命令(安装npm)

3.Run your Project (node app.js for example)

3.运行你的项目(例如 node app.js)