Webpack doesn't find a module that actually exists

Alex picture Alex · Jul 10, 2015 · Viewed 8k times · Source

I'm in trouble, I just did npm uninstall react-bootstrap and then npm install react-bootstrap and webpack cannot load this module anymore.

I launch webpack like this :

/var/www/cloud/node_modules/.bin/webpack --config webpack.config.js --watch --display-error-details

And here is the error, it just can't find the package.json inside react-bootstrap:

ERROR in ./core/static/core/js/modules/dashboard/admin/Customer.js
Module not found: Error: Cannot resolve module 'react-bootstrap' in /var/www/cloud/core/static/core/js/modules/dashboard/admin
resolve module react-bootstrap in /var/www/cloud/core/static/core/js/modules/dashboard/admin
  looking for modules in /var/www/cloud/node_modules
    resolve 'file' react-bootstrap in /var/www/cloud/node_modules
      resolve file
        /var/www/cloud/node_modules/react-bootstrap is not a file
        /var/www/cloud/node_modules/react-bootstrap.js doesn't exist
        /var/www/cloud/node_modules/react-bootstrap.jsx doesn't exist
    resolve 'file' or 'directory' /var/www/cloud/node_modules/react-bootstrap
      resolve file
        /var/www/cloud/node_modules/react-bootstrap is not a file
        /var/www/cloud/node_modules/react-bootstrap.js doesn't exist
        /var/www/cloud/node_modules/react-bootstrap.jsx doesn't exist
      resolve directory
        use lib/index.js from package.json
          resolve 'file' or 'directory' lib/index.js in /var/www/cloud/node_modules/react-bootstrap
            resolve file
              /var/www/cloud/node_modules/react-bootstrap/lib/index.js doesn't exist
              /var/www/cloud/node_modules/react-bootstrap/lib/index.js.js doesn't exist
              /var/www/cloud/node_modules/react-bootstrap/lib/index.js.jsx doesn't exist
            resolve directory
              /var/www/cloud/node_modules/react-bootstrap/lib/index.js/package.json doesn't exist (directory description file)
              /var/www/cloud/node_modules/react-bootstrap/lib/index.js doesn't exist (directory default file)
        directory default file index
          resolve file index in /var/www/cloud/node_modules/react-bootstrap
            /var/www/cloud/node_modules/react-bootstrap/index doesn't exist
            /var/www/cloud/node_modules/react-bootstrap/index.js doesn't exist
            /var/www/cloud/node_modules/react-bootstrap/index.jsx doesn't exist

That's really odd, my package.json looks like :

{
  "name": "cloud",
  "version": "1.0.0",
  "description": "hehe",
  "main": "gulp.js",
  "scripts": {
    "dev": "webpack-dev-server --devtool eval --progress --colors --content-base build"
  },
  "repository": {
    "type": "git",
    "url": ""
  },
  "author": "",
  "license": "",
    "dependencies": {
    "anima": "^0.4.0",
    "immutable": "^3.7.4",
    "imports-loader": "^0.6.4",
    "moment": "^2.10.3",
    "react-bootstrap": "^0.23.7",
    "react-google-recaptcha": "^0.3.2",
    "react-paginate": "^0.1.31",
    "script-loader": "^0.6.1",
    "underscore": "^1.8.3"
  },
  "devDependencies": {
    "babel": "^5.5.6",
    "babel-core": "^5.5.6",
    "babel-loader": "^5.1.4",
    "css-loader": "^0.15.1",
    "node-libs-browser": "^0.5.2",
    "react-hot-loader": "^1.2.7",
    "style-loader": "^0.12.3",
    "webpack": "^1.9.10",
    "webpack-bundle-tracker": "0.0.51"
  }
}

And my webpack config :

var path = require("path");
var webpack = require('webpack');
var BundleTracker = require('webpack-bundle-tracker'); 
var bower_dir = __dirname + '/components/bower_components/';
var node_modules_dir = path.resolve(__dirname, 'node_modules');

module.exports = {
    context: __dirname,
    entry: {
        app: [ './core/static/core/js/main.js', ],
              vendor: ['react', 'react-router', 'react-bootstrap', 'react-google-recaptcha', 'react-spinkit'],  
    },    
    output: {
        path: path.resolve('./static/bundles/'),
        filename: 'bundle.js',
        publicPath:''           
    },  
    externals: {
        // require("jquery") is external and available
        //  on the global var jQuery
        "jquery": "jQuery",
        "utils": "utils"
    }, 
    plugins: [new BundleTracker({filename: './webpack-stats.json'}),
              new webpack.optimize.CommonsChunkPlugin(/* chunkName= */"vendor", /* filename= */"vendor.bundle.js"),   
              /*new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery"
        })*/],
              module: {
                  loaders: [{ test: /\.jsx?$/, exclude: node_modules_dir, loaders: ['babel']}, // to transform JSX into JS
                            { test: require.resolve("jquery"), loader: "imports?jQuery=jquery" },
                            { test: /\.json$/, loader: "json" },
                            { test: /\.css$/, loader: "style-loader!css-loader?root=." }],
              },
              resolve: {
                  modulesDirectories: ['node_modules', 'bower_components'],
                  extensions: ['', '.js', '.jsx']
              },
};

Plus, the folder react-bootstrap do exists in my node_modules.

My tree is like this :

webpack.config.js
node_modules
package.json

I'm totally lost...

Answer

qiu picture qiu · Aug 13, 2020

It's worth noting that webpack stores a cache at node_modules/.cache/terser-webpack-plugin. I'd try deleting that and re-running webpack. This often fixes bundle issues for me, so much so that I include it in a "clean" script I call via npm run clean.