{"name":"link","version":"2.1.0","keywords":["npm","link","symlink"],"author":{"name":"Hiroki Osame","email":"hiroki.osame@gmail.com"},"license":"MIT","_id":"link@2.1.0","homepage":"https://github.com/privatenumber/link#readme","bugs":{"url":"https://github.com/privatenumber/link/issues"},"bin":{"link":"dist/cli.js"},"dist":{"shasum":"a52dee157ff7ab9b44bc18de26effeb61ca35b6a","tarball":"http://registry-npm.ywwl.com/link/download/link-2.1.0.tgz","fileCount":3,"integrity":"sha512-6jgX7ejPBOQaKsFY/9aFEg0HW0JyFuMGDoN+KQX1W94t+8Fi5xwlSlGouRMDqDlrE46drqu4PYduAG7tUwdF7Q==","signatures":[{"sig":"MEUCIQCApcBr7+4a+Gp8v0E/LQydm26SHJXTyKQRcb6Mk+rXMgIgEa2geeDSfFU2ZR10oG+Jxjatzwj3bcIsjBTrWDckwYk=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":87114,"size":29565,"noattachment":false,"key":"/link/-/link-2.1.0.tgz"},"funding":"https://github.com/privatenumber/link?sponsor=1","gitHead":"9b3d3885bd4d58163ccd45b22bb2f2860e005103","_npmUser":{"name":"hirokiosame","email":"hiroki.osame@gmail.com"},"repository":{"url":"git+https://github.com/privatenumber/link.git","type":"git"},"_npmVersion":"9.8.1","description":"A better npm link","directories":{},"_nodeVersion":"18.18.0","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/link_2.1.0_1697319459864_0.15408794953820393","host":"s3://npm-registry-packages"},"readme":"# npx link\n\nA safer and enhanced version of [`npm link`](https://docs.npmjs.com/cli/v8/commands/npm-link).\n\nWhy is `npm link` unsafe? Read the [blog post](https://hirok.io/posts/avoid-npm-link).\n\n### Features\n- 🔗 Link dependencies without removing previous links\n- 🛡 Only resolves to local paths\n- 🔥 Config file quickly linking multiple packages\n- 💫 Deep linking for quickling linking multilple packages\n\n<br>\n\n<p align=\"center\">\n\t<a href=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=platinum\">\n\t\t<picture>\n\t\t\t<source width=\"830\" media=\"(prefers-color-scheme: dark)\" srcset=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=platinum&image=dark\">\n\t\t\t<source width=\"830\" media=\"(prefers-color-scheme: light)\" srcset=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=platinum&image\">\n\t\t\t<img width=\"830\" src=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=platinum&image\" alt=\"Premium sponsor banner\">\n\t\t</picture>\n\t</a>\n</p>\n\n## Terminology\n\n- **Dependency package**\n\n\tThe package getting linked. This is usually a library.\n\n- **Consuming package**\n\n\tThe project you want to link the _Dependency package_ as a dependency of. This is usually an application.\n\n\t`consuming-package/node_modules/dependency-package` → `dependency-package`\n\n\n## Usage\n\n### Linking a package\n\nFrom the _Consuming package_ directory, link the _Dependency package_:\n\n```sh\nnpx link <dependency-package-path>\n```\n\nThis creates a symbolic link inside the `node_modules` of _Consuming package_, referencing the _Dependency package_.\n\n\n> **🛡️ Secure linking**\n>\n> Unlike `npm link`, it doesn't install the _Dependency package_ globally or re-install project dependencies.\n\n### Publish mode\n\nUsing symbolic links may not replicate the exact environment you get from a standard `npm install`. This discrepancy primarily arises from symlinked packages retaining their development `node_modules` directory. This can lead to issues, especially when multiple packages depend on the same library.\n\n<details>\n\t<summary>Here's an example</summary>\n\t<br>\n\nIn a production environment, `npm install` detects common dependencies and installs only one instance of a shared dependency. However, when there's a symbolic link to the development directory of a dependency, separate copies of those dependencies are resolved from the development `node_modules`.\n\nLet's say there's an _App A_ with a dependency on _Package B_, and they both depend on _Library C_:\n\n- Production environment\n\n\t`npm install` detects that both _App A_ and _Package B_ depends on _Library C_, and only installs one copy of _Library C_ for them to share.\n\n- Symbolic link environment\n\n\t_App A_ has its copy of _Library C_, and _Package B_ also has its development copy of _Library C_—possibly with different versions. Consequently, when you run the application, it will load two different versions of _Library C_, leading to unexpected outcomes.\n\n</details>\n\n_Publish mode_ helps replicate the production environment in your development setup.\n\n#### Setup instructions\n\n1. In the _Dependency package_, run `npm pack` to create a tarball:\n\n\t```sh\n\tcd dependency-package-path\n\tnpm pack\n\t```\n\n\tThis generates a tarball (`.tgz`) file in the current directory. Installing from this simulates the conditions of a published package without actually publishing it.\n\n\t> **Tip:** You can skip this step if this dependency is already installed from npm and there are no changes to the dependency's `package.json`\n\n2. In the _Consuming package_\n\n\t1. Install the Dependency tarball from _Step 1_\n\n\t\t```sh\n\t\tnpm install --no-save <dependency-tarball-path>\n\t\t```\n\n\t\tThis sets up the same `node_modules` tree used in a production environment.\n\n\t2. Link the _Dependency package_\n\n\t\t```sh\n\t\tnpx link publish <dependency-package-path>\n\t\t```\n\n\t\tThis creates hard links in `node_modules/dependency` to the specific publish assets of the _Dependency package_.\n\n\t\t<details>\n\t\t<summary><em>Why hard links instead of symbolic links?</em></summary>\n\t\t<br>\n\n\t\tAnother issue with the symlink approach is that Node.js, and popular bundlers, looks up the `node_module` directory relative to a module's realpath rather than the import path (symlink path). By using hard links, we can prevent this behavior and ensure that the `node_modules` directory is resolved using the production tree we set up in _Step 2_.\n\t\t</details>\n\n4. Start developing!\n\n\tAny changes you make to the _Dependency package_ will be reflected in the `node_modules` directory of the _Consuming package_.\n\n\t> **Note:** If the _Dependency package_ emits new files, you'll need to re-run `npx link publish <dependency-package-path>` to create new hard links.\n\t\n### Configuration file\n\nCreate a `link.config.json` (or `link.config.js`) configuration file at the root of the _Consuming package_ to automatically setup links to multiple _Dependency packages_.\n\nExample _link.config.json_:\n```json5\n{\n    \"packages\": [\n        \"/path/to/dependency-path-a\",\n        \"../dependency-path-b\"\n    ]\n}\n```\n\nThe configuration has the following type schema:\n```ts\ntype LinkConfig = {\n\n    // Whether to run `npx link` on dependency packages with link.config.json\n    deepLink?: boolean\n\n    // List of dependency packages to link\n    packages?: string[]\n}\n```\n\n> **Note:** It's not recommended to commit this file to source control since this is for local development with local paths.\n\n\nTo link the dependencies defined in `link.config.json`, run:\n```sh\nnpx link\n```\n\n### Deep linking\n\nBy default, `npx link` only links packages in the _Consuming package_. However, there are cases where the _Dependency packages_ also needs linking setup.\n\nDeep linking recursively runs link on every linked dependency that has a `link.config.json` file.\n\nEnable with the `--deep` flag or `deepLink` property in `link.config.json`.\n\n```sh\nnpx link --deep\n```\n\n<br>\n\n<p align=\"center\">\n\t<a href=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=gold\">\n\t\t<picture>\n\t\t\t<source width=\"830\" media=\"(prefers-color-scheme: dark)\" srcset=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=gold&image=dark\">\n\t\t\t<source width=\"830\" media=\"(prefers-color-scheme: light)\" srcset=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=gold&image\">\n\t\t\t<img width=\"830\" src=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=gold&image\" alt=\"Premium sponsor banner\">\n\t\t</picture>\n\t</a>\n</p>\n\n## FAQ\n\n### Why should I use `npx link` over `npm link`?\nBecause `npm link` [is complicated and dangerous to use](https://hirok.io/posts/avoid-npm-link). And `npx link` offers more features such as _Publish mode_.\n\n### How do I remove the links?\nRun `npm install` and it should remove them.\n\n`npm install` enforces the integrity of `node_modules` by making sure all packages are correctly installed. Reverting the links is a side effect of this.\n\n### Why does `npx link` point to `ln`?\n\nYou must use npx v7 or higher. Check the version with `npx -v`.\n\nIn the obsolete npx v6, local binaries take precedence over npm modules so  `npx link` can point to the native `link`/`ln` command:\n```\n$ npx link\nusage: ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file [target_file]\n       ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file ... target_dir\n       link source_file target_file\n```\n\nTo work around this, install `link` globally first:\n```sh\n$ npm i -g link\n$ npx link\n```\n\n## Related\n\n- [`npx ci`](https://github.com/privatenumber/ci) - A better `npm ci`.\n\n\n## Sponsors\n\n<p align=\"center\">\n\t<a href=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver1\">\n\t\t<picture>\n\t\t\t<source width=\"410\" media=\"(prefers-color-scheme: dark)\" srcset=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver1&image=dark\">\n\t\t\t<source width=\"410\" media=\"(prefers-color-scheme: light)\" srcset=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver1&image\">\n\t\t\t<img width=\"410\" src=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver1&image\" alt=\"Premium sponsor banner\">\n\t\t</picture>\n\t</a>\n\t<a href=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver2\">\n\t\t<picture>\n\t\t\t<source width=\"410\" media=\"(prefers-color-scheme: dark)\" srcset=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver2&image=dark\">\n\t\t\t<source width=\"410\" media=\"(prefers-color-scheme: light)\" srcset=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver2&image\">\n\t\t\t<img width=\"410\" src=\"https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver2&image\" alt=\"Premium sponsor banner\">\n\t\t</picture>\n\t</a>\n</p>\n\n<p align=\"center\">\n\t<a href=\"https://github.com/sponsors/privatenumber\">\n\t\t<img src=\"https://cdn.jsdelivr.net/gh/privatenumber/sponsors/sponsorkit/sponsors.svg\">\n\t</a>\n</p>\n","publish_time":1697319460064,"maintainers":[{"name":"hirokiosame","email":"hiroki.osame@gmail.com"}],"_cnpm_publish_time":1697319460064,"dist-tags":{"latest":"2.1.2"}}