Lodash _.runInContext() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Util

Definition

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])
CallWhat it does
_.runInContext([context=root])Primary signature.
_.runInContext(...)Call the util helper.

Prefer import runInContext from "lodash/runInContext" when you want a smaller bundle.

Library declarations
Lodash
_.runInContext([context=root])

Parameters

ParameterDescription
contextOptional. Object supplying natives (default: root / global)

Return Value

Return ValueDescription
*A new Lodash function

Lodash Examples

Example 1 — Official: Isolated Mixins

Mixins on the copy stay off the shared _.

Lodash
var lodash = _.runInContext();

lodash.mixin({ 'bar': lodash.constant('bar') });

_.isFunction(_.bar);
// → false

lodash.isFunction(lodash.bar);
// → true
Try It Yourself

How It Works

  1. Copy. runInContext() returns a pristine Lodash.
  2. Mixin. bar is added only to that copy.
  3. Global. _.bar is missing → false.
  4. Copy. lodash.bar exists → true.

Example 2 — Safe Plugin Helper

Ship helpers on an isolated Lodash so apps stay clean.

Lodash
var lodash = _.runInContext();

lodash.mixin({
  double: function (n) {
    return n * 2;
  }
});

lodash.double(21);
// → 42

typeof _.double;
// → 'undefined'
Try It Yourself

How It Works

  1. Isolate. Fresh Lodash for the plugin.
  2. Extend. double lives only there.
  3. Call. lodash.double(21) → 42.
  4. Safe. Global _ has no double.

Example 3 — Official: Custom Context

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)
Try It Yourself

How It Works

  1. Context. Supply replacements for natives Lodash calls.
  2. Official. In Node, pass setImmediate as setTimeout.
  3. Demo. Mock setTimeout runs the callback and counts ticks.
  4. Advanced. Everyday apps rarely need a custom context.

Example 4 — runInContext vs noConflict

Different goals: clone Lodash vs free the _ name.

Lodash
// runInContext — extra Lodash; global _ unchanged
var lodash = _.runInContext();
typeof _.map;
// → 'function'
lodash === _;
// → false

// noConflict — restore previous global _; keep one Lodash handle
// var lodash = _.noConflict();
// // window._ is no longer this Lodash
Try It Yourself

How It Works

  1. runInContext. Adds a private Lodash; _ stays put.
  2. Identity. The copy is not the same reference as _.
  3. noConflict. Gives up the global name for another library.
  4. Pick. Isolation → runInContext; name clash → noConflict.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.runInContext Full 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.

Next: _.stubArray()

Return a brand-new empty array on every call.

_.stubArray() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful