resolve-dependency-path
Convert a dependency path into a filepath
Last updated 6 months ago by xhmikosr .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install resolve-dependency-path 
SYNC missed versions from official npm registry.

resolve-dependency-path

CI npm version npm downloads

Convert a dependency path into a filepath

npm install resolve-dependency-path

Usage

ESM

import resolvePath from 'resolve-dependency-path';

const resolved = resolvePath({
  dependency: './foobar',
  filename: 'path/to/file/containing/dependency.js',
  directory: 'path/to/all/files'
});

CommonJS

const { default: resolvePath } = require('resolve-dependency-path');

Options

Option Type Required Description
dependency string Yes The dependency specifier (e.g. the argument to require())
filename string Yes Absolute or relative path of the file that contains the dependency
directory string Yes Root directory used to resolve non-relative dependencies

Example

If you have a file like:

myapp/foo.js

require('./bar');

Then if you want to open the file associated with the dependency, you need to resolve ./bar onto the filesystem.

Since ./bar is a relative path, it should be resolved relative to foo.js, more specifically the directory containing foo.js, myapp/. This resolution would yield myapp/bar.js.

This is why the filename attribute is required to use this library.

If you have a non-relative dependency path like:

myapp/foo.js

define([
  'bar'
], function(bar) {

});

Then bar is relative to the root of all files, myapp. The resolution would yield myapp/bar.js.

A more complex example with subdirectories:

myapp/feature1/foo.js

define([
  'feature2/bar'
], function(bar) {

});

The dependency feature2/bar is relative to the root of all files, myapp, not the file foo.js.

This is why the directory attribute is required to use this library.

License

MIT

Current Tags

  • 5.0.0                                ...           latest (6 months ago)

9 Versions

  • 5.0.0                                ...           6 months ago
  • 4.0.1                                ...           2 years ago
  • 4.0.0                                ...           3 years ago
  • 3.0.2                                ...           4 years ago
  • 3.0.1                                ...           4 years ago
  • 3.0.0                                ...           4 years ago
  • 2.0.0                                ...           8 years ago
  • 1.0.2                                ...           12 years ago
  • 1.0.1                                ...           12 years ago
Downloads
Total 28
Today 0
This Week 0
This Month 13
Last Day 0
Last Week 4
Last Month 2
Dependencies (0)
None
Dev Dependencies (3)

© 2010 - cnpmjs.org x YWFE | Home | YWFE