Here’s the fix that worked for me (and not mentioned in previous replies).
------ THE PROBLEM ------
There seemed to be an incompatible “version pairing” between Node and webpack-cli.
------ MY RESEARCH ------
As of 3/27/25, here are the version pairings that did and didn’t work for me.
…
Version pairing that DID NOT work:
Node: 10.9.0
webpack-cli: 6.0.1 (installed using: npm i -g webpack-cli)
The above version pairing produces the following error when running webpack-cli init.
webpack-cli init
[webpack-cli] Unknown command or entry ‘init’
[webpack-cli] Did you mean ‘info’ (alias ‘i’)?
[webpack-cli] Run ‘webpack --help’ to see available commands and options
…
Version pairing that works:
Node: 10.9.0
webpack-cli: 5.1.4 (installed using: npm i -g [email protected])
(List of webpack-cli versions: webpack-cli - npm)
…
MY RESULTS (using webpack-cli ver 5.1.4):
After running webpack-cli init for the first time, I am prompted with the following, to which I chose ‘Y’:
webpack-cli init
…
[webpack-cli] For using this command you need to install: ‘@webpack-cli/generators’ package.
[webpack-cli] Would you like to install ‘@webpack-cli/generators’ package? (That will run ‘npm install -D @webpack-cli/generators’) (Y/n) Y
This process added the following items to the root folder of my project:
- node_modules folder
- package-lock.json
- package,json
I ran webpack-cli init again, this time getting the expected config questions.
(The questions I got were a bit different than what we see in Mosh’s video…he used webpack-cli ver 2.0.14.)
The questions I got:
webpack-cli init
…
? Which of the following JS solutions do you want to use? ES6
? Do you want to use webpack-dev-server? No
? Do you want to simplify the creation of HTML files for your bundle? No
? Do you want to add PWA support? No
? Which of the following CSS solutions do you want to use? none
? Do you like to install prettier to format generated configuration? No
? Pick a package manager: npm
? Overwrite package.json? Y
? Overwrite src\index.js? N
? Overwrite index.html? N
…
[webpack-cli] Project has been initialised with webpack!
…
Next, Mosh’s video has us create and edit a package.json file, but this step was not necessary since running webpack-cli init the second time automatically created the package.json file, and put the necessary “build:” “webpack…” script in place.
(Side note: the only thing I added was the watch switch (-w) in the “build” script.)
Next, Mosh has us bundle the app by running npm run build. However, on first run I got the following error:
Error: Cannot find package ‘@babel/plugin-syntax-dynamic-import’ imported from…
Fortunately, I discovered I could bypass this error and finish building the bundle if, before running npm run build, I renamed the .babelrc file so as to “hide” it (located in the root folder of my project). Perhaps with the Babel version that webpack-cli downloaded, the .babelrc file is deprecated? I don’t know if hiding .babelrc is effective in all scenarios, but it worked in this case.
After renaming .babelrc, the build succeeded.
Finally, using Live Server (after repointing index.html to dist/main.js) the app successfully ran in the browser’s console.