2013-06-28 121 views
3

我開始探索Grunt。閱讀幾個網站,我看到了一些例子。好吧,我創建的package.json和Gruntfile.js:發出命令時出錯Grunt

的package.json http://pastebin.com/Kkt8fuXJ

Gruntfile.j http://pastebin.com/LnSmTzXZ

我的項目的文件夾的組織:

root (index.html) 
-src (where is package.json and Gruntfile.js) 
-lib 
--css 
    ---min 
--js 
--img 

當我給的指揮咕嚕聲總是發生這樣的錯誤:

Loading "Gruntfile.js" tasks...ERROR 
>> Error: Unable to parse "package.json" file (Unexpected token }). 
Warning: Task "default" not found. Use --force to continue. 

Aborted due to warnings. 

如果我刪除PKG:grunt.file.readJSON( '的package.json'),:

 grunt.loadNpmTasks('grunt-contrib-imagemin'); 
     ^^^^^ 
Loading "Gruntfile.js" tasks...ERROR 
>> SyntaxError: Unexpected identifier 
Warning: Task "default" not found. Use --force to continue. 

Aborted due to warnings. 

有什麼不對?

謝謝

回答

5

第一個錯誤是由您的package.json不是有效的JSON引起的。至於我可以看到你使用的是不必要的逗號在一行的末尾9.有效的JSON會是這樣:

{ 
    "name": "EREBD", 
    "description": "Site do evento EREBD XVII", 
    "version": "1.0.0", 
    "main": "Gruntfile.js", 
    "dependencies": { 
    "grunt": "~0.4.1", 
    "grunt-contrib-cssmin": "~0.6.1", 
    "grunt-contrib-imagemin": "~0.1.4" //<- no comma there 
    } 
} 

但是,你甚至不需要到Gruntfile因爲內導入的package.json反正你沒有使用它的任何屬性。那麼你的Gruntfile有什麼問題?那麼你正在加載你的npm任務,並在導出的函數外定義你的grunt任務。這是它看起來像正確的方式:

module.exports = function(grunt) { 
    grunt.initConfig({ 
     // image optimization 
     imagemin: { 
      dist: { 
       options: { 
        optimizationLevel: 4, 
        progressive: true 
       }, 
       files: [{ 
        expand: true, 
        cwd: '../lib/img/', 
             src: '**/*', 
        dest: '../lib/img/' 
       }] 
      } 
     }, 
     // minify css 
     cssmin: { 
       minify: { 
        expand: true, 
        src: ['../lib/css/*.css'], 
        dest: '../lib/css/min/', 
        ext: '.min.css' 
       } 
     } 
    }); 

    // load tasks 
    grunt.loadNpmTasks('grunt-contrib-imagemin'); 
    grunt.loadNpmTasks('grunt-contrib-cssmin'); 

    // extra tasks 

    // register task 
    grunt.registerTask('default', [ 
     'imagemin', 
     'cssmin' 
    ]); 
}; 
+1

忘記逗號是一個錯誤。 感謝您的幫助。我改變的package.json和Gruntfile.js但我仍然得到一個錯誤: 'C:\用戶\費利佩\桌面\測試的\ src \ Gruntfile.js:27 ); ^ 加載 「Gruntfile.js」 任務...錯誤 >>語法錯誤:意外的令牌) 警告:任務 「cssmin」 找不到。使用--force繼續。 因警告而中止。「 – user1914644

+0

是的,解決方案它不適合我,我的Json文件做得很好 – Xvegas