12

It appears that when I run npm link, it will install the project globally, and it seems to install devDependencies with it.

Is there a way to run npm link without devDependencies, perhaps with the --only=production flag?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

3 Answers3

21

In npm@4.x or lower

When you run npm link in other_module then you will get both dependencies and devDependencies symlinked.

The --production flag doesn't change anything, still creates a symlink to the whole directory

In npm@5.1.0

They fixed it!

If you remove node_modules and then do npm link --only=production, it runs an install before symlinking, and therefore devDependencies folder are indeed excluded.

locropulenton
  • 4,743
  • 3
  • 32
  • 57
  • 8
    Is there any documentation on this available? The [npm link docu](https://docs.npmjs.com/cli/link.html) seems unsufficient… – Beat Dec 14 '18 at 07:23
  • 4
    Does this still work in version 6.0+? I tried doing `npm link --only=production` in my package, but then when looking into the global location (`/Users/me/.nvm/versions/node/v12.18.3/lib/node_modules/my-package`), I see a `node_modules` folder there that contains my dev dependencies like `react` inside. – Saad Nov 23 '20 at 03:22
5

This is currently not possible with npm link. The problem is, if you install only prod dependencies in that dependency, you're able to link it, but you're not able to develop on that dependency anymore (since missing devDependencies). And vice-versa: If you install devDependencies, you can't link anymore.

The solution: A package called npm-local-development at https://github.com/marcj/npm-local-development

It basically does the same thing as npm link, but works around the devDependency limitation by setting up a file watcher and syncs file changes automatically in the background, excluding all devDependencies/peerDependencies.

  1. You install npm-local-development: npm i -g npm-local-development
  2. You create file called .links.json in your root package.
  3. You write every package name with its local relative folder path into it like so

    { "@shared/core": "../../my-library-repo/packages/core" }

  4. Open a console and run npm-local-development in that root package. Let it run in the background.

Disclaimer: I'm the author of this free open-source project.

Marc J. Schmidt
  • 8,302
  • 4
  • 34
  • 33
2

A workaround I use is npm pack then point to the packed file in the example

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265