Pickels
Pickels

Reputation: 34630

Is there a way to include a directory when using CommonJS modules and require?

I have the following structure

|-src
|--test.js
|-spec
|--test_spec.js

I was wondering is there a way to include the src directory to the require path? I am trying to do the following in test_spec.js

require('test');

Instead of doing:

require('../src/test.js');

Upvotes: 1

Views: 681

Answers (2)

fent
fent

Reputation: 18205

If you have a package.json file you can require the directory where the file is and it will require the "main" file specified in it.

{
  "main": "src/text.js"
}

Upvotes: 1

Chandra Sekar
Chandra Sekar

Reputation: 10843

You need to add src to the NODE_PATH environment variable.

export NODE_PATH=/path/to/src

Upvotes: 1

Related Questions