Detalhes do pacote

ncjsm

medikoo3.6mISC4.3.2

CJS (Node.js) style modules resolver

cjs, modules, bundle, browserify

readme (leia-me)

Build status Tests coverage npm version

ncjsm

(Node.js) CJS modules resolver

Environment agnostic (Node.js) CJS modules resolver.
It implements a strict version of Node.js modules resolution logic, differences are as follows:

  • Loading from global folders is not supported
  • Only Unix path separators (/) are supported in require's path arguments (Background: even though Node.js internally seems to follow Windows path separator in Windows environment, it won't work in *nix environments, and even in Window env it's not reliable so by all means should be avoided)
  • There's no awareness of node.js core modules e.g. resolve(dir, 'fs') will naturally result with null

Installation

$ npm install ncjsm

API

getResolver(extensions, confirmFile, resolvePackageMain)

For provided configuration, returns a CJS modules resolver:

  • extensions - List of supported file extensions in order of singificance, e.g. for Node.js it would be ['.js', '.json', '.node']
  • confirmFile - a confirmFile(filepath) function. Confirms whether there's a module at provided (not normalized, absolute) file path. Returns promise-like object which resolves with either normalized full path of a module or null (if there's no module for given path).
    Although result is expected to be a promise-like object, resolution can be synchronous.
  • resolvePackageMain - a resolvePackageMain(dirpath) function. Returns value of package.json's main property for given path. Returns promise-like object which resolves with either resolved value, or null, when either package.json file was not found, or it didn't have main property.
    Same as with confirmFile resolution can be synchronous.

resolve(dir, path)

Node.js resolver

Asynchronously resolves module path against provided directory path. Returns promise. If module is found, then promise resolves with an object, containing two properties:

  • targetPath - A path at which module was resolved
  • realPath - Real path of resolved module (if targetPath involves symlinks then realPath will be different)

If no matching module was found, promise is rejected with MODULE_NOT_FOUND error (unless silent: true is passed with options (passed as third argument), then it resolves with null)

const resolve = require("ncjsm/resolve");

// Asynchronously resolve path for 'foo' module against current path
resolve(__dirname, "foo").then(
  function (pathData) {
    // 'foo' module found at fooModulePath
  },
  function (error) {
    if (error.code === "MODULE_NOT_FOUND") {
      // 'foo' module doesn't exist
    }
  }
);

// `silent` option, prevents module not found rejections:
resolve(__dirname, "foo", { silent: true }).then(function (pathData) {
  if (pathData) {
    // 'foo' module found at fooModulePath
  } else {
    // 'foo' module doesn't exist
  }
});

resolveSync(dir, path)

Node.js resolver

Synchronously resolves module path against provided directory path. Otherwise works same as resolve

const resolveSync = require("ncjsm/resolve/sync");

// Synchronously resolve path for 'foo' module against current path
let fooModulePathData;
try {
  fooModulePathData = resolveSync(__dirname, "foo");
  // 'foo' module found at fooModulePath
} catch (error) {
  if (error.code === "MODULE_NOT_FOUND") {
    // 'foo' module doesn't exist
  }
}

fooModulePathData = resolveSync(__dirname, "foo", { silent: true });
if (fooModulePathData) {
  // 'foo' module found
} else {
  // 'foo' module doesn't exist
}

requireUnached([moduleIds, ]callback)

Create temporary environment where require of specific modules will not resolved the eventually cached verions

var requireUncached = require("ncjsm/require-uncached");

const firstCopyOfModule1 = require("./module1");

var secondCopyOfModule2 = requireUnached([require.resolve("./module1")], function () {
  return require("./module1");
});

console.log(firstCopyOfModule1 === secondCopyOfModule2); // false

Alternatively we may resolve callback in completely cleared require cache, for that moduleIds argument should be skipped

var requireUncached = require("ncjsm/require-uncached");

const firstCopyOfModule1 = require("./module1");

var secondCopyOfModule2 = requireUnached(function () { return require("./module1"); });

console.log(firstCopyOfModule1 === secondCopyOfModule2); // false

isPackageRoot(dirPath)

Whether provided path is a root of a package

var isPackageRoot = require("ncjsm/is-package-root");

isPackageRoot(dirPath).done(function (isRoot) {
  if (isRoot) {
    // Provided path is package root
  }
});

resolvePackageRoot(dirPath)

Resolve package root path for provided path. It is about resolution of first upper package root

var resolvePackageRoot = require("ncjsm/resolve-package-root");

resolvePackageRoot(dirPath).done(function (root) {
  if (!root) {
    // Provided path is not located in any package
  }
});

resolveProjectRoot(dirPath)

Resolve project root path for provided path. It is about resolution of topmost package root for given path

var resolveProjectRoot = require("ncjsm/resolve-project-root");

resolveProjectRoot(dirPath).done(function (root) {
  if (!root) {
    // Provided path is not located in any project
  }
});

getDependecies(modulePath, options = { ... })

Resolve all module dependencies. Returns promise that resolves with an array of paths, that includes path to input module and paths to all its dependencies (it includes deep dependencies, so also dependencies of the dependencies).

Paths to native Node.js modules are ignored.

var getDependencies = require("ncjsm/get-dependencies");

getDependencies(modulePath).done(function (deps) {
  console.log(deps); // e.g. [pathToModulePath, pathToDep1, pathToDep2, ...pathToDepn]
});
Supported options
ignoreMissing: false

If file for given module cannot be found then error is thrown. Set this to true to simply ignore not found modules

ignoreExternal: false

By default all paths to all required modules are resolved. Resolution scope may be narrowed only to modules from same package (referenced via relative path), by settung this option to true

Tests Build Status

$ npm test

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

4.3.2 (2023-01-13)

Bug Fixes

  • requireUncached: Fix modules cache recovery on global reset (c90dae4)

4.3.1 (2022-07-09)

Bug Fixes

Maintenance Improvements

4.3.0 (2022-01-24)

Features

  • Support async callback in requireUncached (91fdb82)

4.2.0 (2021-04-30)

Features

  • Require Uncached: Support clearing all require cache for callback (75c2229)

4.1.0 (2020-07-22)

Features

  • ignoreExternal option for getDependencies (158075b)

4.0.1 (2019-10-25)

Bug Fixes

  • Fix main module resolution in case pkg.main leads to broken path (331dd1c)

Tests

4.0.0 (2019-10-08)

Bug Fixes

  • Fix requireUncached moduleIds validation (eb2e026)

Features

  • By default crash resolve with MODULE_NOT_FOUND if module not found (0e446c4)

BREAKING CHANGES

  • resolve and resolve/sync now by default crash with MODULE_NOT_FOUND error, if module is not found. Old behavior was that null was returned. To maintain old behavior { silent: true } option needs to be passed

3.0.0 (2019-09-02)

Features

  • Change resolve return value format (747493e)
  • Expose realPath on resolve return object (5ad6b23)

Tests

  • Configure directory links tests (988c92a)
  • Cover symlink cases in resolve tests (c97d4a0)
  • Fix symlink type (a371ed0)
  • Improve naming (81785d6)
  • Improve var name (523498a)
  • Improve var naming (e48b5f0)
  • Maintain support for Node.js v6 (3341664)
  • Make symlink tests portable to windows (0b57b2a)
  • Refactor to async/await (6f20188)
  • Rename util (55a6d6f)
  • Skip symlink tests if not capable to create them (53cc946)

BREAKING CHANGES

  • 'resolve', and 'resolve/sync' utils now resolve with objects that expose path on targetPath property (so far path was returned directly)

2.3.0 (2019-04-30)

Features

  • improve validation in isModuleNotFoundError (8b2e641)

2.2.0 (2019-04-30)

Bug Fixes

  • Fix isModuleNotFound crash in Node.js v12 (c9b9cc8)

Features

  • Bring ES3 support to isModuleNotFoundError (e6d0132)

2.1.0 (2019-04-18)

Features

  • support ignoreMissing option in getDependencies (20baf14)

2.0.1 (2019-03-18)

Bug Fixes

  • ensure ES5 support in requireUncached (bab908d)

2.0.0 (2019-03-12)

Features

  • improve resolution of dynamic requires (fa1457b)

reafactor

BREAKING CHANGES

  • Drop support for Node.js v4 and below

1.6.0 (2019-02-07)

Features

  • rename cjs-module to ncjsm (743b95c)

1.5.0 (2018-12-28)

Features

  • improve error messaging (ca04d59)

1.4.1 (2018-12-28)

1.4.0 (2018-09-14)

Features

  • isModuleNotFoundError util (130b066)

1.3.1 (2018-07-26)

Bug Fixes

  • ensure full isolation in require-uncached (090eede)

1.3.0 (2018-05-14)

Features

1.2.2 (2017-09-18)

Bug Fixes

  • resolution of dirs referenced in main of package.json (455ce5a)

1.2.1 (2017-06-20)

Bug Fixes

  • ensure to not resolve builtin modules (1107e40)

1.2.0 (2017-06-16)

Features

  • getDependencies utility (af1c3d0)

Bug Fixes

  • ensure proper tests resolution on windows (4e4960b)

Old Changelog

See CHANGES