Skip to content

TypeScript should not use dependencies from an unrelated node_modules directory in a sibling directory #44362

Closed
@bluenote10

Description

@bluenote10

Bug Report

🔎 Search Terms

wrong dependency unrelated node_modules sibling directory

Related: #30124 (Declarations are unexpectedly used from node_modules in parent directory) but this issue is about unexpectedly using node_modules from a sibling directory.

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ, finding nothing related.

⏯ Playground Link

Not reproducible on Playground because it requires a sibling folder containing a node_modules.

A fully self-contained minimal reproduction example is here on GitHub.

💻 Code

Originally I have posted this issue as a question on StackOverflow, but after several days of experimentation it feels more like a bug. Refer to the question for some more details if needed.


Consider a repository structure that contains two TypeScript projects:

  • common: Some common that is used by the frontend as well.
  • backend: A code NodeJS server app.

common/src/common.ts

// an example dependency
import daysjs from "dayjs";

export function helperFunction() {
  console.log(daysjs());
}

The backend uses this common code like this:

backend/src/backend.ts

import { helperFunction } from "../../common/src/common";

console.log("Running backend");
helperFunction();

Further:

  • In common/package.json we have listed dayjs as a dependency, and we have done an npm install in common.
  • In backend/package.json we forgot to list dayjs as a dependency!

🙁 Actual behavior

The backend compilation silently succeeds, ignoring the fact the dependency is missing. Apparently TypeScript simply picks the dependency from the sibling folder common/node_modules (deleting it makes the compilation fail).

This is not in line with nodejs' behavior of resolving node_modules in parent directories only. Therefore, running the compilation result with nodejs crashes.

We have brought down our production server because of the false positive compilation.

🙂 Expected behavior

Compilation should fail, complaining about the missing dependency instead of picking it up from a sibling node_modules folder.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions