node.js 错误:找不到模块“ejs”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7754799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:40:51  来源:igfitidea点击:

Error: Cannot find module 'ejs'

node.jsexpressejs

提问by Shamoon

Here is my complete error:

这是我的完整错误:

Error: Cannot find module 'ejs'
    at Function._resolveFilename (module.js:317:11)
    at Function._load (module.js:262:25)
    at require (module.js:346:19)
    at View.templateEngine (/Users/shamoon/local/node/lib/node_modules/express/lib/view/view.js:133:38)
    at Function.compile (/Users/shamoon/local/node/lib/node_modules/express/lib/view.js:65:17)
    at ServerResponse._render (/Users/shamoon/local/node/lib/node_modules/express/lib/view.js:414:18)
    at ServerResponse.render (/Users/shamoon/local/node/lib/node_modules/express/lib/view.js:315:17)
    at /Users/shamoon/Sites/soldhere.in/app.js:26:7
    at callbacks (/Users/shamoon/local/node/lib/node_modules/express/lib/router/index.js:272:11)
    at param (/Users/shamoon/local/node/lib/node_modules/express/lib/router/index.js:246:11)

My source code is also very simple:

我的源代码也很简单:

var express = require('express');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.use(express.bodyParser());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.set('view engine', 'ejs');
app.set('view options', {
    layout: false
});

app.get('/', function(req, res) {
  res.render('index', {
    message : 'De groeten'
  });
});

app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

In my folder, I have ejs installed in node_modules which I got using npm install ejs. enter image description hereso my question is.. what gives? What am I doing wrong so that node can't find EJS when I clearly have it installed?

在我的文件夹中,我在 node_modules 中安装了 ejs,我使用npm install ejs. 在此处输入图片说明所以我的问题是..是什么?当我清楚地安装了 EJS 时,我做错了什么,以至于节点找不到 EJS?

Thanks

谢谢

回答by evilcelery

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.

上一层(在 app.js 之上)并执行npm install ejs. 它将创建一个新的 node_modules 文件夹,然后 Express 应该会找到该模块。

回答by Will

Install express locally

在本地安装 express

(npm install expresswhile in the project's root directory)

npm install express在项目的根目录中)



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

您的项目同时依赖于expressejs,因此您应该将它们都列为您的package.json.

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

这样,当您npm install在项目目录中运行时,它将同时安装expressand ejs,因此这var express = require('express')将是express的本地安装(知道ejs您在本地安装的模块),而不是全局安装,后者不知道。

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

通常,package.json即使其中一些可能已经全局安装,显式列出所有依赖项也是一个好主意,因此您不会遇到这些类型的问题。

回答by Pete

I had the same issue. Once I set environment variable NODE_PATH to the location of my modules (/usr/local/node-v0.8.4/node_modules in my case) the problem went away. P.S. NODE_PATH accepts a colon separated list of directories if you need to specify more than one.

我遇到过同样的问题。一旦我将环境变量 NODE_PATH 设置为我的模块的位置(在我的情况下为 /usr/local/node-v0.8.4/node_modules),问题就消失了。如果您需要指定多个目录,PS NODE_PATH 接受以冒号分隔的目录列表。

回答by levon

I my case, I just added ejs manually in package.json:

我的情况是,我只是在package.json 中手动添加了 ejs :

 {
   "name": "myApp"
   "dependencies": {
     "express": "^4.12.2",
     "ejs": "^1.0.0"
   }
 }

And run npm install(may be you need run it with sudo) Please note, that ejs looks views directory by default

并运行npm install(可能你需要用sudo运行它)请注意,默认情况下 ejs 看起来是 views 目录

回答by Ramesh Kotkar

I installed ejsusing command npm install ejsin express directory level and this solved my problem.

我在 express 目录级别ejs使用命令npm install ejs进行安装,这解决了我的问题。

i have install express using steps mention in express guide http://expressjs.com/guide.html

我已经使用快递指南http://expressjs.com/guide.html 中提到的步骤安装快递

回答by Kael

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.

JSON 文件中的 ejs 存在依赖性,尝试在本地和全局安装它,但没有奏效。

Then what I did was manually adding the module by:

然后我所做的是通过以下方式手动添加模块:

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

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

Then it works.

然后它起作用了。

回答by Chanaka Fernando

Install it locally rather installing it globally. Then your project may be run on any machine without any error.I think its better.

本地安装而不是全局安装。那么你的项目可以在任何机器上运行而不会出现任何错误。我认为它更好。

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

回答by apelidoko

Reinstalling npm, express and ejs fixed my problem

重新安装 npm、express 和 ejs 解决了我的问题

This one worked for me,

这个对我有用,

  1. On your terminal or cmd -> Go to your apps directory,
  2. cd pathtoyourapp/AppName
  3. rerun your 'npm install'
  4. rerun your 'npm install express'
  5. rerun your 'npm install ejs'
  1. 在您的终端或 cmd -> 转到您的应用程序目录,
  2. cd pathtoyourapp/AppName
  3. 重新运行你的“npm install”
  4. 重新运行你的“npm install express”
  5. 重新运行你的“npm install ejs”

after that, the error was fixed.

之后,错误被修复。

回答by Timm Jensen

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.

安装 Express V xxx 之后,您需要选择一个模板视图引擎。有很多真的很容易学。我个人的首选EJS

Other really great and easy to learn could be:

其他非常棒且易于学习的可能是:

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

安装 EJS(并修复错误)在项目的根目录中运行:

npm install ejs

Or if you're using Yarn:

或者,如果您使用的是 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)

接下来你需要 require 模块,所以在你需要 express 的地方打开你的文件(通常是 app.js 或 server.js)

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

在下面添加 var express = require('express');

var ejs = require('ejs');

回答by marwari

Add dependency in package.jsonand then run npm install

添加依赖package.json,然后运行npm install

    {
  ...
  ... 
  "dependencies": {
    "express": "*",
    "ejs": "*",
  }
}