【发布时间】:2016-08-09 05:32:56
【问题描述】:
我在我的 Gruntfile 中使用以下内容:
grunt.initConfig({
assets: grunt.option('assets'),
config: grunt.file.readJSON(path.join('<%= assets %>', 'config.json')) || grunt.file.readJSON('./defaults.json'),
...
})
当我执行它时,它会抛出:
>> Error: Unable to read "<%= assets %>/config.json" file (Error code: ENOENT).
>> at Object.util.error (/.../prj/node_modules/grunt-legacy-util/index.js:54:39)
>> at Object.file.read (/.../prj/node_modules/grunt/lib/grunt/file.js:247:22)
>> at Object.file.readJSON (/.../prj/node_modules/grunt/lib/grunt/file.js:253:18)
>> at Object.module.exports (/.../prj/Gruntfile.js:10:28)
>> at loadTask (/.../prj/node_modules/grunt/lib/grunt/task.js:325:10)
>> at Task.task.init (/.../prj/node_modules/grunt/lib/grunt/task.js:437:5)
>> at Object.grunt.tasks (/.../prj/node_modules/grunt/lib/grunt.js:120:8)
>> at Object.module.exports [as cli] (/.../prj/node_modules/grunt/lib/grunt/cli.js:38:9)
>> at Object.<anonymous> (/usr/local/lib/node_modules/grunt-cli/bin/grunt:45:20)
>> at Module._compile (module.js:425:26)
想知道这是否是因为assets var 在我尝试使用它时没有定义?还是不允许以这种方式使用 语法?
根据this answer,它看起来应该可以工作——我发现这是因为以前我只是使用var assets = grunt.option('assets'),但是那个出于某种原因抛出了SyntaxError: Unexpected token var。 (在我弄乱它之前,它看起来是这样的:)
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt)
var util = require('util'),
path = require('path'),
pkg = require('./package.json')
var assets = grunt.option('assets'),
var config = grunt.file.readJSON(path.join(assets, 'config.json')) || grunt.file.readJSON('./defaults.json')
grunt.initConfig({
...
})
像这样使用模块或在 gruntfile 中声明变量的正确的 grunt 方式是什么?和/或,我可以解决Unexpected token var 的问题吗?
(注意:这不是我无法加载的配置文件,而是来自grunt.option() 的资产路径似乎没有被解释)
【问题讨论】:
-
谁投了反对票,请说明你的理由。这是一个完全有效的编程问题 +1。
-
var assets = grunt.option('assets'), 后面多了一个逗号。删除它或删除下一行中的 var -
保持开放以奖励积分,但我履行了庄严的职责并投票结束。
标签: javascript gruntjs gruntfile