Error: Cannot find module html

前端 未结 5 637
情书的邮戳
情书的邮戳 2020-12-04 19:28

I have not used Node.js for a long time and never used express. When I started my application, it just returned :

Error: Cannot find module \'html\'
  at Fun         


        
相关标签:
5条回答
  • 2020-12-04 19:58

    This is what i did for rendering html files. And it solved the errors. Install consolidate and mustache by executing the below command in your project folder.

    $ sudo npm install consolidate mustache --save
    

    And make the following changes to your app.js file

    var engine = require('consolidate');
    
    app.set('views', __dirname + '/views');
    app.engine('html', engine.mustache);
    app.set('view engine', 'html');
    

    And now html pages will be rendered properly.

    0 讨论(0)
  • 2020-12-04 19:59

    I think you might need to declare a view engine.

    If you want to use a view/template engine:

    app.set('view engine', 'ejs');

    or

    app.set('view engine', 'jade');

    But to render plain-html, see this post: Render basic HTML view?.

    0 讨论(0)
  • 2020-12-04 20:01

    I am assuming that test.html is a static file.To render static files use the static middleware like so.

    app.use(express.static(path.join(__dirname, 'public')));
    

    This tells express to look for static files in the public directory of the application.

    Once you have specified this simply point your browser to the location of the file and it should display.

    If however you want to render the views then you have to use the appropriate renderer for it.The list of renderes is defined in consolidate.Once you have decided which library to use just install it.I use mustache so here is a snippet of my config file

    var engines = require('consolidate');
    
    app.set('views', __dirname + '/views');
    app.engine('html', engines.mustache);
    app.set('view engine', 'html');
    

    What this does is tell express to

    • look for files to render in views directory

    • Render the files using mustache

    • The extension of the file is .html(you can use .mustache too)

    0 讨论(0)
  • 2020-12-04 20:01

    Install ejs if it is not.

    npm install ejs
    

    Then after just paste below two lines in your main file. (like app.js, main.js)

    app.set('view engine', 'html');
    
    app.engine('html', require('ejs').renderFile);
    
    0 讨论(0)
  • 2020-12-04 20:03

    Simple way is to use the EJS template engine for serving .html files. Put this line right next to your view engine setup:

    app.engine('html', require('ejs').renderFile);
    
    0 讨论(0)
提交回复
热议问题