Lodash _.runInContext() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
Util utilities

What You’ll Learn

By the end of this tutorial, you’ll use Lodash’s _.runInContext() to spin up a separate Lodash copy—so custom mixins and experiments never leak onto the shared global _.

01

Core Syntax

_.runInContext()

02

Fresh copy

Full Lodash fn.

03

Isolation

Mixin safely.

04

Plugins

Library authors.

05

vs noConflict

Globals vs fork.

06

+ mixin

Extend the copy.

What Is _.runInContext()?

_.runInContext() creates a new Lodash function with its own internal context—the machinery Lodash uses for constructors, templates, and method wiring. The copy behaves like regular Lodash: you can call lodash.map, lodash.get, and lodash.mixin on it. Changes you make through _.mixin() on that copy stay on that copy.

💡
Beginner tip — do not pass custom methods as “context”

Older tutorials show _.runInContext({ customMixin: {} })—that is not how you add APIs. Call _.runInContext(), then lodash.mixin({ myHelper() {} }) on the returned function.

Think of it as a sandboxed Lodash for plugin authors: extend freely, ship your plugin, and leave the app’s main _ untouched.

📝 Syntax

Most projects call it with no arguments:

javascript
_.runInContext([context])

Syntax Rules

  • No arguments (usual) — returns a pristine Lodash function in a new context.
  • Optional context — advanced Lodash internals; skip unless you are deep in library customization.
  • Return value — a Lodash function—assign it: const lodash = _.runInContext().
  • Extend with mixin — add methods via lodash.mixin({ ... }), not via the context object.
  • Globals unchanged — unlike noConflict, global _ is not released or replaced.
javascript
import runInContext from "lodash/runInContext";



const lodash = runInContext();



lodash.map([1, 2, 3], (n) => n * 2);

// [2, 4, 6]

⚡ Quick Reference

TaskCode patternNotes
Isolated copyconst lodash = _.runInContext()Default use
Add custom fnlodash.mixin({ fn() {} })On the copy
Plugin sandboxrunInContext + mixinNo global leak
Two extensionsconst a = _.runInContext(); const b = …Separate copies
Free global __.noConflict()Not runInContext
App utilitiesexport function helper()Often enough
Returns
Function

Lodash copy

Args
0 or 1

Usually none

Isolation
Yes

Own context

Category
Util

Meta API

🧰 Parameters

Argument to _.runInContext() and what you receive:

context Optional

Lodash internal context object (constructors, ctx template). Omit for the standard isolated copy beginners need.

_.runInContext()
return value Lodash fn

A callable Lodash function with the full method surface—store it in a variable and pass it to consumers.

const lodash = _.runInContext()
extensions Via mixin

Custom APIs belong on the returned function through lodash.mixin, not inside the context parameter.

lodash.mixin({ tap() {} })
global _ Unchanged

The original _ remains as-is. Only the new copy receives mixins you add to it (unless you also mixin global _).

_.map !== lodash.map // if extended

Lodash’s own _.mixin() docs demonstrate runInContext immediately before mixing into an isolated instance.

Examples Gallery

Practical _.runInContext() patterns with copy-ready code, sample output, and interactive Try It Yourself labs.

📚 Getting Started

Create a copy and use built-in methods normally.

Example 1 — Pristine Lodash copy

Same API as global _—just a separate instance.

javascript
const lodash = _.runInContext();



console.log(lodash.map([1, 2, 3], (n) => n * 2));

console.log(lodash.VERSION === _.VERSION);

// [2, 4, 6]

// true
Try It Yourself

How It Works

runInContext() bootstraps a new Lodash function object. Built-ins work immediately—no setup object required.

Example 2 — Official mixin isolation pattern

From Lodash docs—foo on global _, bar only on the fork.

javascript
_.mixin({ foo: _.constant("foo") });



const lodash = _.runInContext();



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



console.log(_.isFunction(_.foo));

console.log(_.isFunction(_.bar));

console.log(lodash.isFunction(lodash.bar));

// true, false, true
Try It Yourself

How It Works

Mixins on the fork do not back-propagate to _. Global foo is visible on both; bar exists only on lodash.

📈 Practical Patterns

Multiple sandboxes, plugins, and safe globals.

Example 3 — Two isolated copies

Separate plugins—each extends its own Lodash without clobbering the other.

javascript
const pluginA = _.runInContext();

const pluginB = _.runInContext();



pluginA.mixin({ greet: () => "Hello from A" });

pluginB.mixin({ greet: () => "Hello from B" });



console.log(pluginA.greet());

console.log(pluginB.greet());

// Hello from A

// Hello from B
Try It Yourself

How It Works

Both define greet—no conflict because each lives on a different Lodash function object.

Example 4 — Global _ stays clean

Extend the fork; verify the shared global did not pick up the mixin.

javascript
const lodash = _.runInContext();



lodash.mixin({

  doubleAll: (arr) => lodash.map(arr, (n) => n * 2),

});



console.log(lodash.doubleAll([1, 2, 3]));

console.log(_.isFunction(_.doubleAll));

// [2, 4, 6]

// false

How It Works

App code keeps using plain _; your library exports methods from its private lodash copy.

Example 5 — Minimal plugin factory

Pattern for shipping a small Lodash extension as a module.

javascript
function createMyLodash(base) {

  const lodash = base.runInContext();



  lodash.mixin({

    sumByProp(arr, key) {

      return lodash.sumBy(arr, key);

    },

  });



  return lodash;

}



const myLodash = createMyLodash(_);



console.log(myLodash.sumByProp(

  [{ v: 1 }, { v: 2 }],

  "v"

));

// 3

How It Works

Consumers import myLodash (or named helpers) instead of mutating the global they share with other scripts.

🚀 Beyond the Basics

runInContext vs noConflict vs plain modules.

Example 6 — When to use which tool

Pick the right isolation strategy for your environment.

javascript
// Browser: Underscore owns global _ — free the name once

// const lodash = _.noConflict();



// Plugin author: extra copy, global _ unchanged

const lodash = _.runInContext();

lodash.mixin({ myPluginFn() {} });



// Modern app: plain modules — often no meta API needed

// import { map } from "lodash-es";

When to use which

noConflict solves global naming collisions. runInContext solves extension isolation. ES modules solve most app-level scoping without either.

🧠 How _.runInContext() Works

1

Clone template

Lodash duplicates its internal context (constructors, helpers) or uses the one you pass (advanced).

Setup
2

Build new _ fn

Wire all standard methods onto a fresh Lodash function object.

Factory
3

Mixin in isolation

Extensions added via lodash.mixin attach only to this context.

Extend
=

Private Lodash

A full Lodash you can export from a library without side effects on the host’s _.

📝 Notes

  • Do not pass { customMethods: {} } expecting plug-in APIs—the optional context is not a settings bag.
  • Use lodash.mixin on the returned function to add helpers (see mixin tutorial).
  • runInContext does not replace noConflict—different problems (isolation vs global naming).
  • Each call produces another full copy—fine for plugins; avoid creating hundreds in hot paths.
  • In bundler apps, plain modules plus named imports cover most needs without runInContext.
  • Next in the series: _.stubArray()—empty array factory for defaults.

Conclusion

_.runInContext() is Lodash’s isolation switch for authors who extend the library. Create a copy, mixin your helpers there, and ship the copy—global _ and other extensions stay out of the way.

For script-tag _ collisions, use noConflict. For everyday app utilities, use modules. Reach for runInContext when you are building on Lodash itself.

💡 Best Practices

✅ Do

  • Call _.runInContext() with no args unless you know the internal context API
  • Mixin on the returned copy for plugin methods
  • Export the isolated copy (or wrappers) from your library entry point
  • Follow the official foo/bar isolation demo when testing mixins
  • Document that consumers should use your copy, not assume global extensions

❌ Don’t

  • Pass arbitrary objects as “context” expecting custom methods to appear
  • Mixin experimental APIs directly onto shared global _ in libraries
  • Confuse runInContext with noConflict—they solve different issues
  • Assume every app needs runInContext—modules often suffice
  • Overwrite built-in Lodash methods on the fork without namespacing

Key Takeaways

Knowledge Unlocked

Five things to remember about _.runInContext()

Use these points when extending Lodash safely.

5
Core concepts
🔄 02

Isolation

Own context.

Why
03

+ mixin

Add APIs.

Pattern
📝 04

Not context bag

Use mixin.

Pitfall
🛠 05

vs noConflict

Different job.

Compare

❓ Frequently Asked Questions

_.runInContext() returns a new Lodash function backed by its own internal context—a separate copy of the library. Built-in methods work the same; extensions you add to that copy do not automatically appear on the global _.
No—that is a common mistake. The optional context argument is for Lodash internals (advanced). Add custom APIs with lodash.mixin({ myFn() {} }) on the copy returned by runInContext(), not as properties of the context parameter.
noConflict frees the browser global _ for another library and returns Lodash once. runInContext creates an additional Lodash instance without changing globals—ideal when you need isolation while _ stays Lodash.
Use it when building Lodash plugins, shipping a library that extends Lodash, or testing mixins without polluting the shared global _. Pair it with mixin on the isolated copy.
Usually not for everyday app code—each module import is already scoped. runInContext matters most for plugin authors, script-tag globals, or when multiple extensions must not collide on one _ object.
Yes—you get a full Lodash function (map, get, mixin, chain, etc.) wired to a fresh context. It is not a partial subset.
Did you know?

Lodash’s _.mixin documentation uses _.runInContext() in the same breath—official proof that isolated copies are the intended home for custom methods, not one-off hacks on the global _.

Practice _.runInContext() in the Live Editor

Try a pristine copy, the official mixin isolation demo, and dual plugin sandboxes.

Open Try It editor →

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.

6 people found this page helpful