JS Utils Kit
    Preparing search index...

    Function resolveModuleRelative

    • Resolves a path relative to the provided module's directory.

      Parameters

      • relativePath: string

        Path relative to the module directory

      • metaUrlOrPath: string

        import.meta.url (ESM) or __filename (CJS)

      Returns string

      Absolute resolved path.

      Useful for loading internal assets such as:

      • .hbs templates
      • JSON files
      • SQL schemas
      • bundled static resources

      Unlike process.cwd(), this resolves relative to the module file location, making it safe for usage inside node_modules.

      The relativePath must follow strict rules:

      Allowed:

      • ./file → same directory
      • ../file → one level up (only one ../ allowed)
      • /file → absolute path
      • file/... → normal relative paths (no leading ./)

      Disallowed:

      • Multiple parent traversals (../../file)
      • Nested traversal tricks (a/../file)
      • Any path containing more than one ../

      These constraints help prevent directory traversal vulnerabilities.

      If module path is not provided.

      const templatePath = resolveModuleRelative(
      "../templates/welcome.hbs",
      import.meta.url
      );
      const templatePath = resolveModuleRelative(
      "../templates/welcome.hbs",
      __filename
      );