41
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?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Nick Goad
  • 411
  • 1
  • 5
  • 6

20 Answers20

56

try installing mongoose using this command:

npm install mongoose

do not use the -g switch.

BTW: I ran command prompt in admin mode. Reference

Osama Rizwan
  • 615
  • 1
  • 7
  • 19
  • 5
    From the [doc](http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/): 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. – Devs love ZenUML Jun 04 '15 at 10:43
20

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 .

satyam kumar
  • 1,467
  • 13
  • 7
9

Install with the --save option:

npm install mongoose --save

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

Tim Scott
  • 15,106
  • 9
  • 65
  • 79
6

You have to call in the command line

npm install mongoose

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

Angelo Nodari
  • 365
  • 7
  • 14
5
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)
Gaurav Paliwal
  • 1,556
  • 16
  • 27
2

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.

SheshPai
  • 319
  • 4
  • 7
  • Yup - same here. I npm installed it to Users/Name on my Windows box and was getting this error. When I moved node_modules up a few directories it worked just fine. – mattbryanswan Mar 29 '13 at 15:19
2

For Typescript, I had to add @types/mongoose: yarn add -D @types/mongoose

Almaju
  • 1,283
  • 12
  • 31
1

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

Community
  • 1
  • 1
user832147
  • 11
  • 3
1

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

% npm link mongoose

in the project directory. This worked for me.

Pirate X
  • 3,023
  • 5
  • 33
  • 60
hhzhu
  • 599
  • 4
  • 6
1

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

Артур Гудиев
  • 1,004
  • 2
  • 10
  • 26
0

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
Nikhil Ranjan
  • 994
  • 12
  • 16
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.

Devs love ZenUML
  • 11,344
  • 8
  • 53
  • 67
0

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

Kamlesh
  • 5,233
  • 39
  • 50
0

npm install mongoose --save //it will add file in package.json --if still not solved close you cmd and editor and open again

0

if you are using Yarn:

yarn add mongoose
7laria
  • 44
  • 3
0

faced same issue but i had already installed mongoose but still geting cannot find module mongoose. imported mongoose file using import mongoose from 'mongoose'; in node.js after uninstalling the mongoose using

npm uninstall mongoose

and installed again using

npm install mongoose

fixed my issue

Krishnadev. V
  • 345
  • 2
  • 8
  • 23
0

This module is a module for Nest. you must use of below code to fix your issue.

npm i --save @nestjs/mongoose mongoose
Amirali Eshghi
  • 963
  • 1
  • 14
  • 21
  • 1
    [Please do not post an answer that consists essentially of code](https://stackoverflow.com/questions/how-to-answer). Please [edit] your answer to include an explanation of how and why the code solves the problem, when it should be used, what its limitations are, and if possible a link to relevant documentation. – ljmc Feb 11 '23 at 15:47
0

I was having the same problem, I tried reinstalling the module but the real problem was in the node_modules path so try to change it in the main project.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 29 '23 at 09:10
0

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.

Paul
  • 35,689
  • 11
  • 93
  • 122
  • no there isnt but there is a node_modules folder with mongoose in it is it in the wrong spot? on my other computer at my apartment its working fine and its all the same execpt on this computer its getting this error – Nick Goad Dec 24 '11 at 07:55
  • If you're sure it's exactly the same, then make sure that mongodb is installed properly and running on the machine that's having the problem. – Paul Dec 24 '11 at 20:19
-1

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.

jsDebugger
  • 122
  • 2
  • 11