grunt requirejs 'define is undefined'

16,748

Solution 1

As define is a requireJs function it seems you miss to load requireJs or any other AMD loader. If you dont need to load any other AMD module then your complied once, you can use a light weight loader shim like almond.

Solution 2

Adding the require.js file as an "include" option should work.

requirejs: {
    compile : {
        options : {
            name  : 'main',
            baseUrl : ".",
            mainConfigFile : "./main.js",
            out : "./optimized.js",
            preserveLicenseComments: false,
            include: ['path/to/require.js']
        }
    }
}
Share:
16,748

Related videos on Youtube

Otskimanot Sqilal
Author by

Otskimanot Sqilal

omw learning python and django

Updated on September 20, 2022

Comments

  • Otskimanot Sqilal
    Otskimanot Sqilal over 1 year

    I'm trying to optimize RequireJS using GruntJS, using the grunt-contrib-requirejs plugin.

    The problem is my code works fine before optimizing it, and then after optimizing it, on the console it says Uncaught ReferenceError: define is not defined.

    Here's the Gruntfile.js

    module.exports = function (grunt) {
      grunt.loadNpmTasks('grunt-contrib-requirejs');
    
      grunt.initConfig({
        requirejs: {
            compile : {
                options : {
                  name  : 'main',
                  baseUrl : ".",
                  mainConfigFile : "./main.js",
                  out : "./optimized.js",
                  preserveLicenseComments: false
               }
            }
    }
      })
    
      grunt.registerTask('default', 'requirejs');
    
    }
    
    • Andreas Köberle
      Andreas Köberle about 11 years
      ok will add this as an answer too.
    • streetlight
      streetlight over 10 years
      @OtskimanotSqilal how did you include it? Did you add it as a seperate script tag or put the minified script in data-main?
  • hybrid9
    hybrid9 almost 10 years
    I'm having a similar issue, but wouldn't that just add requirejs to the output? It doesn't seem like it goes through the optimizer.
  • Thomas Higginbotham
    Thomas Higginbotham almost 10 years
    Yes, it does add RequireJS to the output. It's a simple solution to the problem, but using a smaller AMD loader in the minified script (see accepted answer) would be more ideal.