npm Community Forum (Archive)

The npm community forum has been discontinued.

To discuss usage of npm, visit the GitHub Support Community.

npm link causes importing of package.json instead of index.js when the linked packages's parent folder contains another package.json

What I Wanted to Do

I have a library project and a client project that depends on that library package. For development, I’d like to use npm link to faster iterate on changes without having to publish my package for each change.

My project structures look like this:

Library Project:

Client Project:

The client project compiles fine when myLib is added as a regular dependency in myClient/package.json.

Then for development I link myLib to myClient project like this:

Now compiling the client project should still compile fine, but it doesn’t.

What Happened Instead

I get the following compile error:
"export 'SomeComponent' was not found in '@myorg/myLib'

Doing a console.log, I found out that the import actually does not import the myLib/package/lib/index.js content, but instead the myLib/package/package.json content :scream:. This really is weird and looks like a bug.

Now even weirder: When I delete myLib/package.json (NOT the one in myLib/package/package.json), suddenly the client project builds fine! It’s weird, as the link points to /path/to/myLib/package and not /path/to/myLib, and therefore the package.json in the parent folder should not modify anything. I can even replace the whole content of myLib/package.json with {} and it will still fail the build, just deleting the file resolves the problem.

Reproduction Steps

I’ll create a demo project when I get to it, I hope the project structure above helps to understand the problem for now.

Platform Info

$ npm --versions
{ npm: '6.4.1',
  ares: '1.14.0',
  cldr: '33.1',
  http_parser: '2.8.0',
  icu: '62.1',
  modules: '64',
  napi: '3',
  nghttp2: '1.34.0',
  node: '10.14.0',
  openssl: '1.1.0j',
  tz: '2018e',
  unicode: '11.0',
  uv: '1.23.2',
  v8: '',
  zlib: '1.2.11' }
$ node -p process.platform

My client project is a next.js project, it’s thinkable that it’s a bug in how its import works. I’d be happy for directions.

It could be that it‘s a webpack problem, similar to this one.

I created a webpack bug: