How to use the optional-require.resolve function in optional-require

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(
    // ensure require request paths are POSIX
    Path.posix.join(name, "package.json"),

  if (!resolved) return undefined;

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

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


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

Latest version published 3 years ago

Package Health Score

62 / 100
Full package analysis

Popular optional-require functions