@amjs/file-resolver
v0.1.1
Published
Resolves a file path
Downloads
5
Readme
@amjs/file-resolver
Resolve the file path of a .js
file within a root or from its node_modules
context.
Installation
$ npm i --save @amjs/file-resolver
API
{String|null} resolver({{String} file, {String} ext, {String} context, {Boolean} silent})
Examples
Given the following directory:
.
├── node_modules
│ ├── module
│ │ └── file.js
├── package.json
└── src
├── file.js
└── folder
└── file.js
- Resolve
src/file.js
file:
const path = require('path');
const resolver = require('@amjs/file-resolver');
console.log(resolver({ file : path.join('src', 'file') })); // "path-to/src/file.js"
- Resolve
src/folder/file.js
file:
const path = require('path');
const resolver = require('@amjs/file-resolver');
console.log(resolver({ file : path.join('src', 'folder', 'file') })); // "path-to/src/folder/file.js"
- Resolve
node_modules/module/file.js
file:
const path = require('path');
const resolver = require('@amjs/file-resolver');
console.log(resolver({ file : 'file', ext : undefined, module : 'module' })); // "path-to/node_modules/module/file.js"
- Resolve
package.json
file:
const path = require('path');
const resolver = require('@amjs/file-resolver');
console.log(resolver({ file : 'package', ext : '.json' })); // "path-to/package.json"
- Impossible to solve a file path:
const path = require('path');
const resolver = require('@amjs/file-resolver');
console.log(resolver({ file : 'foo' }));
// [ERROR] Could not resolve: "foo.js"
// "null"
- Silent errors:
const path = require('path');
const resolver = require('@amjs/file-resolver');
console.log(resolver({ file : 'foo', silent : true })); // "null"