webpack-cli: webpack-dev-server doesn't work with installed webpack and webpack-cli in the parent folder

Bug report

Actual Behavior

Installation of the webpack-dev-server in the child-app folder causes installation of the webpack in same folder as it depends on it, even the webpack is already installed in the parent-app folder.

If dependency is not found in the child-app, the node will search parent folder automatically as stated here: modules_loading_from_node_modules_folders

On the other hand, installation of the webpack-cli (also installed in parent-app) is not triggered.

In the child-app, commands ./node_modules/.bin/webpack serve --mode development ../node_modules/.bin/webpack serve --mode development ending with same result (I would to point, that webpack-dev-server is already installed):

[webpack-cli] For using 'serve' command you need to install: 'webpack-dev-server' package.
[webpack-cli] Would you like to install 'webpack-dev-server' package? (That will run 'npm install -D webpack-dev-server') (Y/n)

After confirm that, we get:

up to date, audited 277 packages in 1s

33 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
[webpack-cli] You need to install 'webpack-dev-server' for running 'webpack serve'.
Error: Cannot find module 'webpack-dev-server'
Require stack:
- /usr/src/app/node_modules/@webpack-cli/serve/lib/index.js
- /usr/src/app/node_modules/webpack-cli/lib/webpack-cli.js
- /usr/src/app/node_modules/webpack-cli/lib/bootstrap.js
- /usr/src/app/node_modules/webpack-cli/bin/cli.js
- /usr/src/app/child-app/node_modules/webpack/bin/webpack.js

To solve this problem, I need to install webpack-cli in the child-app folder npm i webpack-cli once finished, the ./node_modules/.bin/webpack serve --mode development works as expected.

Another solution without installing the webpack-cli is to set the NODE_PATH, so command NODE_PATH="$NODE_PATH:/usr/src/app/child-app/node_modules" ./node_modules/.bin/webpack serve --mode development will serve as expected also.

Expected Behavior

I think that webpack and webpack-cli should not be installed in the child-app node_modules at all, but they should by used from the parent folder.

Also installation of the webpack-dev-server should not install webpack dependency automatically, the responsibility lies on developer (same for the webpack-cli what is correct). In fact, after inspecting both node_module folders, I saw plenty intersections. And I don’t understand why we need these duplicates if they are available in the parent’s node_module / NODE_PATH.

How Do We Reproduce?

  1. do folder called parent-app with this structure where package.json files are default (generated by npm init)
parent-app
├── child-app
│   ├── package.json
│   ├── public
│   │   └── index.html
│   └── src
│       └── index.js
└── package.json

content of index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  </head>
  <body>
    <div id="rootNode" />
    <script src="/main.js"></script>
  </body>
</html>

content of index.js:

const rootNode = document.getElementById("rootNode");
rootNode.innerHTML = "<div>This is the content rendered by js</div>";
  1. in the parent-app install webpack and webpack-cli and then in the child-app install webpack-dev-server It is handy to use docker environment upon the folder: docker run -it --rm -v $(pwd):/usr/src/app -w /usr/src/app -p 8080:8080 node:latest /bin/bash

Results of npx webpack-cli info

System: OS: Linux 5.15 Debian GNU/Linux 11 (bullseye) 11 (bullseye) CPU: (8) x64 Intel® Core™ i9-9880H CPU @ 2.30GHz Memory: 18.01 GB / 19.55 GB Binaries: Node: 20.2.0 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 9.6.6 - /usr/local/bin/npm Packages parent-app: webpack: ^5.85.0 => 5.85.0 webpack-cli: ^5.1.1 => 5.1.1 Packages child-app: webpack-dev-server: ^4.15.0 => 4.15.0

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (3 by maintainers)

Most upvoted comments

Ah, I see the whole picture now … because I’m running webpack which is in the parent, it doesn’t know about the webpack-dev-server. Sorry it is quite obvious.

Well, I would to point that on my local NODE_PATH is empty by default and to have webpack-dev-server in child and the rest in parent I need to locate both node_modules folders.

NODE_PATH="$NODE_PATH:/Users/*****/my/development/parent-app/child-app/node_modules:/Users/*****/my/development/parent-app/node_modules" npm run serve:dev

for reference.