Webpack doesn’t seem to find module when absolute context path is not based off current directory

My folder structure looks like this:

project
    app
        public
            entry.coffee
    build
        webpack.config.js
    dist
    test

My webpack.config.js looks like this:

module.exports = {
    cache: true,
    context: path.resolve(__dirname + "/../app/public"),
    entry: "./entry.coffee",
...

I’m receiving this error:

ERROR in Entry module not found: Error: Cannot resolve file or directory ./entry.coffee in /usr/local/webprojects/webpack/app/public

but the file ‘/usr/local/webprojects/webpack/app/public/entry.coffee’ does exist.

Author: Fantashit

12 thoughts on “Webpack doesn’t seem to find module when absolute context path is not based off current directory

  1. I just ran into this using the CLI.

    $ webpack index.js

    is valid, but

    $ webpack index=index.js anotherPage=anotherPage.js

    fails with this error. The solution is to specify the full relative path:

    $ webpack index=./index.js anotherPage=./anotherPage.js
  2. My json is fine linted ok.
    I have the following structure
    ROOT/src/entry.js

    ROOT:webpack.config.js
    module.exports = {
    entry: ‘./src’,
    output: {
    path: ‘build’,
    filename: ‘bundle.js’,
    },
    };

    I tried ../src /src and src so I am a bit at a loss..anyone any ideas ? TY

    [SOLVED FOR ME] I need to actually state the entry

    entry: ‘./src/entry.js’,

  3. I had to ensure that I displayed the full extension e.g. ‘./MyComponent.jsx’ instead of ‘./MyComponent’

  4. For me this happened because in my entry object I wrote:

    app: path.resolve(__dirname, '/test.js'),
    

    While I should have written one of these:

    app: path.join(__dirname, '/test.js'),
    app: path.join(__dirname, 'test.js'),
    app: path.resolve(__dirname, 'test.js'),
    

    The problem was that path.resolve will ignore any parameters given to it before the last parameter that starts with a /.

    So path.resolve(__dirname, '/test.js'), will return /test.js instead of /path/of/your/project/directory/test.js.

  5. This happened to me, and this issue was that had the following:

        entry: {
    	'app/public/bundle.js': 'app/index.js',
    	'app/server.js': 'app/server.js'
        },
    
        output: {
    	path: path.resolve (__dirname, 'dist'),
    	filename: '[name]'
        },
    

    … instead of:

        entry: {
    	'app/public/bundle.js': './app/index.js',
    	'app/server.js': './app/server.js'
        },
        output: {
    	path: path.resolve (__dirname, 'dist'),
    	filename: '[name]'
        }
    

    See if you can even find the difference (and once you do, see if it should even matter… relative paths should be considered to be rooted at whatever “./” is by whatever is interpreting the relative path…… no?)

Comments are closed.