JSON.parse在gulp任务中给出错误

JSON.parse在gulp任务中给出错误,json,angularjs,gulp,Json,Angularjs,Gulp,我有这个任务 gulp.task('replace', function () { // Get the environment from the command line var env = args.env || 'localdev'; // Read the settings from the right file var filename = env + '.json'; console.log(filename) var settings = JSON.

我有这个任务

gulp.task('replace', function () {  
  // Get the environment from the command line
  var env = args.env || 'localdev';

  // Read the settings from the right file
  var filename = env + '.json';
  console.log(filename)

  var settings = JSON.parse(fs.readFileSync('./config/' + filename, 'utf8'));

// Replace each placeholder with the correct value for the variable.  
  gulp.src('./js/constants.js')  
    .pipe(replace({
      patterns: [
        {
          match: 'apiUrl',
          replacement: settings.apiUrl
        }
      ]
    }))
  .pipe(gulp.dest('./js'));
});
当我执行代码时,总是会出现这个错误

SyntaxError: Unexpected token /
  at Object.parse (native)
  at Gulp.<anonymous> (/home/k1ngsley/Projects/loanstreet-rea-app/gulpfile.js:86:23)
  at module.exports (/home/k1ngsley/Projects/loanstreet-rea-app/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)
  at Gulp.Orchestrator._runTask (/home/k1ngsley/Projects/loanstreet-rea-app/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
  at Gulp.Orchestrator._runStep (/home/k1ngsley/Projects/loanstreet-rea-app/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
  at Gulp.Orchestrator.start (/home/k1ngsley/Projects/loanstreet-rea-app/node_modules/gulp/node_modules/orchestrator/index.js:134:8)
  at /usr/lib/node_modules/gulp/bin/gulp.js:129:20
  at process._tickCallback (node.js:448:13)
  at Function.Module.runMain (module.js:499:11)
  at startup (node.js:119:16)
  at node.js:935:3
我没有得到任何错误,但是代码没有被解析为json。我该怎么办?为什么JSON.parse在gulpfile.js中不起作用


感谢任何帮助

通过这样做解决了此错误

var setting = fs.readFileSync('./config/' + filename, 'utf8');
 var settings = JSON.parse(JSON.stringify(setting));

这个错误还不够明确吗?解析配置文件时出现解析错误,否?文件的内容是什么?我在
SyntaxError:Unexpected token
中遇到了类似的错误。这是一种对shenanigan进行的空白编码,所以我使用了
JSON.stringify(setting.replace(/\s+/g,”)
)并成功了。@Josh这正是我的问题所在,有多奇怪……你能解释为什么会发生这种情况吗?有些空白字符可能不会出现在编辑器中,但不是有效的JSON。
/\s+/g,“
替换将它们全部转换为空格(在JSON中有效)。
var setting = fs.readFileSync('./config/' + filename, 'utf8');
 var settings = JSON.parse(JSON.stringify(setting));