Nick Goad
Nick Goad

Reputation: 431

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?

Upvotes: 43

Views: 135126

Answers (21)

haghighi251
haghighi251

Reputation: 153

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!

Upvotes: 0

Mahmoud Hany
Mahmoud Hany

Reputation: 3

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.

Upvotes: 0

Amirali Eshghi
Amirali Eshghi

Reputation: 1011

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

npm i --save @nestjs/mongoose mongoose

Upvotes: 0

Krishnadev. V
Krishnadev. V

Reputation: 554

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

Upvotes: 0

7laria
7laria

Reputation: 44

if you are using Yarn:

yarn add mongoose

Upvotes: 0

try installing mongoose using this command:

npm install mongoose

do not use the -g switch.

BTW: I ran command prompt in admin mode. Reference

Upvotes: 58

Chandrakant shinde
Chandrakant shinde

Reputation: 101

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

Upvotes: 0

Almaju
Almaju

Reputation: 1383

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

Upvotes: 2

Артур Гудиев
Артур Гудиев

Reputation: 1144

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

Upvotes: 1

Kamlesh
Kamlesh

Reputation: 6135

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

Upvotes: 0

Gaurav Paliwal
Gaurav Paliwal

Reputation: 1596

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])

Upvotes: 5

satyam kumar
satyam kumar

Reputation: 1487

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 .

Upvotes: 20

hhzhu
hhzhu

Reputation: 609

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

% npm link mongoose

in the project directory. This worked for me.

Upvotes: 1

Tim Scott
Tim Scott

Reputation: 15205

Install with the --save option:

npm install mongoose --save

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

Upvotes: 9

Devs love ZenUML
Devs love ZenUML

Reputation: 11842

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.

Upvotes: 0

user832147
user832147

Reputation: 11

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

Upvotes: 1

Angelo Nodari
Angelo Nodari

Reputation: 385

You have to call in the command line

npm install mongoose

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

Upvotes: 6

jsDebugger
jsDebugger

Reputation: 122

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.

Upvotes: -1

Nikhil Ranjan
Nikhil Ranjan

Reputation: 992

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

Upvotes: 0

SheshPai
SheshPai

Reputation: 319

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.

Upvotes: 2

Paul
Paul

Reputation: 36319

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.

Upvotes: 0

Related Questions