Local package fails to find package installed in the project

(Thomas Deinhamer) #1

I’ve got a project which defines a local dependency via link:../localpackage/ in the package.json file and a devDependency on webpack. The local package localpackage has a peer dependency on webpack but does not define any dependency or devDependency on webpack.

When running npm install in the project, it creates a symlink to localpackage in the node_modules directory and it also installs webpack into the node_modules directory.

Yet, when executing npm start in the project, which runs a script, which uses localpackage, I get the following error:

Error: Cannot find module 'webpack'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    ...

Why can’t it find the webpack package although it’s properly installed? Does that mean local packages can not resolve peer dependencies within a project due to the symlink it creates for linking the local package? Or is this a bug?

I’m running Window 10 using Node 10.15.3 and npm 6.9.0.

Thanks a lot, I really appreciate your help on this!

(John Gee) #2

What does npm start actually do? (i.e. what is the start script?)

And you said it runs a script which uses localpackage. If that is another run-script, what does it do?

(Thomas Deinhamer) #3

npm start just runs webpack --mode production actually, which loads the project’s webpack.config.js file, which imports modules from localpackage; and localpackage itself needs some webpack functionality and therefor defines webpack as a peer dependency.

1 Like
(John Gee) #4

Thanks, I think I understand the problem now, but don’t have a followup suggestion! Hopefully is familiar to another reader.

(Thomas Deinhamer) #5

Thanks for taking time to help me anyway. To be more clear, just for other readers too; installing webpack inside the localpackage using npm install webpack solves the problem i.e. the error goes away; but that’s no solution because I don’t need a dependency of webpack in the localpackage pacage; that’s why it’s defined as a peer dependency.

Also, stating that the error went away also means that there’s a new error, complaining about the absence of another peer dependency; as I have defined multiple peer dependencies.

(Lars Willighagen) #6

I don’t think symlinked dependencies can have shared instances of dependencies, so peer dependencies won’t work either. That’s because of how the Node.js module resolution works: if the required thing isn’t a file, it starts looking in the node_modules directories in any directory up to root. However, if the symlinked dependency is in a sibling directory of the package where webpack is installed, it won’t ever reach that node_modules.

.
├── a
│   ├── index.js
│   └── package.json
└── b
    ├── node_modules
    │   └── a -> ../../a
    ├── package.json
    └── package-lock.json

So you’d either want localpackage to have a direct (dev) dependency on webpack, although that won’t work if it needs to be the same Webpack instance; or you need to make sure localpackage is copied, by publishing it to a registry or packing it and installing the local tar file. I understand that neither options are great, but I don’t think there’s a different workaround.

1 Like
(system) closed #7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.