Shrout1 Shrout1 - 1 month ago 6
Node.js Question

How does node.js know express.js location

How does my node.js server know how to find the express.js file?

In my server.js file I have a requirement for

express
:

var express = require('express');
var app = express();


My server.js file is in the
app
folder while express.js is in the
app\node_modules\express\lib
folder:

Server.js File

express.js is here in the
lib
directory

Express.js File

Does node.js automatically know to look in
node_modules
for dependencies? There is no direct enumeration of the path anywhere - I don't see it in the code.

This is a sample project from scotch.io and I'm trying to pick it apart and learn. I'm pretty new to the MEAN stack and I'm trying to understand this at a basic level.

FYI this is node v 4.5.0

Answer

The comments already cover the main answer, but I'll try to break it out here for a slightly more complete overview of the subject (not all encompassing). The short version is that node looks in your project's node_modules path for modules (packages, but we generally require modules) matching the name. 1

Package Installation, Saving, and Location

Node uses npm to install dependencies, which can be either a "dependency" or "devDependency"; the latter is used for development concerns that shouldn't be required for just normally using the module. We save these to our project's package.json using the flag --save or --save-dev (such as npm install express --save). 2

The package.json file is what sits at the root of your project tree (the project's folder/directory) and contains the dependency information, along with other information. 3 This is defines a "package". When a person publishes a package to npmjs, the (default) registry for packages to install via npm, they should include a well formed package.json which lists its own dependencies, files to include, and what is the "main" file to start with, should it be used in a require statement.

The dependencies you install by running npm install after cloning down the project's repository, will install the packages specified in the package.json, into the node_modules path in the root of your project (where you should be running the install command from).

Side Note

After checking the GitHub repo listed by the article you referenced, you appear to have created each of the files inside the app/ directory.

Require-ing

The use of a require statement in node is in the style of CommonJS and for node, looks first in the node_modules/ path or, if you specify a relative path to a folder or file, you can get it directly. Path relative means that it starts with ./ for a prefix to the current working directory (that the file is executing from), ../ for the directory above the current one, etc. There are other ways of handling paths, such as as the path module that is built into node 4. For example, the following are valid:

  • require('./some-other.js') require in that file's module.exports, from the some-other.js file 5, in the current, relative path
  • require('./some.json') will bring in JSON formatted content from a .json file 6, in the current, relative path
  • require('./routes') will/can also bring in exported content from the routes/ path (directory), which will by default start with the directory's index.js, should it exist 7

That last method is a nice way to be able to bring in a more complex requirement, without keeping everything in a single, overly busy file.

Comments