Trying to build AMD modules with Webpack — getting "Cannot resolve module 'exports'" error
<p>I am trying to introduce Webpack in a project that has been using AMD modules with Require.js. The relevant part of the project structure is this:</p>

<p><a href="https://i.stack.imgur.com/7aaT2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7aaT2.png" alt="enter image description here"></a></p>

<p>(i.e. the project root folder contains a <code>js</code> folder with all the application scripts, whose entry point is main.js, and also a <code>tools</code> folder with <code>npm_modules</code> and the gulp tasks that I am building)</p>

<p><em>(and yeah, I know that it would be simpler to have <code>npm_modules</code> and <code>gulpfile</code> in the root of the project, but that's the structure that I have)</em></p>

<p>So I am trying to build a js bundle out of the AMD modules into which the app is divided. To simplify, let’s say the main.js file has the following code:</p>

<pre class="lang-js prettyprint-override"><code>define([
    'jquery'
], function (
    $
) {
    'use strict';

    console.log($);
});
</code></pre>

<p>which means that I need to import <code>jquery</code> from <code>./js/libs/vendor/jquery/dist/jquery.js</code></p>

<p>So here is the webpack config (<code>webpack.js</code> file on the tree diagram) that I wrote:</p>

<pre class="lang-js prettyprint-override"><code>var path = require('path');

var appModulesPath = path.resolve('../js'); // gulpfile’s process is run in ./tools folder
var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
console.log(appModulesPath, ' and ', nodeModulesPath); // I confirm that paths are correct

module.exports = {
    context: path.join(__dirname, '../../'),
    debug: false,
    resolve: {
        root: [appModulesPath, nodeModulesPath],
        alias: {
            'jquery': 'libs/vendors/jquery/dist/jquery',
        },
        extensions: ['.js']
    },
    entry: ['./js/main'],
    output: {
        path: path.join(__dirname, '../../js'),
        filename: 'bundle.js'
    },
    module: {
        loaders: [
            {test: /jquery/, loader: 'exports?$'},
        ]
    }
};
</code></pre>

<p>but when I try to run the task I get the following error:</p>

<pre><code>ERROR in ../js/main.js
Module not found: Error: Cannot resolve module 'exports' in /path/to/project/root/folder/js
 @ ../js/main.js 1:0-9:2
</code></pre>

<p>If I understand the error correctly, Webpack tries and fails to use the <code>exports-loader</code>. But I have it installed, and it sits in the <code>node_modules</code> folder. Is it that Webpack doesn't know where to look for <code>node_modules</code>? But I am resolving the root in the config, and when I log out the paths that I pass to <code>resolve.root</code>, they are: <code>/path/to/project/root/js</code> and <code>/path/to/project/root/tools/node_modules</code>. Why doesn’t Webpack pick <code>exports-loader</code> up?</p>

<p>I am out of ideas. Could you please help?  </p>