Search code examples
javascriptnode.jswebpackwebpack-dev-serverentry-point

Overriding Webpack 4 Entry Point


Here's the directory of my project:

   -node_modules
   -src
       -client
          -js
          -styles
          -views
          -index.js
          -webpack.config.js
       -server
   -.babelrc
   -package
   -package-lock
   -README.md
   -webpack.dev
   -webpack.prod

Here's the content of my webpack.config.js

const path = require('path')
const webpack = require('webpack')

module.exports = {
    mode: 'none',
    //entry: path.resolve(__dirname) + './src/client/index.js',

    module: {
        rules: [
            {
                test: '/\.js$./',
                exclude: /node_modules/,
                loader: "babel-loader"
            }
        ]
    }
}

I want to override the default entry point inside webpack.config.js to point to the index.js file in the same level.

By default, if I have the index.js file inside the src directory, without specifying the entry point, it works as expected.

I have tried changing the entry points like this:

entry: path.resolve(__dirname, './src/client/index.js'),

entry: path.resolve(__dirname, 'src') + 'client/index.js',

entry: path.resolve(__dirname, './src/index.js'),

entry: path.resolve(__dirname) + '/src/client/index.js',

entry: path.resolve(__dirname) + './src/client/index.js'

I'm using a windows 10 Operating System.


Solution

  • module.exports = {
      entry: './index.js'
    };
    

    Ref: https://webpack.js.org/concepts/entry-points/


    Alternatively, you can also use

    module.exports = {
      entry: require.resolve('./index')
    };
    

    Ref: https://nodejs.org/api/modules.html#modules_require_resolve_request_options