Error: Cannot find module 'ejs'

后端 未结 29 2035
南方客
南方客 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 06:05

    STEP 1

    See npm ls | grep ejs at root level of your project to check if you have already added ejs dependency to your project.

    If not, add it as dependencies to your project. (I prefer adding dependency to package.json instead of npm installing the module.)

    eg.

    {                                                                                                      
      "name": "musicpedia",                                                                                
      "version": "0.0.0",                                                                                  
      "private": true,                                                                                     
      "scripts": {                                                                                         
        "start": "node ./bin/www"                                                                          
      },                                                                                                   
      "dependencies": {                                                                                    
        "body-parser": "~1.15.1",                                                                          
        "cookie-parser": "~1.4.3",                                                                         
        "debug": "~2.2.0",                                                                                 
        "express": "~4.13.4",                                                                              
        "jade": "~1.11.0",                                                                                 
        "ejs": "^1.0.0",                                                                                                                                                            
        "morgan": "~1.7.0",                                                                                
        "serve-favicon": "~2.3.0"                                                                          
      }                                                                                                    
    }   
    

    STEP 2 download the dependencies

    npm install
    

    STEP 3 check ejs module

    $ npm ls | grep ejs
    musicpedia@0.0.0 /Users/prayagupd/nodejs-fkers/musicpedia
    ├── ejs@1.0.0
    
    0 讨论(0)
  • 2020-12-13 06:06

    Install express locally

    (npm install express while in the project's root directory)


    Your project depends on both express and ejs, so you should list them both as dependencies in your package.json.

    That way when you run npm install in you project directory, it'll install both express and ejs, so that var express = require('express') will be the local installation of express (which knows about the ejs module that you installed locally) rather than the global one, which doesn't.

    In general it's a good idea to explicitly list all dependencies in your package.json even though some of them might already be globally installed, so you don't have these types of issues.

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

    npm i ejs --force

    this worked for me

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

    npm install ejs --save worked for me ! ✅

    On goormIDE, I had this file configuration :

    • container
      • main.js
      • package-lock.json
      • package.json
      • node_modules
      • views
        • home.ejs

    In my main.js file, I also had this route

    app.get("/", function(req, res){
    res.render("home.ejs");
    })
    

    npm install ejs -g didn't add the corresponding dependency within the package.json. npm install ejs --save did. I executed the command line from the container directory. Manually it could have been added into the package.json with : **

    "dependencies": {
        "ejs": "^3.0.2",}
    

    **

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

    i had the same issue and tried a few of the given solutions but it still didn't work. all i did was to run the "npx yarn" command in the root folder of my project and that was it.

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

    i had the same problem. So i did the following and it worked for me.

    solution:

    1. run " npm init " in the project directory if not already done.
    2. install ejs and express as follows:

    npm install ejs --save
    npm install express --save

    by doing so it creates the required dependencies in the package.json file

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