website/docs/en/config/node.mdx
import WebpackLicense from '@components/WebpackLicense';
<WebpackLicense from="https://webpack.js.org/configuration/node/" />The following Node.js options configure whether to polyfill or mock certain Node.js globals.
boolean | 'warn''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.
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 runtimesfalse: 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 usedFor example, to disable global polyfill:
export default {
node: {
global: false,
},
};
boolean | 'mock' | 'warn-mock' | 'eval-only'target does not include node, defaults to 'warn-mock'.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.
true: Replace with the relative file path of the source file, relative to the context optionfalse: 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 outputFor example, to avoid processing __filename and import.meta.filename, preserving the original content in the output:
export default {
node: {
__filename: false,
},
};
To replace __filename and import.meta.filename in ESM output with fileURLToPath(import.meta.url):
export default {
target: 'node',
output: {
module: true,
},
node: {
__filename: 'node-module',
},
};
boolean | 'mock' | 'warn-mock' | 'eval-only'target does not include node, defaults to 'warn-mock'.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.
true: Replace with the directory path of the source file, relative to the context optionfalse: 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 outputFor example, to avoid processing __dirname and import.meta.dirname, preserving the original content in the output:
export default {
node: {
__dirname: false,
},
};
To replace __dirname and import.meta.dirname in ESM output with fileURLToPath(import.meta.url + "/.."):
export default {
target: 'node',
output: {
module: true,
},
node: {
__dirname: 'node-module',
},
};