Kuan
Kuan

Reputation: 11389

How to know what path to specify in Node for require

All:

When I use Express.js(but I believe this is more of a Node question), if I want to import some modules, I need to use require(module path).

But I am a little confused what root path should be used for each require, are they all same to the path we execute the node command?

Sometime, It works when I use require("../modelname") while sometimes require("./modelname") even I did not change the modelname.js location.

Thanks

Upvotes: 3

Views: 91

Answers (4)

Arnav Joshi
Arnav Joshi

Reputation: 96

use webstorm tool where you have debugging facility like we have in eclipse, start your application in debug mode and put breakpoints.

Upvotes: 0

Arnav Joshi
Arnav Joshi

Reputation: 96

If you are in a directory where you want to access modelname there are two scenarios given below

  1. if modelname is in the same directory then you can use require("./modelname")

  2. if calling directory is different and one level above to your modelname directory then you have to use require("../modelname").

Upvotes: 2

jmartins
jmartins

Reputation: 991

Node documentation provides a pseudo-code algorithm for how it resolves the path for require()

To sum up:

require(X) from module at path Y
If X begins with './' or '/' or '../'
   a. LOAD_AS_FILE(Y + X)
   b. LOAD_AS_DIRECTORY(Y + X)

LOAD_AS_FILE(X)
1. If X is a file, load X as JavaScript text.  STOP
2. If X.js is a file, load X.js as JavaScript text.  STOP
3. If X.json is a file, parse X.json to a JavaScript Object.  STOP
4. If X.node is a file, load X.node as binary addon.  STOP

LOAD_AS_DIRECTORY(X)
1. If X/package.json is a file,
   a. Parse X/package.json, and look for "main" field.
   b. let M = X + (json main field)
   c. LOAD_AS_FILE(M)
2. If X/index.js is a file, load X/index.js as JavaScript text.  STOP
3. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
4. If X/index.node is a file, load X/index.node as binary addon.  STOP

Upvotes: 3

JoshWillik
JoshWillik

Reputation: 2645

When you do require( 'express' ), node will begin in your current folder, and step up the file tree until it locates a folder that has a package.json inside it.

Once it finds this file, it will check for a node_modules folder and look for node_modules/express. It will then follow the instructions in node_modules/express/package.json as to how to go about including the module.

If you want to require your own code that lacks a package.json, you'd do something like this.

require( './router' )

or

require( '../middleware/site-data' )

In the first example, the . refers to the folder the file doing the import is in, and will import either ./router.js or ./router/index.js, depending on which exists. In require, . is effectively translated into __dirname + '/'.

In the second example, the .. refers to the parent of the directory the importing file is in. In require, .. is effectively translated into __dirname + '/../'.

Good luck!

Upvotes: 1

Related Questions