20

Is there a way to restrict npm link to create the symlink only to point at ./dist?

I am struggling so far as I want to test my library (react component) locally before publishing.

In my package.json I have included the main (entry point):

"main": "./dist/index.js"

But still when I run npm link it links to the parent folder which has node_modules and so on. This is causing problems cause now I have 2 react apps installed.

I thought of a way to exclude node_modules but I couldn't even find a way to do that.

Ali Elkhateeb
  • 3,413
  • 4
  • 21
  • 39

2 Answers2

10

This is actually simpler than expected. First run ng build to build your library project, then go under the dist folder and run npm link from there instead of the library root project. The symlink will be created pointing to the dist folder instead.

velval
  • 3,072
  • 36
  • 45
-2

Using "main" parameter doesn't make sense because npm link use 'npm prefix' for create symlink see here

You can use next hack:

  1. Cd to project directory
  2. Copy package.json from package to ./dist/package.json directory
  3. Cd to dist directory
  4. Run 'npm link'

It looks dirty but seem to only way to do it with npm link

  • Well yeah I didn't wanna go down that road :D Thank you tho – Ali Elkhateeb Dec 23 '19 at 02:24
  • if you can use additional tools then you may try using [lerna link](https://github.com/lerna/lerna/tree/master/commands/link) You necessary to add in package.json (in root level) next lines: "publishConfig": { "directory": "dist" } – name212 Dec 23 '19 at 08:41
  • Beyond "looking dirty," it won't work in anything but the most basic projects. Any sort of relative import path, for example, will explode. – Zane Claes Nov 02 '20 at 01:23