Search code examples
node.jsgruntjsconcatenationgrunt-contrib-concat

Issue using Grunt with grunt-contrib-concat with node require and module.exports


After successfully running grunt concat to concatnate all my node js files into a single file, I get an error when running the file using "$node bundle.js", saying that it cannot find module "./config.js"

In original file: var config = require('./config.js'); // do something with config.

In bundle file, it pastes config.js's contents into the bundle file, and then does "var config = require('./config.js);

But obviously this isn't going to be in the same location... isn't concat supposed to put all modules into the same file?

Am I using concat with grunt wrong?


Solution

  • You should not be concatenating Node files! This is not how node (and require()) works. We only concatenate JavaScript files intended for the browser in order to minimize page load time by reducing the number of HTTP requests.

    So the answer is: do not concatenate Node JS files!