user1725316
user1725316

Reputation:

NPM Installs Package Outside Current Directory

I try to install express package using npm from inside /home/iwan/my-project directory:

npm install express

[email protected] ../node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
└── [email protected] ([email protected])

The strange thing is npm doesn't install express package in current dir (/home/iwan/my-project/node_modules/express), but in /home/iwan/node_modules/express.

Did i miss something?

Upvotes: 20

Views: 11691

Answers (3)

Ij888
Ij888

Reputation: 60

You could also create a blank package.json file using guidelines from the https://docs.npmjs.com/files/package.json webpage. Then place this in your project folder and type npm install.

Upvotes: 1

Nafiul Islam
Nafiul Islam

Reputation: 82590

I believe the best way to install packages with npm is to make a package.json file. Like this, just put it in the smae directory as your app. A sample package.json file could look like this:

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.3.5",
    "jade": "*",
    "less-middleware": "*",
    "ejs": "*",
    "mongoose": "3.6.*"
  }
}

Take a look at the dependencies list. Just add the module that you want, for example, underscore. Just add it to the dependencies dict. Like so:

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.3.5",
    "jade": "*",
    "less-middleware": "*",
    "ejs": "*",
    "mongoose": "3.6.*",
    "underscore": "*"   <-------------- Added
  }
}

Then head over to your directory and just run npm install, and bam! All the packages and their dependencies will be installed for you. It will do all the work, and that means making your node_modules folder for you. This is how my app directory looks like:

enter image description here

Upvotes: 1

Brad
Brad

Reputation: 163603

If the node_modules directory doesn't exist in your current directory, NPM will look for it in the higher directories until it finds it. So, if the parent directory has a node_modules directory, NPM will assume that's where it is to install modules.

A quick way around this is to create an empty node_modules directory where you want the modules to be placed.

Upvotes: 51

Related Questions