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

# Node

The following Node.js options configure whether to polyfill or mock certain [Node.js globals](https://nodejs.org/docs/latest/api/globals.html).

- **Type:** `false | object`

Set `node` to `false` to disable all Node.js global polyfills and mocks. Otherwise, configure the individual options below.

## node.global

- **Type:** `boolean | 'warn'`
- **Default:** `false` when the [target](https://rspack.rs/config/target.md) provides `global` natively, otherwise `'warn'`.

Controls whether Rspack should provide a polyfill for the Node.js `global` object when bundling for non-Node environments.

For example, `target: 'node'` defaults to `false`, while `target: 'web'` defaults to `'warn'`.

See [the Node.js documentation](https://nodejs.org/api/globals.html#globals_global) for the exact behavior of this object.

### Available values

- `true`: Rspack injects a polyfill so that `global` is available in the bundle. This ensures compatibility with code that relies on Node.js globals in non-Node runtimes
- `false`: No polyfill is provided. References to `global` remain untouched. If your target environment does not define `global`, accessing it will throw a `ReferenceError` at runtime
- `'warn'`: Inject a polyfill like `true`, but also emit a warning when `global` is used

### Examples

For example, to disable `global` polyfill:

```js title="rspack.config.mjs"
export default {
  node: {
    global: false,
  },
};
```

## node.\_\_filename

- **Type:** `boolean | 'mock' | 'warn-mock' | 'eval-only' | 'node-module'`
- **Default:**
  - If `target` does not include `node`, defaults to `'warn-mock'`.
  - If `target` includes `node`, uses `'node-module'` if [output.module](https://rspack.rs/config/output.md#outputmodule) is enabled, otherwise `'eval-only'`.

Controls how Rspack handles the Node.js [`__filename`](https://nodejs.org/api/modules.html#__filename) and [`import.meta.filename`](https://nodejs.org/api/esm.html#importmetafilename) variables when bundling for non-Node environments.

### Available values

- `true`: Replace with the relative file path of the source file, relative to the [`context`](https://rspack.rs/config/context.md) option
- `false`: Do nothing and keep the native behavior
- `'mock'`: Replace with `'/index.js'`
- `'eval-only'`: Equivalent to `false`
- `'warn-mock'`: Replace with `'/index.js'`, and emit a warning to indicate a potential Node.js dependency in the code
- `'node-module'`: Only used when [output.module](https://rspack.rs/config/output.md#outputmodule) is enabled. Replace `__filename` in CommonJS with an equivalent implementation based on `import.meta.url`, suitable for ESM output

### Examples

For example, to avoid processing `__filename` and `import.meta.filename`, preserving the original content in the output:

```js title="rspack.config.mjs"
export default {
  node: {
    __filename: false,
  },
};
```

To replace `__filename` and `import.meta.filename` in ESM output with `fileURLToPath(import.meta.url)`:

```js title="rspack.config.mjs"
export default {
  target: 'node',
  output: {
    module: true,
  },
  node: {
    __filename: 'node-module',
  },
};
```

## node.\_\_dirname

- **Type:** `boolean | 'mock' | 'warn-mock' | 'eval-only' | 'node-module'`
- **Default:**
  - If `target` does not include `node`, defaults to `'warn-mock'`.
  - If `target` includes `node`, uses `'node-module'` if [output.module](https://rspack.rs/config/output.md#outputmodule) is enabled, otherwise `'eval-only'`.

Controls how Rspack handles the Node.js [`__dirname`](https://nodejs.org/api/modules.html#__dirname) and [`import.meta.dirname`](https://nodejs.org/api/esm.html#importmetadirname) variables when bundling for non-Node environments.

### Available values

- `true`: Replace with the directory path of the source file, relative to the [`context`](https://rspack.rs/config/context.md) option
- `false`: Do nothing and keep the native behavior
- `'mock'`: Replace with `'/'`
- `'eval-only'`: Equivalent to `false`
- `'warn-mock'`: Replace with `'/'`, and emit a warning to indicate a potential Node.js dependency in the code
- `'node-module'`: Only used when [output.module](https://rspack.rs/config/output.md#outputmodule) is enabled. Replace `__dirname` in CommonJS with an equivalent implementation based on `import.meta.url`, suitable for ESM output

### Examples

For example, to avoid processing `__dirname` and `import.meta.dirname`, preserving the original content in the output:

```js title="rspack.config.mjs"
export default {
  node: {
    __dirname: false,
  },
};
```

To replace `__dirname` and `import.meta.dirname` in ESM output with `fileURLToPath(import.meta.url + "/..")`:

```js title="rspack.config.mjs"
export default {
  target: 'node',
  output: {
    module: true,
  },
  node: {
    __dirname: 'node-module',
  },
};
```


This page is adapted from [webpack documentation](https://webpack.js.org/configuration/node/) under the [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), with modifications.

