How to use optional-require - 1 common examples

To help you get started, we’ve selected a few optional-require examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github electrode-io / electrode / packages / electrode-archetype-react-app-dev / lib / node-resolver.js View on Github external
function resolve(req, atPath) {
  // can only resolve modules under node_modules
  if (!isModuleRequest(req)) return undefined;

  const splits = req.split("/");
  const nameX = splits[0].startsWith("@") ? 2 : 1; // check for scoped module
  const name = splits.slice(0, nameX).join("/");

  //
  // All modules must have package.json
  //
  const resolved = optionalRequire.resolve(
    requireAt(atPath),
    // ensure require request paths are POSIX
    Path.posix.join(name, "package.json"),
    false
  );

  if (!resolved) return undefined;

  splits.splice(0, nameX, ".");

  return {
    path: Path.dirname(resolved),
    request: splits.join("/")
  };
}

optional-require

NodeJS Require that let you handle module not found error without try/catch

Apache-2.0
Latest version published 3 years ago

Package Health Score

62 / 100
Full package analysis

Popular optional-require functions