• Version 4.0.2
  • Published
  • 23.8 kB
  • 1 dependency
  • MIT license


npm i bundle-require
yarn add bundle-require
pnpm add bundle-require


bundle and require a file



variable dynamicImport

const dynamicImport: RequireFunction;
  • Dynamically import files.

    As a temporary workaround for Jest's lack of stable ESM support, we fallback to require if we're in a Jest environment. See https://github.com/vitejs/vite/pull/5197#issuecomment-938054077

    Parameter file

    File path to import.

variable JS_EXT_RE

const JS_EXT_RE: RegExp;


    function bundleRequire

    bundleRequire: <T = any>(
    options: Options
    ) => Promise<{ mod: T; dependencies: string[] }>;

      function externalPlugin

      externalPlugin: ({
      }?: {
      external?: (string | RegExp)[] | undefined;
      notExternal?: (string | RegExp)[] | undefined;
      }) => Plugin;
      • An esbuild plugin to mark node_modules as external

      function injectFileScopePlugin

      injectFileScopePlugin: () => Plugin;

        function match

        match: (id: string, patterns?: (string | RegExp)[]) => boolean;

          function tsconfigPathsToRegExp

          tsconfigPathsToRegExp: (paths: Record<string, any>) => RegExp[];


            interface Options

            interface Options {}

              property cwd

              cwd?: string;

                property esbuildOptions

                esbuildOptions?: BuildOptions & {
                | boolean
                | {
                onRebuild?: RebuildCallback;
                • esbuild options


                  esbuildOptions.watch is deprecated, use onRebuild instead

                property external

                external?: (string | RegExp)[];
                • External packages

                property filepath

                filepath: string;
                • The filepath to bundle and require

                property format

                format?: 'cjs' | 'esm';
                • Provide bundle format explicitly to skip the default format inference

                property getOutputFile

                getOutputFile?: GetOutputFile;
                • Get the path to the output file By default we simply replace the extension with .bundled_{randomId}.js

                property onRebuild

                onRebuild?: (ctx: {
                err?: Pick<BuildFailure, 'errors' | 'warnings'>;
                mod?: any;
                dependencies?: string[];
                }) => void;
                • Enable watching and call the callback after each rebuild

                property preserveTemporaryFile

                preserveTemporaryFile?: boolean;
                • Preserve compiled temporary file for debugging Default to process.env.BUNDLE_REQUIRE_PRESERVE

                property require

                require?: RequireFunction;
                • The require function that is used to load the output file Default to the global require function This function can be asynchronous, i.e. returns a Promise

                property tsconfig

                tsconfig?: string;
                • A custom tsconfig path to read paths option

                Type Aliases

                type GetOutputFile

                type GetOutputFile = (filepath: string, format: 'esm' | 'cjs') => string;

                  type RebuildCallback

                  type RebuildCallback = (
                  error: Pick<BuildFailure, 'errors' | 'warnings'> | null,
                  result: BuildResult | null
                  ) => void;

                    type RequireFunction

                    type RequireFunction = (
                    outfile: string,
                    ctx: {
                    format: 'cjs' | 'esm';
                    ) => any;

                      Package Files (1)

                      Dependencies (1)

                      Dev Dependencies (7)

                      Peer Dependencies (1)


                      To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                      You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/bundle-require.

                      • Markdown
                      • HTML
                        <a href="https://www.jsdocs.io/package/bundle-require"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>