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

cli
triaged

(Fabian) #1

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:

  • myLib/
    • package.json - the package file that contains all the dev dependencies needed to build the project, build output will be put into package/lib/
    • src/ - source files
    • package/
      • package.json - this is the package file that gets published, it just contains peerDependencies and the package name @myorg/myLib, and "main": "lib/index.js"
      • lib/ - contains the built code
        • index.js - the built code
        • more folders that contain files that are required by index.js

Client Project:

  • myClient/
    • package.json - contains the dependency on myLib
    • src/ - source files etc.
      • someSourceFile.js - contains an import of a named export, like import {SomeComponent} from '@myorg/myLib'
    • other folders

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:

  • cd /path/to/myLib
  • npm run build - builds the client project and puts the result in myLib/package/lib
  • cd package
  • npm link
  • cd /path/to/myClient
  • npm link @myorg/myLib

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: '6.8.275.32-node.36',
  zlib: '1.2.11' }
$ node -p process.platform
darwin

(Fabian) #2

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.


(Fabian) #3

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


(Fabian) #4

I created a webpack bug: https://github.com/webpack/webpack/issues/8673