node.js找不到模块'mongodb' [英] node.js cannot find module 'mongodb'
问题描述
我正在完成我的第一个node.js项目.我已经安装了mongodb,有一个server.js文件,当我尝试运行它时,出现此错误
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
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)
我敢肯定我已经安装了mongodb,我是来自C#Windows背景的UNIX新手,但是我认为这是未正确配置的路径吗?
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的NPM软件包.
The error you are getting indicates that the NPM package for MongoDB is not correctly installed.
此处的解决方法取决于您计划如何利用NPM. 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.
第一种(和默认)模式是本地".
The first (and default) mode is "local".
如果使用server.js
转到文件夹,将看到一个名为node_modules
的子文件夹.在该文件夹下将是一个mongodb
文件夹.如果该文件夹不存在,则mongodb
模块未安装在该路径上.
If you go to the folder with server.js
you will see a sub-folder named node_modules
. Under that folder will be a mongodb
folder. If that folder is not present, then the mongodb
module is not installed on that path.
要更正此问题,请cd
到该文件夹,然后键入npm install mongodb
.完成该过程后,您应该具有node_modules/mongodb
文件夹.
To correct this, cd
to that folder and type npm install mongodb
. When the process is done you should have the node_modules/mongodb
folder available.
您还可以使用npm install -g mongodb
在全球范围内安装MongoDB软件包.如果您使用大量的node.js命令行工具,这将很有用,但如果要部署整个工具,则将没有太大用处.
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.
旁注:package.json
周围有一个不断发展的标准. package.json
是包括给定模块的所有依赖项的标准化方法.这使您可以在项目/程序包的根目录下运行npm update
或npm install
,并有效地拉入"所有依赖项.这大大简化了部署过程以及保持依存关系在线的过程.
Side Note: there is an evolving standard around package.json
. The package.json
is a standardized way of including all dependencies for a given module. This allows you to run npm update
or npm install
at 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.
这篇关于node.js找不到模块'mongodb'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!