1.22.8 • Published 6 months ago

resolve v1.22.8

Weekly downloads
40,435,466
License
MIT
Repository
github
Last release
6 months ago

resolve

implements the node require.resolve() algorithm such that you can require.resolve() on behalf of a file asynchronously and synchronously

build status

example

asynchronously resolve:

var resolve = require('resolve');
resolve('tap', { basedir: __dirname }, function (err, res) {
    if (err) console.error(err);
    else console.log(res);
});
$ node example/async.js
/home/substack/projects/node-resolve/node_modules/tap/lib/main.js

synchronously resolve:

var resolve = require('resolve');
var res = resolve.sync('tap', { basedir: __dirname });
console.log(res);
$ node example/sync.js
/home/substack/projects/node-resolve/node_modules/tap/lib/main.js

methods

var resolve = require('resolve');

resolve(id, opts={}, cb)

Asynchronously resolve the module path string id into cb(err, res [, pkg]), where pkg (if defined) is the data from package.json.

options are:

  • opts.basedir - directory to begin resolving from

  • opts.package - package.json data applicable to the module being loaded

  • opts.extensions - array of file extensions to search in order

  • opts.readFile - how to read files asynchronously

  • opts.isFile - function to asynchronously test whether a file exists

  • opts.isDirectory - function to asynchronously test whether a directory exists

  • opts.packageFilter(pkg, pkgfile, dir) - transform the parsed package.json contents before looking at the "main" field

    • pkg - package data
    • pkgfile - path to package.json
    • dir - directory for package.json
  • opts.pathFilter(pkg, path, relativePath) - transform a path within a package

    • pkg - package data
    • path - the path being resolved
    • relativePath - the path relative from the package.json location
    • returns - a relative path that will be joined from the package.json location
  • opts.paths - require.paths array to use if nothing is found on the normal node_modules recursive walk (probably don't use this)

    For advanced users, paths can also be a opts.paths(request, start, opts) function

    • request - the import specifier being resolved
    • start - lookup path
    • getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard node_modules resolution
    • opts - the resolution options
  • opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: "node_modules"

  • opts.preserveSymlinks - if true, doesn't resolve basedir to real path before resolving. This is the way Node resolves dependencies when executed with the --preserve-symlinks flag. Note: this property is currently true by default but it will be changed to false in the next major version because Node's resolution algorithm does not preserve symlinks by default.

default opts values:

{
    paths: [],
    basedir: __dirname,
    extensions: ['.js'],
    readFile: fs.readFile,
    isFile: function isFile(file, cb) {
        fs.stat(file, function (err, stat) {
            if (!err) {
                return cb(null, stat.isFile() || stat.isFIFO());
            }
            if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
            return cb(err);
        });
    },
    isDirectory: function isDirectory(dir, cb) {
        fs.stat(dir, function (err, stat) {
            if (!err) {
                return cb(null, stat.isDirectory());
            }
            if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
            return cb(err);
        });
    },
    moduleDirectory: 'node_modules',
    preserveSymlinks: true
}

resolve.sync(id, opts)

Synchronously resolve the module path string id, returning the result and throwing an error when id can't be resolved.

options are:

  • opts.basedir - directory to begin resolving from

  • opts.extensions - array of file extensions to search in order

  • opts.readFile - how to read files synchronously

  • opts.isFile - function to synchronously test whether a file exists

  • opts.isDirectory - function to synchronously test whether a directory exists

  • opts.packageFilter(pkg, dir) - transform the parsed package.json contents before looking at the "main" field

    • pkg - package data
    • dir - directory for package.json (Note: the second argument will change to "pkgfile" in v2)
  • opts.pathFilter(pkg, path, relativePath) - transform a path within a package

    • pkg - package data
    • path - the path being resolved
    • relativePath - the path relative from the package.json location
    • returns - a relative path that will be joined from the package.json location
  • opts.paths - require.paths array to use if nothing is found on the normal node_modules recursive walk (probably don't use this)

  • opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: "node_modules"

  • opts.preserveSymlinks - if true, doesn't resolve basedir to real path before resolving. This is the way Node resolves dependencies when executed with the --preserve-symlinks flag. Note: this property is currently true by default but it will be changed to false in the next major version because Node's resolution algorithm does not preserve symlinks by default.

default opts values:

{
    paths: [],
    basedir: __dirname,
    extensions: ['.js'],
    readFileSync: fs.readFileSync,
    isFile: function isFile(file) {
        try {
            var stat = fs.statSync(file);
        } catch (e) {
            if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
            throw e;
        }
        return stat.isFile() || stat.isFIFO();
    },
    isDirectory: function isDirectory(dir) {
        try {
            var stat = fs.statSync(dir);
        } catch (e) {
            if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
            throw e;
        }
        return stat.isDirectory();
    },
    moduleDirectory: 'node_modules',
    preserveSymlinks: true
}

resolve.isCore(pkg)

Return whether a package is in core.

install

With npm do:

npm install resolve

license

MIT

jest-resolveeslint-import-resolver-nodeeslint-plugin-reactrechoirbabel-eslintbrowser-resolvebabel-plugin-macrospostcss-importtslinteslint-plugin-nodeliftoffreact-docgen@babel/helper-define-polyfill-providerreact-scriptsmatchdeprequire-in-the-middle@angular/clipug-filtersbrowserifybabel-plugin-module-resolverbrfsweb-desktop-helperxmi-scriptsfree-minelcia-novaui@bsolution/dev@bsolution/fast@bsolution/scripts@mace/nx-cli@proteria/react-scripts@prisma/internals@slp/nx-cli@prisma/sdk@huyhpham/rn-line@uukit/react-dev-kit@uukit/scripts@savaleukhin/react-scripts@scentregroup/react-scripts@procore/core-scripts@talesman/react-scriptsarchetype-librarycarrier_anal@gd-uikit/react-scripts@demo./cli-utils@demo./yo@firere/roblox-tscbbpa-clientcc-gram-democordova-fetchcomponennentt@teambit/legacy@dinert/utils@knapsack/file-utilseasy-select-rn@sooha/clireact-wind-ui@g7fe/g7-scriptschinjowwchinjowsmart-input-demovuedragdropuploadimagesesnext-web-modulesbyteplayerkstick-react-scriptsbabel-plugin-extension-resolver-web@joelcox22/boilerplate@dmytrohusiev/fork-babel-plugin-inline-react-svgreact-native-bluetooth2listner-resize-pagekilli8n-react-native-fast-imagewatsize-test-react-scripts@psimk/react-scriptsglobal-martcomatch-mvp-debugging@darapti/darapti-react-scriptsswitch_mostqroda-sdkqroda-sdk-v1pipihomemetaliquid-react-scriptsrockywu-ui@tomas2d/react-scriptsjbojcic-react-scriptsupnext-apps-reader@darrinholst/cucumber@jvhellemondt/custom-react-scripts@st4rfox/electron-scriptssv-component-librarysv-component-library-1sv-component-library-2@thuanpq/airvert-ui@xinyifly/react-scripts@isi-gach/babel-plugin-inline-react-svg@ipavinthan/react-scriptsmio-cli-servicebabel-plugin-inline-react-svg-folk@katalabut/react-scriptsandculturecode-react-scriptswebeetle-react-scriptsyadong-react
1.22.7

6 months ago

1.22.8

6 months ago

2.0.0-next.5

6 months ago

1.22.4

9 months ago

1.22.5

7 months ago

1.22.6

7 months ago

1.22.3

1 year ago

1.22.2

1 year ago

2.0.0-next.4

2 years ago

1.22.1

2 years ago

1.21.0

2 years ago

1.21.1

2 years ago

1.22.0

2 years ago

1.20.0

3 years ago

2.0.0-next.3

3 years ago

1.19.0

3 years ago

2.0.0-next.2

3 years ago

1.18.1

4 years ago

1.18.0

4 years ago

1.17.0

4 years ago

1.16.1

4 years ago

1.16.0

4 years ago

1.15.1

4 years ago

2.0.0-next.1

4 years ago

1.15.0

4 years ago

1.14.2

4 years ago

2.0.0-next.0

4 years ago

1.14.1

4 years ago

1.14.0

4 years ago

1.12.3

4 years ago

1.13.1

4 years ago

1.13.0

4 years ago

1.12.2

4 years ago

1.12.1

4 years ago

1.12.0

5 years ago

1.11.1

5 years ago

1.11.0

5 years ago

1.10.1

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.1

6 years ago

1.8.0

6 years ago

1.7.1

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.2.1

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.7

8 years ago

1.1.6

9 years ago

1.1.5

9 years ago

1.1.4

9 years ago

1.1.3

9 years ago

1.1.2

9 years ago

1.1.0

9 years ago

1.0.0

10 years ago

0.7.4

10 years ago

0.7.3

10 years ago

0.7.2

10 years ago

0.7.1

10 years ago

0.7.0

10 years ago

0.6.3

10 years ago

0.6.2

10 years ago

0.6.1

10 years ago

0.6.0

10 years ago

0.5.1

11 years ago

0.5.0

11 years ago

0.4.3

11 years ago

0.4.2

11 years ago

0.4.1

11 years ago

0.4.0

11 years ago

0.3.1

11 years ago

0.3.0

11 years ago

0.2.8

11 years ago

0.2.7

11 years ago

0.2.6

11 years ago

0.2.5

11 years ago

0.2.4

11 years ago

0.2.3

12 years ago

0.2.2

12 years ago

0.2.1

12 years ago

0.2.0

12 years ago

0.1.3

12 years ago

0.1.2

12 years ago

0.1.0

13 years ago

0.0.4

13 years ago

0.0.3

13 years ago

0.0.2

13 years ago

0.0.1

13 years ago

0.0.0

13 years ago