74

I was trying to run webpack-4 first time

webpack ./src/js/app.js ./dist/app.bundle.js

it shows warning / error :

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/

ERROR in multi ./src/js/app.js ./dist/app.bundle.js
Module not found: Error: Can't resolve './dist/app.bundle.js' in 'D:\wamp64\www\webpack-4'
 @ multi ./src/js/app.js ./dist/app.bundle.js

Then i tried to change the mode

webpack --mode development

it shows :

ERROR in Entry module not found: Error: Can't resolve './src'
1

16 Answers 16

133

Resolved

Spent a lot of time to find out the solution.

Solution: Add index.js file into src folder.

That's it!.. solved :)


During Research, I found some facts about webpack 4 :

webpack 4 doesn’t need a configuration file by default!

webpack 4 there is no need to define the entry point: it will take ./src/index.js as the default!

3
  • 1
    what if you js file is at some location how would you define the path then or entry point ? Aug 3, 2018 at 7:24
  • 2
    @MertMetin In case you don't want to change your js file location you can always use this answer. stackoverflow.com/questions/45255190/… Aug 3, 2018 at 8:27
  • This solved my problem but I set my index file to be a typescript file. entry: path.resolve(process.cwd(), './src/index.ts') not sure why this is happening Jun 20, 2022 at 3:34
26

Met this problem when deploying on now.sh

Solution: Use Default Behavior

Move entry point to src/index.js.

This leverage webpack@4 default value for entry:

By default its value is ./src/index.js, but you can specify a different (or multiple entry points) by configuring the entry property in the webpack configuration.

Solution: Be Specific

As @Lokeh pointed out, if you don't want to change your JS file location you can always use path.resolve() in your webpack.config.js:

entry: path.resolve(__dirname, 'src') + '/path/to/your/file.js',
2
  • Your last solution helped me out, even with webpack reading custom .ejs files. Thanks
    – klewis
    Oct 18, 2019 at 13:22
  • 1
    I just built a new rails 6.1.3 app and there's no index.js anywhere
    – Mices
    May 1, 2021 at 2:16
6

Adding a context explicitly in webpack.config.js fixed issue for me. Adapt the following piece of code in your project:

context: __dirname + '/src',
entry: './index.js',
4

webpack version 4.46.0 Perhaps someone gets stuck during migration from webpack 4 to 5.

in case of multiple webpack config files and if anyone uses merge: Say webpack.common.js relies on some variables passed from cli eg:

module.export = (env) => {
  const {myCustomVar} = env;

  return {
    // some common webpack config that uses myCustomVar
  }
}

When you require common config in say webpack.prod.js:

const { merge } = require('webpack-merge'); // <-- `merge` is now named import if you are using > v5
const common = require('./webpack.common.js');

const getProdConfig = () => {....}

module.exports = (env) => {
  return merge(common(env), getProdConfig()); // <-- here, `call` common as its exported as a fn()
};

3
  • Seeing how to use the merge command in combination with the env parameters was very helpful. Thank you.
    – Onosa
    Mar 18, 2022 at 19:14
  • Thank you very very much, my dear friend! (Is actual for Webpack 5)
    – Eugene
    Jan 30, 2023 at 10:44
  • Great hint. Forgot I was exporting a function and then tried to merge the functions instead of their return value.
    – bgausden
    Aug 27, 2023 at 11:16
3
webpack ./src/js/app.js --output ./dist/app.bundle.js --mode development

This worked for me. I had the same trouble, it is because of a new version of webpack

2

I had a similar error and was able to resolve it with the command webpack src/index.js -o dist/bundle.js the -o did the trick. The issue wasn't the location of index.js it was missing the operator for defining the output path location.

See https://webpack.js.org/api/cli/

Version of webpack was 4.44.1

2

Just leaving this here, incase someone is not paying attention to the details like me, I had the same error, but because my webpack config file was named webpack.config instead on webpack.config.js, so my custom configurations were never picked and webpack was falling back to the defaults entry "src/index.js"

2

Other solutions didn't work. I solved this by adding this to package.json

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack" //this
  },

Then npm run build and it works. At first i've tried with npx webpack. Would love to know why it works.

1

In my case I used .tsx by mistake instead of .jsx. the project was in javascript not in typescript. using web pack for bundling tool. try using vite for avoiding this kind of situations.

0

As of webpack ^4.29.6 you don't need any configuration file so instead of giving path in package.json we need to write simply "build": "webpack" and keep index.js as entry point in src folder. However if you want to change entry point you can do so in webpack config file

0

For Rails 6 application this steps worked for me:

1) bundle exec rails webpacker:install system will reinstall webpacker but will rewrite few files:

  modified:   config/webpack/environment.js
  modified:   config/webpacker.yml
  modified:   package.json
  modified:   yarn.lock

2) Return configs to initial state:

git checkout config/webpack/environment.js

git checkout config/webpacker.yml

package.json and yarn.lock you can leave as they are

0

Spent a lot of time similarly to others to get around this annoying problem. Finally changed webpack.config.js as follows:-

output: {
  path: path.resolve(__dirname, './src'), //src instead of dist
  publicPath: '/src/', //src instead of dist
  filename: 'main.js' //main.js instead of build.js
}

...as Edouard Lopez and Sabir Hussain mentioned that you don't need to mention an entry point, removed that also and the app compiled after a long frustration.

0

So my problem, which I would wager is a lot of people's problem is that I set the entry path based on my whole app root. So in my case, it was /client/main.ts. But because my webpack.config.js file was actually inside /client, I had to move into that folder to run webpack. Therefore my entry was now looking for /client/client/main.ts.

So if you get this error you need to really look at your entry path and make sure it is right based on where you are running webpack and where your webpack.config.js file is. Your entry path needs to be relative to where you are running webpack. Not relative to your app root.

0

I had this problem when changing between React/Rails gems. Running rails webpacker:install restored me to the Rails webpacker defaults, but also overwrote all of my config files. Upon closer inspection, the culprit turned out to be my webpack/development.js file, which in a previous gem version had gotten modified from this Rails webpacker default:

process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')

module.exports = environment.toWebpackConfig()

Once I restored the file to those contents, this error went away. Specifically I had been missing the module.exports = environment.toWebpackConfig() line, which apparently is pretty important for those who want to avoid Rails webpacker thinking it needs a src/index.js file (it doesn't)

0

in my case I created an index.ts (TypeScript) file instead of index.js (JavaScript). Renaming to .js worked.

0

check again your working directory, you want to run npx webpack in the right folder.

i run into this problem when running npx webpack inside ../dist/ so i cd .. and then run npx webpack, it runs properly now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.