Steve
Steve

Reputation: 31652

Specify node.js named-module path

I'm new to node.js - so I think my question is best asked with an example:

The Connect framework requires the mime module - and loads is as such: require('mime')

If I wanted to make this a local module (i.e. I've placed the mime module within my project instead of installing it into node.js's Core Modules folder) is there some way for me to associate that module name with my known path to that module?

Upvotes: 0

Views: 904

Answers (1)

Wes Johnson
Wes Johnson

Reputation: 3101

You can reference a module three ways:

  • using a relative path, ie: mime = require('./lib/mime.js');
  • using an absolute path, ie: mime = require('/home/usr/www/lib/mime.js');
  • using a search: ie: mime = require('mime');

For the search method, Node starts in your app's directory, and adds ./node_modules/ and tries to load from that location. If that fails it moves to the parent directory and so on until it hits the root.

If you're in your app's directory and install a module (like mime) via NPM, it'll install it in that node_modules sub-directory by default.

Upvotes: 2

Related Questions