> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# lib.shims

- **Type:**

```ts
type Shims = {
  cjs?: {
    'import.meta.url'?: boolean;
    'import.meta.dirname'?: boolean;
    'import.meta.filename'?: boolean;
  };
  esm?: {
    __filename?: boolean;
    __dirname?: boolean;
    require?: boolean;
  };
};
```

- **Default:**

```js
const defaultShims = {
  cjs: {
    'import.meta.url': true,
    'import.meta.dirname': true,
    'import.meta.filename': true,
  },
  esm: {
    __filename: false,
    __dirname: false,
    require: false,
  },
};
```

- **Top-level config:** Supported

Configure the [shims](https://developer.mozilla.org/en-US/docs/Glossary/Shim) for CommonJS and ESM output.

## shims.cjs

Set the fields to `true` to enable the corresponding shims for CommonJS output.

### shims.cjs\['import.meta.url']

Whether to inject shims for the `import.meta.url` in CommonJS output.

- **Default:** `true`

Options:

- `true`: when [format](/config/lib/format.md) is `cjs`, the `import.meta.url` in source code will be replaced with the URL of the current module.

  For example, given the following source code:

  ```js
  import { readFileSync } from 'fs';
  const buffer = readFileSync(new URL('./data.proto', import.meta.url));
  ```

  the CJS output will be transformed to:

  ```js
  const { readFileSync } = require('fs');
  const buffer = readFileSync(
    new URL(
      './data.proto',
      /*#__PURE__*/ (function () {
        return typeof document === 'undefined'
          ? new (module.require('url'.replace('', '')).URL)(
              'file:' + __filename,
            ).href
          : (document.currentScript && document.currentScript.src) ||
              new URL('main.js', document.baseURI).href;
      })(),
    ),
  );
  ```

- `false`: the `import.meta.url` will be left as is, which will cause a runtime error when running the output.

### shims.cjs\['import.meta.dirname']

Whether to inject shims for the `import.meta.dirname` in CommonJS output.

- **Default:** `true`

Options:

- `true`: when [format](/config/lib/format.md) is `cjs`, the `import.meta.dirname` in source code will be replaced with the `__dirname`.

  For example, given the following source code:

  ```js
  console.log(import.meta.dirname);
  ```

  the CJS output will be transformed to:

  ```js
  console.log(__dirname);
  ```

- `false`: the `import.meta.dirname` will be left as is, which will cause a runtime error when running the output.

### shims.cjs\['import.meta.filename']

Whether to inject shims for the `import.meta.filename` in CommonJS output.

- **Default:** `true`

Options:

- `true`: when [format](/config/lib/format.md) is `cjs`, the `import.meta.filename` in source code will be replaced with the `__filename`.

  For example, given the following source code:

  ```js
  console.log(import.meta.filename);
  ```

  the CJS output will be transformed to:

  ```js
  console.log(__filename);
  ```

- `false`: the `import.meta.filename` will be left as is, which will cause a runtime error when running the output.

## shims.esm

Set the fields to `true` to enable the corresponding shims for ESM output.

### shims.esm.\_\_filename

Whether to inject shims for the global `__filename` of CommonJS in ESM output.

- **Default:** `false`

Options:

- `true`: when [format](/config/lib/format.md) is `esm`, the `__filename` in source code will be replaced with the filename of the current module.

  For example, given the following source code:

  ```js
  console.log(__filename);
  ```

  the ESM output will be transformed to:

  ```js
  import { fileURLToPath as __webpack_fileURLToPath__ } from 'url';
  import { dirname as __webpack_dirname__ } from 'path';
  var src_dirname = __webpack_dirname__(
    __webpack_fileURLToPath__(import.meta.url),
  );
  var src_filename = __webpack_fileURLToPath__(import.meta.url);
  console.log(src_filename);
  ```

- `false`: the `__filename` will be left as is, which will cause a runtime error when running the output.

### shims.esm.\_\_dirname

Whether to inject shims for the global `__dirname` of CommonJS in ESM output.

- **Default:** `false`

Options:

- `true`: when [format](/config/lib/format.md) is `esm`, the `__dirname` in source code will be replaced with the directory name of the current module.

  For example, given the following source code:

  ```js
  console.log(__dirname);
  ```

  the ESM output will be transformed to:

  ```js
  import { fileURLToPath as __webpack_fileURLToPath__ } from 'url';
  import { dirname as __webpack_dirname__ } from 'path';
  var src_dirname = __webpack_dirname__(
    __webpack_fileURLToPath__(import.meta.url),
  );
  console.log(src_dirname);
  ```

- `false`: the `__dirname` will be left as is, which will cause a runtime error when running the output.

### shims.esm.require

Whether to inject shims for the global `require` of CommonJS in ESM output.

- **Default:** `false`

Options:

- `true`: when [format](/config/lib/format.md) is `esm`, there will be a `require` that is created by `createRequire` at the beginning of the output which can be accessed in source code like the global `require` like CommonJS.

  For example, given the following source code:

  ```js
  const someModule = require('./someModule');

  // dynamic require
  const dynamicRequiredModule = require(SOME_VALUE_IN_RUNTIME);
  // require.resolve
  const someModulePath = require.resolve('./someModule');
  // use require as a expression
  const lazyFn = (module, requireFn) => {};
  lazyFn('./other.js', require);
  ```

  the ESM output will be transformed to:

  ```js
  import __rslib_shim_module__ from 'node:module';
  const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(
    import.meta.url,
  );
  // dynamic require
  require(SOME_VALUE_IN_RUNTIME);
  // require.resolve
  require.resolve('./someModule');
  // use require as a expression
  const lazyFn = (module, requireFn) => {};
  lazyFn('./other.js', require);
  ```

- `false`: the `require` will be left as is, which will cause a runtime error when running the output.
