Crunchyroll
Crunchyroll

Reputation: 33

Why does NodeJS require me to use the full directory for downloaded modules?

For something like Express for example, which does not come with Node by default. I have to use var express = require('C:/Users/User/node_modules/express'); instead of just var express = require('express');. I notice the modules which come by default such as http aren't in the same location as the ones I install. So what do I need to do in order to not have to write the whole directory. If it makes any difference I keep all my Node projects in C:/Node/, not the default one.

Upvotes: 0

Views: 39

Answers (1)

shyam padia
shyam padia

Reputation: 406

This is happening because you probably don't have node modules installed locally. For that you need a package.json file which you can get by running

npm init

This will ask you some questions about your project and will set up node locally. A package.json file will be created which should look something like this (without dependencies).

{
  "name": "express-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "Link to your repository"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^1.0.2",
    "body-parser": "^1.17.2",
    "chalk": "^2.0.1",
    "compression": "^1.7.0",
    "cookie-parser": "^1.4.3",
    "cors": "^2.8.4",
    "express": "^4.15.3",
    "glob": "^7.1.2",
    "moment": "^2.18.1",
    "mongoose": "^4.11.3",
    "morgan": "^1.8.2",
    "passport": "^0.3.2",
    "path": "^0.12.7",
    "yargs": "^8.0.2"
  }
}

You can then add the node modules you want by putting them in dependencies and running

npm install

If you want to add node modules from commond line you can use

npm install package-name --save

Upvotes: 2

Related Questions