简体   繁体   中英

Node.js error Error: Cannot find module 'mongoose'

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?

try installing mongoose using this command:

npm install mongoose

do not use the -g switch.

BTW: I ran command prompt in admin mode. Reference

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

1) Install mongoose globally with below steps :-

a) npm install mongoose -g

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

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.

2) Another approach is to install mongoose locally, not globally via

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.

Now, run node bot.js , it will work .

Install with the --save option:

npm install mongoose --save

This adds mongoose it to package.json which Heroku uses to build your app.

You have to call in the command line

npm install mongoose

remember to call this command from the root path of your project

npm install mongoose

it will work and if everything is alright then you will get following in your terminal

mongoose@4.4.19 node_modules/mongoose
├── sliced@1.0.1
├── hooks-fixed@1.1.0
├── regexp-clone@0.0.1
├── mpromise@0.5.5
├── muri@1.1.0
├── kareem@1.0.1
├── mpath@0.2.1
├── bson@0.4.23
├── mquery@1.10.0 (sliced@0.0.5, debug@2.2.0, bluebird@2.10.2)
└── mongodb@2.1.18 (readable-stream@1.0.31, es6-promise@3.0.2, mongodb-core@1.3.18)

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.

npm install creates "node_modules" in the pwd(present working directory)

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.

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

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

If you already installed mongoose globally (npm install -g mongoose), then do

% npm link mongoose

in the project directory. This worked for me.

I had the same problem. But I just used mongose instead of mongoose . The packages names are almost similar.

You are using windows operation system which mongoose doesn't support. It is apparent from this error message:

C:\>npm install mongoose
npm http GET https://registry.npmjs.org/mongoose/2.5.10
npm http 304 https://registry.npmjs.org/mongoose/2.5.10
npm http GET https://registry.npmjs.org/hooks/0.2.0
npm http GET https://registry.npmjs.org/mongodb/0.9.9-4
npm http 304 https://registry.npmjs.org/mongodb/0.9.9-4
npm http 304 https://registry.npmjs.org/hooks/0.2.0
npm WARN package.json mongodb@0.9.9-4 No README.md file found!
npm ERR! notsup Unsupported
npm ERR! notsup Not compatible with your operating system or architecture: mongo
db@0.9.9-4
npm ERR! notsup Valid OS:    linux,darwin,freebsd
npm ERR! notsup Valid Arch:  any
npm ERR! notsup Actual OS:   win32
npm ERR! notsup Actual Arch: x64

npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
ejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "mongoose"
npm ERR! cwd C:\
npm ERR! node -v v0.8.18
npm ERR! npm -v 1.2.2
npm ERR! code EBADPLATFORM
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     C:\npm-debug.log
npm ERR! not ok code 0

From the doc/blog

In general, the rule of thumb is:

If you're installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.

If you're installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.

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.

$ sudo npm install --save mongoose

对于打字稿,我不得不添加@types/mongoose: yarn add -D @types/mongoose

npm install mongoose --save //它会在package.json中添加文件 --如果仍然没有解决关闭你的cmd和编辑器并再次打开

in the directory housing bot.js, is there a node_modules folder that has a mongoose folder in it? Is your mongodb server running?

You can test it also by being in the project's root directory, calling node (no args, to open the REPL), and trying to require mongoose there.

on windows if you do

npm install mongoose

it will install it by default on your C:\\ Drive

and if you try to run some *.js file from say D:\\ drive

it will give you same error.

so i guess the installation directory and the *.js file should have same root.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM