Node.js error Error: Cannot find module 'mongoose'
Asked Answered
E

21

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

Etheline answered 24/12, 2011 at 6:54 Comment(2)
feel free to mark the answer if it has helped you. CheersDiet
I used pip to install mongoose. But i am having the same problem.Can anyone point me in right direction.?Vagrom
D
58

try installing mongoose using this command:

npm install mongoose

do not use the -g switch.

BTW: I ran command prompt in admin mode. Reference

Diet answered 23/4, 2012 at 13:16 Comment(1)
From the doc: 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.Berube
F
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 .

Fulvi answered 18/9, 2016 at 14:35 Comment(1)
Clearest explanation imho, it helped me understand. +1!Commensurate
H
9

Install with the --save option:

npm install mongoose --save

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

Handbook answered 24/1, 2016 at 21:54 Comment(0)
S
6

You have to call in the command line

npm install mongoose

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

Salve answered 8/10, 2013 at 9:24 Comment(0)
T
5
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])
Trammell answered 5/6, 2016 at 6:22 Comment(0)
I
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.

Inviting answered 23/4, 2012 at 17:37 Comment(1)
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.Eat
C
2

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

Cassandra answered 9/1, 2020 at 8:28 Comment(0)
D
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://mcmap.net/q/390407/-is-it-possible-to-automatically-install-the-required-modules-for-a-node-js-script ) 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

Debenture answered 24/1, 2014 at 19:38 Comment(0)
L
1

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

% npm link mongoose

in the project directory. This worked for me.

Lizbeth answered 19/6, 2016 at 18:46 Comment(0)
I
1

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

Indiction answered 14/11, 2019 at 16:36 Comment(0)
L
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.

Lands answered 24/12, 2011 at 7:47 Comment(2)
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 errorEtheline
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.Lands
R
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 [email protected] No README.md file found!
npm ERR! notsup Unsupported
npm ERR! notsup Not compatible with your operating system or architecture: mongo
[email protected]
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
Raymer answered 9/4, 2013 at 6:55 Comment(0)
B
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.

Berube answered 4/6, 2015 at 10:46 Comment(0)
N
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

Neilla answered 16/1, 2019 at 12:41 Comment(0)
D
0

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

Damian answered 22/9, 2020 at 11:11 Comment(0)
S
0

if you are using Yarn:

yarn add mongoose
Snowbound answered 9/9, 2021 at 15:42 Comment(0)
E
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

Earsplitting answered 21/10, 2022 at 13:12 Comment(0)
C
0

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

npm i --save @nestjs/mongoose mongoose
Cirrose answered 10/2, 2023 at 10:13 Comment(1)
Please do not post an answer that consists essentially of code. 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.Katheryn
T
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.

Tarah answered 28/5, 2023 at 21:31 Comment(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.Penutian
P
0

My problem was that when I checked my package.json file I saw the @nestjs/mongoose is coming from github, I removed it and added "@nestjs/mongoose": "^10.0.6", in the packages.json file and then run pnpm i and the problem got solved!

Pearle answered 17/6 at 17:32 Comment(0)
V
-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.

Valonia answered 12/5, 2013 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.