Node.js error Error: Cannot find module 'mongo

2020-02-08 06:00发布

问题:

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?

回答1:

trying installing using this command :

npm install mongoose

do not use the -g switch.

BTW: I ran command prompt in admin mode.

http://prazjain.wordpress.com/2012/04/24/node-js-error-error-cannot-find-module-xyz/



回答2:

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 .



回答3:

Install with the --save option:

npm install mongoose --save

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



回答4:

You have to call in the command line

npm install mongoose

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



回答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)


回答6:

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.



回答7:

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



回答8:

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

% npm link mongoose

in the project directory. This worked for me.



回答9:

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.



回答10:

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


回答11:

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.



回答12:

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



回答13:

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



回答14:

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



回答15:

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.