2015-10-14 78 views
1

我正在使用grunt作为“JS Task Runner”。
节点安装在“C:/ Program Files”中。
NPM安装在C:/ users/Peterson/appdata/roaming/npm中。
npm文件夹内的Grunt,Bower和Grunt-cli。使用grunt进行自定义项目

创建项目 - d:/项目A.
内d以下文件:/项目A - “的package.json”gruntfile.js

的package.json

{ 
    "name": "Test-Project", 
    "version": "0.1.0", 
    "devDependencies": { 
    "grunt": "~0.4.1", 
    "grunt-contrib-concat": "~0.1.3" 
    } 
} 

gruntfile.js

module.exports = function(grunt) { 

// Project configuration. 
grunt.initConfig({ 

    //Read the package.json (optional) 
    pkg: grunt.file.readJSON('package.json'), 

    // Metadata. 
    meta: { 
     basePath: '../', 
     srcPath: '../src/', 
     deployPath: '../deploy/' 
    }, 

    banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' + 
      '<%= grunt.template.today("yyyy-mm-dd") %>\n' + 
      '* Copyright (c) <%= grunt.template.today("yyyy") %> ', 

    // Task configuration. 
    concat: { 
     options: { 
      stripBanners: true 
     }, 
     dist: { 
      src: ['<%= meta.srcPath %>scripts/fileone.js', '<%= meta.srcPath %>scripts/filetwo.js'], 
      dest: '<%= meta.deployPath %>scripts/app.js' 
     } 
    } 
}); 

// These plugins provide necessary tasks. 
grunt.loadNpmTasks('grunt-contrib-concat'); 

// Default task 
grunt.registerTask('default', ['concat']); 

}; 

在CLI d:/项目A>咕噜

Fatal Error: Unable to find local grunt 

有人能帮忙吗?

+1

指定的是否在'D:/ Project A'文件夹中运行'npm install'? – topheman

+0

不,我没有。我在安装节点js时运行它,所以它安装在C:/ users/Peterson/appdata/roaming/npm – Deadpool

+0

我需要在'D:/ projectA'目录中再次运行npm吗?如果是这样,为什么? – Deadpool

回答

2

您已经安装节点/ NPM和咕噜&鲍尔(通过npm install -g全球安装他们,我假设)的CLIS

什么你缺少的是在你的项目的根目录运行npm install(您package.json文件是),安装你的项目依赖关系package.json

+0

你说得对。你的答案奏效。但是,我无法理解,当我在全球安装npm时,为什么我需要再次将它安装到我的项目文件夹中亲爱的? – Deadpool

+1

您不是在本地安装npm。您正在根据您的package.json文件安装grunt和grunt插件grunt-contrib-concat的本地副本。随着项目的发展,将来会有更多的依赖性。 http://gruntjs.com/plugins –