node.js - Symlink into node_modules for TypeScript? - Stack Overflow

admin2025-04-15  4

I'm planning of building my own package manager and registry, but I need to support file: dependencies. The reason why I want to do this is the lot of trouble I have using NPM, Bun, or whatever, and the forced use of React in the ecosystem leads to version conflicting to ever changing React's major version (I want to sort of create a React-Emotion-TypeScript-TypeDoc integrated ecosystem that targets HTML5 and Node.js).

I'm just playing with node_modules, but am unable to symlink a local dependency there. Why can Node.js do it through symlink too though?

Reproducing it. I have created these files manually, and symlinked the y directory using Windows Create shortcut* action.

x/index.ts

// Could not resolve "y" module (TypeScript IDE error)
import { property } from "y";

x/tsconfig.json

{
    "compilerOptions": {
        "target": "ES2024",
        "moduleResolution": "node",
        "preserveSymlinks": true
    }
}

x/node_modules/y = symlink to a y directory

x/node_modules/y/package.json

{
  "name": "y",
  "version": "0.1.0",
  "main": "index.ts"
}

x/node_modules/y/index.ts

export const property: number = 10;

x/node_modules/y/tsconfig.json

{
    "compilerOptions": {
        "target": "ES2024"
    }
}

Why is y not found when importing it from x?

I'm planning of building my own package manager and registry, but I need to support file: dependencies. The reason why I want to do this is the lot of trouble I have using NPM, Bun, or whatever, and the forced use of React in the ecosystem leads to version conflicting to ever changing React's major version (I want to sort of create a React-Emotion-TypeScript-TypeDoc integrated ecosystem that targets HTML5 and Node.js).

I'm just playing with node_modules, but am unable to symlink a local dependency there. Why can Node.js do it through symlink too though?

Reproducing it. I have created these files manually, and symlinked the y directory using Windows Create shortcut* action.

x/index.ts

// Could not resolve "y" module (TypeScript IDE error)
import { property } from "y";

x/tsconfig.json

{
    "compilerOptions": {
        "target": "ES2024",
        "moduleResolution": "node",
        "preserveSymlinks": true
    }
}

x/node_modules/y = symlink to a y directory

x/node_modules/y/package.json

{
  "name": "y",
  "version": "0.1.0",
  "main": "index.ts"
}

x/node_modules/y/index.ts

export const property: number = 10;

x/node_modules/y/tsconfig.json

{
    "compilerOptions": {
        "target": "ES2024"
    }
}

Why is y not found when importing it from x?

Share Improve this question edited Feb 4 at 14:34 Hydroper asked Feb 4 at 14:16 HydroperHydroper 217 bronze badges 2
  • Please, clarify the steps to reproduce. Do you run "tsc"? Is it TS or Node error? There is supposed to be be package.json in project root, it's unclear if there's one – Estus Flask Commented Feb 4 at 14:26
  • @EstusFlask I have manually done it. It is a TS IDE error. – Hydroper Commented Feb 4 at 14:34
Add a comment  | 

1 Answer 1

Reset to default 0

The problem is that the Windows Explorer's Create shortcut action that I was using creates a file symbolic link, not a directory symbolic link.

I had to use the command prompt to create the symlink:

mklink /d "x/node_modules/y" "C:\Users\hydro\Repository Groups\Jet\y"

Then the y module was finally found by TypeScript.

转载请注明原文地址:http://www.anycun.com/QandA/1744713948a86596.html