Creates a new pristine Lodash function using an optional context object. The copy is independent of the shared _ — mixins on one do not appear on the other. Part of Lodash Util (stable — not deprecated).
Pass the arguments shown below. Prefer modular imports when you only need this helper.
Syntax
Call _.runInContext. Start with this line.
Lodash
_.runInContext([context=root])
Call
What it does
_.runInContext([context=root])
Primary signature.
_.runInContext(...)
Call the util helper.
Prefer import runInContext from "lodash/runInContext" when you want a smaller bundle.
Override natives Lodash uses — Node example from the docs.
Lodash
// Official Node.js idea: use setImmediate instead of setTimeout
// var defer = _.runInContext({ 'setTimeout': setImmediate }).defer;
// Browser-friendly mock of the same idea:
var ticks = 0;
var lodash = _.runInContext({
setTimeout: function (fn) {
ticks += 1;
fn();
}
});
lodash.defer(function () {});
// ticks → 1 (mock ran immediately)
Identity. The copy is not the same reference as _.
noConflict. Gives up the global name for another library.
Pick. Isolation → runInContext; name clash → noConflict.
Compatibility
Browser & Runtime Support
_.runInContext is a Lodash Util helper (stable). It needs the full Lodash build — modular single-method imports do not include it. Works in modern browsers (CDN) and Node.js / Bun / Deno with npm install lodash.
✓ Stable · Full Lodash build
_.runInContext()
Available in [email protected] full builds. Custom context overrides (e.g. setImmediate) are most useful in Node. Evergreen browsers and current Node LTS are fine when Lodash is loaded.
100%With full Lodash build
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.runInContextFull Lodash build
Bottom line: Use the full lodash package or CDN build. Create one isolated copy for plugins; mixin on that copy, not the shared _.
Frequently Asked Questions
_.runInContext([context]) returns a new pristine Lodash function. Built-ins work the same; mixins on that copy do not appear on the shared global _.
An optional object that supplies natives Lodash uses (advanced) — for example overriding setTimeout. Do not put your custom helpers there; add those with .mixin() on the returned Lodash.
noConflict restores the previous global _ and returns Lodash once. runInContext creates an extra Lodash instance without changing globals.
Plugin authors, libraries that mixin helpers, or tests that must not pollute the shared _. Pair it with lodash.mixin on the isolated copy.
Rarely for everyday app code. It matters most with script-tag globals or when multiple extensions must not collide on one _.
No. It is a stable Lodash Util method. Official docs also recommend it before mixin to avoid conflicts.