93

I am trying to do a homework for a mongodb uni course. They gave us some files, instructions are:

run npm install mongodb then node app.js

for some reason npm install does not create a node_modules directory but I don't see any build errors:

mongo-uni/hw1-2$ npm install mongodb
npm WARN package.json [email protected] path is also the name of a node core module.
npm http GET https://registry.npmjs.org/mongodb
npm http 304 https://registry.npmjs.org/mongodb
npm http GET https://registry.npmjs.org/bson/0.2.5
npm http GET https://registry.npmjs.org/kerberos/0.0.3
npm http 304 https://registry.npmjs.org/kerberos/0.0.3
npm http 304 https://registry.npmjs.org/bson/0.2.5

> [email protected] install /home/jasonshark/node_modules/mongodb/node_modules/kerberos
> (node-gyp rebuild 2> builderror.log) || (exit 0)

make: Entering directory `/home/jasonshark/node_modules/mongodb/node_modules/kerberos/build'
  SOLINK_MODULE(target) Release/obj.target/kerberos.node
  SOLINK_MODULE(target) Release/obj.target/kerberos.node: Finished
  COPY Release/kerberos.node
make: Leaving directory `/home/jasonshark/node_modules/mongodb/node_modules/kerberos/build'

> [email protected] install /home/jasonshark/node_modules/mongodb/node_modules/bson
> (node-gyp rebuild 2> builderror.log) || (exit 0)

make: Entering directory `/home/jasonshark/node_modules/mongodb/node_modules/bson/build'
  CXX(target) Release/obj.target/bson/ext/bson.o
make: Leaving directory `/home/jasonshark/node_modules/mongodb/node_modules/bson/build'
[email protected] ../../../node_modules/mongodb
├── [email protected]
└── [email protected]
mongo-uni/hw1-2$ node app.js
Failed to load c++ bson extension, using pure JS version
'No document found'
4
  • From the npm log it looks like this is using the directory /home/jasonshark/node_modules/. Can you try an ls /home/jasonshark/node_modules/ to see if that path exists? The message you are seeing about "bson extension" is a warning that you are using the less-performant JS module (instead of the C++ module). The "no document found" message is coming from your app.js, which may actually be running but not finding any documents yet.
    – Stennie
    Commented Jan 21, 2014 at 6:41
  • I'm also accessing the internet behind a proxy server, which might affect the downloading.. Commented Jan 21, 2014 at 6:43
  • 1
    I don't think downloading modules is the issue: the C++ bson module likely wasn't installed because you don't have a compiler properly setup in your path, but the pure JS module should be fine for dev. I would look at the app.js code to see what it is expecting. I suspect the app is running fine :)
    – Stennie
    Commented Jan 21, 2014 at 6:50
  • Did you figure this out? I'm having the same problem. I've tried each of the answers below but am not getting a node modules folder.
    – Mel
    Commented Jun 6, 2017 at 23:52

14 Answers 14

106

npm init

It is all you need. It will create the package.json file on the fly for you.

5
  • 2
    In the same folder of your project (where you want to create the node_modules folder).
    – Dror Bar
    Commented Dec 17, 2018 at 15:54
  • 1
    Thanks, where does an npm install install to if the package.json is not present? I wonder if there should be a message displayed by npm saying there wasn't a package.json file found and that one should run npm init. Commented Aug 3, 2021 at 0:16
  • 1
    For future reference, when doing this in visual studio: it took me longer than I'd like to admit that I didn't see the node_modules folder in the solution explorer as it wasn't included in the project automatically. But the folder was actually there!
    – Tom Wuyts
    Commented Aug 16, 2021 at 18:38
  • 1
    Same as @TomWuyts... Please anyone using VS Code, refresh the file explorer with the icon in the top right when hovering over the file explorer side menu... Saved me some time and sanity
    – Lushawn
    Commented Dec 20, 2022 at 10:31
  • Thanks a lot. It help me to solve my problem.
    – Wyatt
    Commented Nov 10, 2023 at 9:12
47

NPM has created a node_modules directory at '/home/jasonshark/' path.

From your question it looks like you wanted node_modules to be created in the current directory.

For that,

  1. Create project directory: mkdir <project-name>
  2. Switch to: cd <project-name>
  3. Do: npm init This will create package.json file at current path
  4. Open package.json & fill it something like below

    {
        "name": "project-name",
        "version": "project-version",
        "dependencies": {
            "mongodb": "*"
        }
    }
    
  5. Now do : npm install OR npm update

Now it will create node_modules directory under folder 'project-name' you created.

2
  • Installation directory switches in build process & was missing npm init so that it choose current directory for installation. Better explained then accepted answer. Commented Dec 7, 2017 at 9:27
  • 2
    I would specify here that what makes this particular package.json generate a node_modules directory is the presence of the dependecies property, with something inside it. Commented Jun 19, 2019 at 2:10
25

If you have a package-lock.json file, you may have to delete that file then run npm i. That worked for me

0
14

See @Cesco's answer: npm init is really all you need


I was having the same issue - running npm install somePackage was not generating a node_modules dir.

I created a package.json file at the root, which contained a simple JSON obj:

{
    "name": "please-work"
}

On the next npm install the node_modules directory appeared.

2
  • 3
    Before anyone starts to flame, this is a feature, not a bug. Commented Jan 30, 2015 at 21:19
  • 2
    actually, I don't want an package.json file if I am installing top-level package not used by anything. In that case, it appears that all I need to do before npm install is "mkdir node_modules"
    – Michael
    Commented Nov 22, 2017 at 23:41
4

As soon as you have run npm init and you start installing npm packages it'll create the node_moduals folder after that first install

e.g

npm init

(Asks you to set up your package.json file)

npm install <package name here> --save-dev

installs package & creates the node modules directory

4

cd into the directory and then run

npm install node
3

I just fixed it!

first be sure to be in your project folder, then run:

1. npm init
2. npm install --save   
3. Install any module.

This is the result:

enter image description here

2

A few things to try that worked for me:

  1. Try running the NPM install from outside of your IDE, just from the bash / PowerShell / cmd prompt. It will work the same, and might complete successfully.

  2. Try upgrading NPM:

npm install -g npm

or, to stay within a specific major version:

npm install -g @latest-7

THEN run your npm install again.

0

I ran into this trying to integrate React Native into an existing swift project using cocoapods. The FB docs (at time of writing) did not specify that npm install react-native wouldn't work without first having a package.json file. Per the RN docs set your entry point: (index.js) as index.ios.js

0

my problem was to copy the whole source files contains .idea directory and my webstorm terminal commands were run on the original directory of the source
I delete the .idea directory and it worked fine

0

For me, I had to go into the directory where the package.json is itself and then run npm install in order to see node_modules folder. Apparently, running npm install in a directory that has NO package.json does not error or tell you otherwise.

0

This answer is for users like my students who named their windows account name with unicode characters (other than a-z) :) They placed their project folder on their desktop folder.

Please make sure that your project folder path doesn't include special characters such as "ÇŞİ " etc..

In our case, moving folder to C:\ fixed the problem.

0

I had a similar problem to this. It turns out that this can be caused by a number of things. The normal workflow is this:

npm init generates a package.json file

Next

npm install <package name> generates node_modules folder

In my case, the problem was because the configuration of npm in my .npmrc file was wrong. I was essentially requesting packages from the wrong URL. As of now, the public npm registry URL is

https://registry.npmjs.org/

This may be the problem in your project. So locate the .npmrc file on your system and ensure its pointing to the right URL.

-3

For node_modules you have to follow the below steps

1) In Command prompt -> Goto your project directory.

2) Command :npm init

3) It asks you to set up your package.json file

4) Command: npm install or npm update

1
  • 8
    NEVER run install commands for ANY language package manager as sudo. NEVER change directory permissions to try to fix it
    – Andy Ray
    Commented Aug 15, 2018 at 18:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.