Error: Cannot find module 'ejs'

后端 未结 29 2036
南方客
南方客 2020-12-13 05:33

Here is my complete error:

Error: Cannot find module \'ejs\'
    at Function._resolveFilename (module.js:317:11)
    at Function._load (module.js:262:25)
            


        
相关标签:
29条回答
  • 2020-12-13 05:56

    kindly ensure that your dependencies in your package.json files are up to date. Try reinstalling them one at a time after also ensuring that your NPM is the latest version (up-to-date). It worked for me. I advise you to run npm install for the packages(thats what worked in my own case after it refused to work because I added the dependencies manually).

    0 讨论(0)
  • 2020-12-13 05:59

    After you've installed Express V x.x.x You need to choose an template view-engine. There are many really easy to learn. My personal go-to is EJS.

    Other really great and easy to learn could be:

    • Handlebars
    • PUG (Former Jade)

    To install EJS (And fix your error) Run in root of your project:

    npm install ejs
    

    Or if you're using Yarn:

    yarn add ejs
    

    Next you'll need to require the module, so open up your file where you require express (usually app.js or server.js)

    add below var express = require('express');

    var ejs = require('ejs');
    
    0 讨论(0)
  • 2020-12-13 06:03

    I had this exact same problem a couple of days ago and couldn't figure it out. Haven't managed to fix the problem properly but this works as a temporary fix:

    Go up one level (above app.js) and do npm install ejs. It will create a new node_modules folder and Express should find the module then.

    0 讨论(0)
  • 2020-12-13 06:03

    In my case it was a stupid mistake- it was a typo in the middleware. I wrote app.set('view engine', 'ejs.'); the dot caused the error. I installed ejs and express locally

    0 讨论(0)
  • 2020-12-13 06:04

    Way back when the same issue happened with me.

    Dependency was there for ejs in JSON file, tried installing it locally and globally but did not work.

    Then what I did was manually adding the module by:

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

    Then it works.

    0 讨论(0)
  • 2020-12-13 06:04

    Ensure all dependencies are installed. npm install

    I was making a quick app for myself and I had forgotten to add express. Threw the above error.

    0 讨论(0)
提交回复
热议问题