scrblnrd3
scrblnrd3

Reputation: 7426

Using grunt on a large Javascript project with multiple subdirectories

I have a Javascript project with multiple subdirectories, each its own individual project. I could just use one massive Gruntfile with different tasks for each project, but I'd rather have a Gruntfile in each subfolder. A typical file structure would be this

main_folder/
    project_1
        src
            js/
        dist/
        doc/
        Gruntfile.js
        package.json
    package.json
    node_modules/

And then repeat the file structure for each project

Here is my Gruntfile inside project_1

module.exports = function(grunt) {



    // Project configuration.
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        uglify: {
            options: {
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
            },
            build: {
                src: 'src/<%= pkg.name %>.js',
                dest: 'build/<%= pkg.name %>.min.js'
            }
        }
    });

    // Load the plugin that provides the "uglify" task.
    grunt.loadNpmTasks('grunt-contrib-uglify');


    grunt.registerTask('default', ['uglify']);

};

It has fewer tasks than I will use, but the errors are still there. Whenever I try to run grunt, I get the error message Local Npm module "grunt-contrib-uglify" not found. Is it installed?, even though I have it installed in node_modules.

How can I specify the location of node_modules, or do I have to reinstall node_modules inside each folder?

Upvotes: 2

Views: 386

Answers (2)

Yannick Block
Yannick Block

Reputation: 51

Another good way to solve this might be the use of subgrunt. It let's you configure various targets for your subprojects and has the option npmInstall, which "Determines wether npm install will be ran for the sub-project (thus installing dev dependencies)."

Upvotes: 0

Andrew Miner
Andrew Miner

Reputation: 6175

I think you can still keep everything in the same place if you tell Grunt more explicitly where to find your plugins. For example, here's a bit from one of my Gruntfiles:

grunt.loadTasks tasks for tasks in grunt.file.expand '../node_modules/grunt-*/tasks'

As a side note: I faced exactly the same choice you did, but made the opposite choice: a single Gruntfile for the whole project and created a symlink to it from each project sub-directory. It's turned out to be a very easy way to keep things together in one place, and side-steps a lot of confusing issues like the one you're facing.

Upvotes: 3

Related Questions