JSON.parse 在 gulp 任务上出错

JSON.parse gives error on gulp task

我有这个 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.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.parse 并且只是 运行

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

我没有收到任何错误,但代码未被解析为 json。我该怎么办?为什么 JSON.parse 在 gulpfile.js

中不起作用

感谢任何帮助

通过这样做解决了这个错误

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