I have a directory that looks like this:
-- app/ |- models/ |- user.js |- config.json
I want my user.js
file to require config.json
. Right now I'm using require('/config')
but this is not working. What am I doing wrong? I'd like to avoid using require('../config')
.
The __filename variable returns the absolute path of the current module. So, if you use the __filename variable in a module located at /home/user/my-module. js , the __filename variable would return /home/user/my-module. js .
You can think of the require module as the command and the module module as the organizer of all required modules. Requiring a module in Node isn't that complicated of a concept. const config = require('/path/to/file'); The main object exported by the require module is a function (as used in the above example).
There are two basic choices when it comes to selection of URLs, absolute URL and relative URL. An absolute URL points to a very specific location on the Web and contains all the necessary information to locate a resource. Absolute URLs must be used to link to other websites that are not located on the same domain.
It gives the current working directory of the Node. js process. __dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.
The simple answer is that you aren't doing anything wrong. Per a little research, the require function looks for one of:
See: http://www.bennadel.com/blog/2169-Where-Does-Node-js-And-Require-Look-For-Modules-.htm And: http://nodejs.org/api/modules.html#loading_from_node_modules_Folders
Both resources above reference the ability to also modify a NODE_PATH environment variable, however this sounds like very bad practice and at a minimum would make your code way less portable. It also probably wouldn't even work for a file like config.json since, though I'd never done it, I'd imagine changing the NODE_PATH variable only changes where require() looks for package.json files corresponding to real modules.
In summary, see: How to make the require in node.js to be always relative to the root folder of the project? as referenced above by Piotr Kowalczuk. Per that post, you have two real options:
Finally, just bear in mind that what you are trying to do goes against the grain of the program that you are using. I think this is one case where you will ultimately make your life easier (and your collaborators'!) by going with the grain of Node and using relative file paths as the design intends.
I couldn't find any better solution than to write my own module.
After testing it in some projects I have decided to make it open source.
const user = require('../../../database/user'); // 👎 what you have // OR const user = require('$db/user'); // 👍 no matter how deep you are const product = require('/database/product'); // 👍 alias or pathing from root directory
Three simple steps to use it.
Install the package: npm install sexy-require --save
Include require('sexy-require')
once on the top of your main application file.
require('sexy-require'); const routers = require('/routers'); const api = require('$api'); ...
Optional step. Path configuration can be defined in .paths
file on root directory of your project.
$db = /server/database $api-v1 = /server/api/legacy $api-v2 = /server/api/v2
Anywhere in your project you can get the defined shortcut paths:
const path = require(`sexy-require`); console.log(path.$db); // -> '/full/path/to/app/server/database'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With