Import the closest file as a node module with loader.
Say you're writing a CLI tool. You may want the following features:
- Get the closest configure file
- Automatically register module loader according to extension name
import-file
manages these for you.
export default {
name: 'awesome',
};
const importFile = require('import-file');
const config = importFile('my_config.babel.js');
console.log(config.name);
$ cd /project/src && node index
# => `awesome`
Import a file as a node module. Return a node module if the file exists, otherwise throw an error.
filepath
(String): Target file path. If not provide a extension name, it will automatically add extension and register module loader. For more detail of extensions and modules, please read interpret.options
(Object): These are all the available options.cwd
(String): The directory resolves from. Defaults toprocess.cwd()
useLoader
(Boolean): Enable automatically register module loader according to the adding extension name. Defaults totrue
useFindUp
(Boolean): Find by walking up parent directories. Defaults totrue
useCache
(Boolean): Cache imported file. The same behaviour with node.jsrequire
. Default totrue
.useESDefault
(Boolean): Exportdefault
in ES module. Default totrue
.resolvers
([String]): TellimportFile
what directories should be searched when resolving filesexts
([String]): Extensions white list for loader. Only work ifuseLoader
istrue
. Defaults to all interpret extensions
The same with importFile()
, but just return the resolved filename. Throw an error if the file is not exists.
$ npm install import-file
MIT