Warning: connect.static is not a function Use --force to continue

无人久伴 提交于 2019-11-30 17:15:36

You have to install connect and serve-static:

npm install --save-dev grunt-contrib-connect serve-static 

And then you have to import serve-static in Gruntfile.js:

module.exports = function (grunt) {
  ...
  var serveStatic = require('serve-static');

  grunt.initConfig({
  ...
    connect: {
    ...
      livereload: {
        options: {
          middleware: function(connect) {
            return [
              serveStatic('.tmp'),
              connect().use('/bower_components', serveStatic('./bower_components')),
              serveStatic(config.app)
            ];
          }
        }
      }

From version 0.11.x, the new grunt-contrib-connect does not support connect.static and connect.directory.
You should install serve-static(for serve static files) and serve-index (for Serves pages that contain directory listings for a given path).

like this:
var serveStatic = require('serve-static');
var serveIndex = require('serve-index');

Use serveStatic instead connect.static
and
serveIndex instead connect.directory

grunt.initConfig({
    connect: {
        options: {
            test: {
               directory: 'somePath',
               middleware: function(connect, options){
                    var _staticPath = path.resolve(options.directory);
                    return [serveStatic(_staticPath), serveIndex(_staticPath)]
               }
            }
        }
    }
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!