I wanna create a node package modules, but I have difficulty to require a file from root project directory to use inside my node package module I created.
If I have directory structure like this
- node_modules
- library_name
- lib
- index.js
- bin
- run.sh
- config.js
If the run.sh called, it will run index.js. Inside index.js, how do I resolve to root directory which later I can require config.js inside index.js?
I wanna create a node package modules, but I have difficulty to require a file from root project directory to use inside my node package module I created.
If I have directory structure like this
- node_modules
- library_name
- lib
- index.js
- bin
- run.sh
- config.js
If the run.sh called, it will run index.js. Inside index.js, how do I resolve to root directory which later I can require config.js inside index.js?
Share Improve this question asked Jun 17, 2018 at 2:32 Jefry DewanggaJefry Dewangga 8491 gold badge11 silver badges24 bronze badges 2- Why do you need to call an outer script from node_modules folder? – JulianSoto Commented Jun 17, 2018 at 3:53
- 2 @JulianSoto Actually I wanna create node package that require some config from root project, it infer the config from nuxt.config.js – Jefry Dewangga Commented Jun 17, 2018 at 15:12
3 Answers
Reset to default 5Package binary can accept configuration path explicitly as an argument.
If package binary doesn't run as NPM script, it shouldn't rely on parent project structure.
If package binary runs via NPM script:
"scripts": {
"foo": "library_name"
}
This will set current working directory to project root, so it could be required as:
const config = require(path.join(process.cwd(), 'config'));
Both approaches can be bined; this is often used to provide configuration files with default locations to third-party CLI (Mocha, etc).
If you're in index.js
and config.js
is in the directory above node_modules
in your diagram, then you can build a path to config.js
like this:
const path = require('path');
let configFilename = path.join(__dirname, "../../../", "config.js");
__dirname
is the directory that index.js
is in.
The first ../
takes you up to the library_name
directory.
The second ../
takes you up to the node_modules
directory.
The third ../
takes you up to the parent of node_modules
(what you call project root) where config.js
appears to be.
If you really want your module to be independent of how it is installed or how NPM might change in the future, then you need to somehow pass in the location of the config file in any number of ways:
- By making sure the current working directory is set to the project root so you can use
process.cwd()
to get access to the config file. - By setting an environment variable to the root directory when starting your project.
- By passing the root directory in to a module constructor function.
- By loading and passing the config object itself in to a module constructor function.
I create module same your module.
And I call const config = require('../config')
, it work.