您可以。我不知道为什么任何人都需要做这个,因为目前Grunt是一个 命令行工具 。 警告:我不建议以这种方式运行Grunt。 但是这里是:
var grunt = require('grunt');// hack to avoid loading a Gruntfile// You can skip this and just use a Gruntfile insteadgrunt.task.init = function() {};// Init configgrunt.initConfig({ jshint: { all: ['index.js'] }});// Register your own tasksgrunt.registerTask('mytask', function() { grunt.log.write('Ran my task.');});// Load tasks from npmgrunt.loadNpmTasks('grunt-contrib-jshint');// Finally run the tasks, with options and a callback when we're donegrunt.tasks(['mytask', 'jshint'], {}, function() { grunt.log.ok('Done running tasks.');});


