【问题标题】:Using grunt for a custom Project将 grunt 用于自定义项目
【发布时间】:2016-01-12 07:13:49
【问题描述】:

我使用 Grunt 作为“JS Task Runner”。
节点安装在“C:/Program Files”中。
NPM 安装在 C:/users/Peterson/appdata/roaming/npm 中。
npm 文件夹中的 Grunt、Bower 和 Grunt-cli。

创建一个项目 - D:/Project A.
D:/ProjectA 中的以下文件 - "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:/ProjectA> 咕噜声

Fatal Error: Unable to find local grunt

有人可以帮忙吗?

【问题讨论】:

  • 您是否在D:/Project A 文件夹中运行了npm install
  • 不,我没有。我在安装 node js 时运行它,所以它安装在 C:/users/Peterson/appdata/roaming/npm
  • 我需要在 'D:/projectA' 目录中再次运行 npm 吗?如果是,为什么?

标签: javascript json node.js gruntjs npm


【解决方案1】:

您已经安装了 node/npm 和 Grunt & Bower 的 cli(我假设通过 npm install -g 全局安装它们)

您缺少的是在项目的根目录(您的package.json 文件所在的位置)运行npm install,以安装package.json 中指定的项目依赖项 p>

【讨论】:

  • 你是对的。你的回答奏效了。但是,我无法理解,当我在全局安装 npm 后,为什么我需要在我的项目文件夹中再次安装它呢?
  • 您没有在本地安装 npm。您正在根据您的 package.json 文件安装 grunt 和 grunt 插件 grunt-contrib-concat 的本地副本。随着项目的发展,将来会有更多的依赖关系。 gruntjs.com/plugins
猜你喜欢
  • 1970-01-01
  • 2014-04-17
  • 2023-03-26
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多