Cannot find module formidable - Node.js

15,951

Solution 1

To understand module resolution, have a look at the Modules documentation, especially Loading from node_modules Folders.

For example, if the file at '/home/ry/projects/foo.js' called require('bar.js'), then node would look in the following locations, in this order:

  • /home/ry/projects/node_modules/bar.js
  • /home/ry/node_modules/bar.js
  • /home/node_modules/bar.js
  • /node_modules/bar.js

NPM takes advantage of this by installing modules into:

./node_modules/{module}

So, when you use npm install formidable, it will create and install the module into:

./node_modules/formidable

But, this means that only scripts within the current directory, including sub-directories, will succeed in using require('formidable'):

./foo.js
./lib/bar.js
./src/baz.js
./src/sub/qux.js

You can however install modules as "global," but you have to explicitly ask for it with -g or --global:

npm install -g formidable

Then, any script on the system should be able to require('formidable').


As for the tree output, you current have 5 installed modules available from the current directory:

  • express
  • formidable
  • node-inspector
  • npm
  • socket.io

Everything else in the tree is a list of these modules' dependencies, and their dependencies, etc., but only these 5 are available for require(...) within your scripts.

Solution 2

The accepted answer looks very comprehensive and correct, but this worked for me:

npm install -d

d stands for dependencies (I think)

Share:
15,951
bengo
Author by

bengo

Updated on June 27, 2022

Comments