question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hoisting breaks `require` semantics

See original GitHub issue

Hoisting is intended as a runtime optimization that doesn’t affect program correctness. But it’s easy to accidentally a write a package that only works when hoisting is in effect that then breaks if deployed as a normal package.

Expected Behavior

Each package should have access to the exact same set of dependencies, whether or not hoisting is applied.

Current Behavior

Hoisted dependencies leak into packages that otherwise should not have access to them.

Possible Solution

Move the hoisted dependencies out of the natural node module resolution path and instead link them explicitly into the packages where they are appropriate.

For example, assume that first and second share my-hoisted-dep. Today we will get this, which leaks my-hoisted-dep into third:

├── node_modules
│   └── my-hoisted-dep
└── packages
    ├── first
    ├── second
    └── third

Instead we could move the hoisted deps off the module resolution path:

├── lerna-common
│   └── node_modules
│       └── my-hoisted-dep
└── packages
    ├── first
    │   └── node_modules
    │       └── my-hoisted-dep -> ../../../lerna-common/node_modules/my-hoisted-dep
    ├── second
    │   └── node_modules
    │       └── my-hoisted-dep -> ../../../lerna-common/node_modules/my-hoisted-dep
    └── third

This would also eliminate the need for npm --global-style, which is an incomplete solution to this same general problem (it replaces wrong versions with correct versions, but won’t replace wrong versions with “no version”).

Context

Moving a piece of code between packages is a reasonably common thing to do. If you do that while using hoisting, it’s easy to produce packages that no longer work standalone without realizing it. For this reason, IMO it’s not currently safe to use --hoist, especially in environments like CI, since it will let this class of bug escape.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:8
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
evocateurcommented, Jun 6, 2017

I would argue that’s what eslint-plugin-import is for (specifically import/no-unresolved).

1reaction
jquensecommented, Feb 6, 2018

One project cannot import the dependencies from another project

That’s not true, you can import nearly every transitive dependency regardless of whether you depend on it directly just look in node_modules on a single package and try, yes Lerna theoretically adds more you can accidentally require but it’s not more unsafe than any project that uses npm or yarn

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript Hoisting Explained By Examples
In this tutorial, you'll learn how about the JavaScript hoisting and how it works under the hood.
Read more >
Hoisting & Rigging Fundamentals
The catalog term. "Breaking Strength" -- is the nominal strength given the rope by engineers. When put under tension on a test device,...
Read more >
NoUndef attribute breaks hoisting? We have a speculation problem ...
It's true that to hoist a load from a loop you need to drop !noundef (in general). ... it usually means that the...
Read more >
JavaScript Hoisting - W3Schools
Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script...
Read more >
How does this hoisting work with block scope? - Stack Overflow
According to the web compat semantics at the place of the function declaration, the value of the blocked scope variable is bound to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found