Back to Rspack

Node

website/docs/en/config/node.mdx

2.0.14.3 KB
Original Source

import WebpackLicense from '@components/WebpackLicense';

<WebpackLicense from="https://webpack.js.org/configuration/node/" />

Node

The following Node.js options configure whether to polyfill or mock certain Node.js globals.

node.global

  • Type: boolean | 'warn'
  • Default: 'warn'

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

See the Node.js documentation 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
export default {
  node: {
    global: false,
  },
};

node.__filename

  • Type: boolean | 'mock' | 'warn-mock' | 'eval-only'
  • Default:
    • If target does not include node, defaults to 'warn-mock'.
    • If target includes node, uses 'node-module' if output.module is enabled, otherwise 'eval-only'.

Controls how Rspack handles the Node.js __filename and import.meta.filename variables when bundling for non-Node environments.

Available values

  • true: Replace with the relative file path of the source file, relative to the context 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 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
export default {
  node: {
    __filename: false,
  },
};

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

js
export default {
  target: 'node',
  output: {
    module: true,
  },
  node: {
    __filename: 'node-module',
  },
};

node.__dirname

  • Type: boolean | 'mock' | 'warn-mock' | 'eval-only'
  • Default:
    • If target does not include node, defaults to 'warn-mock'.
    • If target includes node, uses 'node-module' if output.module is enabled, otherwise 'eval-only'.

Controls how Rspack handles the Node.js __dirname and import.meta.dirname variables when bundling for non-Node environments.

Available values

  • true: Replace with the directory path of the source file, relative to the context 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 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
export default {
  node: {
    __dirname: false,
  },
};

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

js
export default {
  target: 'node',
  output: {
    module: true,
  },
  node: {
    __dirname: 'node-module',
  },
};