错误:找不到模块“ejs”

发布于 2024-12-09 19:13:31 字数 1624 浏览 0 评论 0原文

这是我的完整错误:

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)

我的源代码也非常简单:

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);

在我的文件夹中,我在使用 npm install ejs 获得的 node_modules 中安装了 ejs。 在此处输入图像描述 所以我的问题是.. 给出了什么?我做错了什么,当我明确安装了 EJS 时,节点找不到 EJS?

谢谢

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 here so my question is.. what gives? What am I doing wrong so that node can't find EJS when I clearly have it installed?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

愁杀 2024-12-16 19:13:32

我遇到了这个问题。我使用node-inspector进行调试,发现从express源文件所在的node_modules文件夹中,没有安装ejs。所以我在那里安装了它并且它起作用了。

尽管 NODE_PATH 设置为相同的 node_modules 文件夹,但 npm install -g ejs 并未将其放在我期望的位置。可能做错了,刚从节点开始。

I had this problem. I debugged using node-inspector and saw that from the node_modules folder where the express source files were, ejs was not installed. So I installed it there and it worked.

npm install -g ejs didn't put it where I expected it to despite NODE_PATH being set to the same node_modules folder. Prob doing it wrong, just started with node.

就是爱搞怪 2024-12-16 19:13:32

我使用选项 --save 安装了express和ejs:

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

这样express和ejs就是依赖的package.json文件。

I installed both: express and ejs with the option --save:

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

This way express and ejs are dependecies package.json file.

冬天旳寂寞 2024-12-16 19:13:32

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

这对我有用,

  1. 在你的终端或 cmd ->转到您的应用程序目录,
  2. cd pathtoyourapp/AppName
  3. 重新运行“npm install”,
  4. 重新运行“npm installexpress”,
  5. 重新运行“npm install ejs”

,之后错误已修复。

Reinstalling npm, express and ejs fixed my problem

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'

after that, the error was fixed.

糖粟与秋泊 2024-12-16 19:13:32

第 1 步

请参阅npm ls | grep ejs 在项目的根级别检查您是否已将 ejs dependency 添加到项目中。

如果没有,请将其作为依赖项添加到您的项目中。 (我更喜欢将依赖项添加到 package.json 而不是 npm install 模块。)

例如。

{                                                                                                      
  "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 下载依赖项

npm install

STEP 3 检查 ejs 模块

$ npm ls | grep ejs
[email protected] /Users/prayagupd/nodejs-fkers/musicpedia
├── [email protected]

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
[email protected] /Users/prayagupd/nodejs-fkers/musicpedia
├── [email protected]
戏蝶舞 2024-12-16 19:13:32

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

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

Add dependency in package.json and then run npm install

    {
  ...
  ... 
  "dependencies": {
    "express": "*",
    "ejs": "*",
  }
}
紫轩蝶泪 2024-12-16 19:13:32

我认为您的计算机上未正确安装 ejs 模板引擎。
您只需使用 npm 安装模板引擎

npm install ejs --save

,然后在 app.js 中包含以下代码

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

I think ejs template engine is not properly installed on your machine.
You just install the template engine using npm

npm install ejs --save

then include the following code in app.js

app.set('view engine', 'ejs')
记忆之渊 2024-12-16 19:13:32

在我的项目目录中安装 Express 后,我遇到了同样的问题。以前我使用 npm install 命令使用 -g 选项将其安装在全局范围内。

I have the same issue it resolve after installing the express in my project directory. previously i install it in global scope with -g option with npm install command.

执着的年纪 2024-12-16 19:13:32

就我而言,没有愚蠢的语法错误,但出现了同样的错误。
我已经在全球范围内安装了 ejs 和 ejs-mate。我在本地安装了它并发现我的错误已解决。

In my case there was no silly syntax error, but same error arised.
I had installed ejs and ejs-mate globally. I installed it locally and found my error resolved.

_失温 2024-12-16 19:13:32

请确保 package.json 文件中的依赖项是最新的。确保您的 NPM 是最新版本(最新)后,请尝试一次重新安装它们。这对我有用。我建议您为软件包运行 npm install (这就是我自己的情况,因为我手动添加了依赖项而拒绝工作)。

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).

我们只是彼此的过ke 2024-12-16 19:13:32

就我而言,这是一个愚蠢的错误——这是中间件中的一个拼写错误。我写了 app.set('view engine', 'ejs.'); 点导致了错误。我在本地安装了ejs和express

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

稀香 2024-12-16 19:13:32

确保所有依赖项均已安装。 npm install

我正在为自己制作一个快速应用程序,但我忘记添加 Express。抛出了上面的错误。

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.

抱猫软卧 2024-12-16 19:13:32

我对 ejs 遇到同样的错误,然后我只需运行 node install ejs
这将再次安装 ejs。

然后再次运行 npm install 来安装node_modules。
这对我来说是工作。

I face same error for ejs, then i just run node install ejs
This will install ejs again.

and then also run npm install to install node_modules again.
That's work for me.

凉世弥音 2024-12-16 19:13:32

我在第一次运行应用程序之前忘记安装 ejs 后遇到了这个问题。由于某种原因,后来安装后看不到 ejs。我找到了解决此问题的快速、干净且有效的解决方案,即在重新启动服务器之前,通过在本地目录中运行 npm uninstallexpress 然后运行 ​​npm installexpress 来重新安装express。

I ran into this problem after I forgot to install ejs before the first time I ran my app. For some reason ejs was not being seen after installing it later. I found a quick, clean, and effective solution to this problem which was to reinstall express by running npm uninstall express then npm install express in the local directory before restarting the server.

纵情客 2024-12-16 19:13:32

npm i ejs --force

这对我有用

npm i ejs --force

this worked for me

糖果控 2024-12-16 19:13:32

在本地安装express解决了我同样的问题。
npm 我表达--保存

Installing express locally solved my same problem.
npm i express --save

白色秋天 2024-12-16 19:13:32

我遇到了同样的问题并尝试了一些给定的解决方案,但它仍然不起作用。我所做的就是在项目的根文件夹中运行“npx yarn”命令,仅此而已。

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.

べ映画 2024-12-16 19:13:32

你必须有

const ejs = require('ejs')
app.set('view engine', 'ejs')

you must have

const ejs = require('ejs')
app.set('view engine', 'ejs')
轻许诺言 2024-12-16 19:13:32
app.set('view engine', 'ejs')

然后在终端

npm install ejs --save 

解决问题

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

and then in the terminal

npm install ejs --save 

resolves the issue

感悟人生的甜 2024-12-16 19:13:32

就我而言,我只是卸载然后再次安装 ejs。

npm uninstall ejs

然后

npm install ejs

In my case, I just uninstall then install ejs again.

npm uninstall ejs

then

npm install ejs
天涯沦落人 2024-12-16 19:13:32

npm install ejs --save 对我有用! ✅

在 goormIDE 上,我有这个文件配置:

  • 容器
    • main.js
    • package-lock.json
    • package.json
    • 节点模块
    • 观看次数
      • home.ejs

在我的 main.js 文件中,我也有这条路线

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

npm install ejs -g 没有在 package.json 中添加相应的依赖项。
npm install ejs --save 做到了。我从容器目录执行了命令行。可以手动将其添加到 package.json 中:
**

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

**

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",}

**

远昼 2024-12-16 19:13:31

几天前我遇到了同样的问题,但无法解决。尚未设法正确解决问题,但这可以作为临时解决方案:

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

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.

百思不得你姐 2024-12-16 19:13:31

在本地安装express

(在项目根目录中的npm installexpress


您的项目依赖于expressejs,因此您应该将它们都列出作为 package.json 中的依赖项。

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

一般来说,最好在 package.json 中显式列出所有依赖项,即使其中一些依赖项可能已经全局安装,这样您就不会遇到这些类型的问题。

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.

迷迭香的记忆 2024-12-16 19:13:31

很久以前,同样的问题发生在我身上。

JSON 文件中的 ejs 存在依赖关系,尝试在本地和全局安装它,但没有成功。

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

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

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

然后它就可以工作了。

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.

清旖 2024-12-16 19:13:31

就我而言,我只是在 package.json 中手动添加了 ejs:

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

并运行 npm install (可能您需要使用 sudo 运行它)
请注意,ejs 默认情况下会查看视图目录

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

 {
   "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

没︽人懂的悲伤 2024-12-16 19:13:31

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

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.

樱娆 2024-12-16 19:13:31

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

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

I installed ejs using command npm install ejs in express directory level and this solved my problem.

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

筱武穆 2024-12-16 19:13:31

安装nodeexpress项目后,您需要安装ejs或pug包,

您可以尝试使用node

npm install ejs 
npm install pug

,也可以尝试

yarn add ejs
yarn add pug

全局或本地使用yarn

After installing node express project you need to install the ejs or pug package for that

either you can try with node

npm install ejs 
npm install pug

or you can try with yarn

yarn add ejs
yarn add pug

globally or locally

夏花。依旧 2024-12-16 19:13:31

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

其他真正伟大且易于学习的可能是:

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

npm install ejs

或者如果您使用的是 Yarn:

yarn add ejs

接下来您需要 require 该模块,因此打开您需要 express 的文件(通常是 app.js 或 server.js),

var 下面添加快递 = 要求('快递');

var ejs = require('ejs');

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:

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');
冧九 2024-12-16 19:13:31

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

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

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
安穩 2024-12-16 19:13:31

我有同样的问题。所以我做了以下事情,它对我有用。

解决方案:

  1. 如果尚未完成,请在项目目录中运行“ npm init ”。
  2. 安装ejs并表达如下:

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

这样做会在 package.json 文件中创建所需的依赖项

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文