Nodejs 运行 node app.js 时 express 报错 cannot find module 'jade'

Nodejs 运行 node app.js 时 express 报错 cannot find module 'jade’

Express

500 Error: Cannot find module 'jade'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at new View (F:\nodejs\microblog\node_modules\express\lib\view.js:43:49)
at Function.app.render (F:\nodejs\microblog\node_modules\express\lib\application.js:488:12)
at ServerResponse.res.render (F:\nodejs\microblog\node_modules\express\lib\response.js:759:7)
at exports.index (F:\nodejs\microblog\routes\index.js:7:7)
at callbacks (F:\nodejs\microblog\node_modules\express\lib\router\index.js:164:37)
at param (F:\nodejs\microblog\node_modules\express\lib\router\index.js:138:11)

5 回复

Nodejs 运行 node app.js 时 express 报错 cannot find module 'jade'

问题描述

当你尝试运行你的 Node.js 应用程序时,你可能会遇到一个错误信息,提示 Error: Cannot find module 'jade'。这通常意味着你的应用程序依赖的某个模块(在这个例子中是 jade)没有被正确安装。

解释

jade 是一个模板引擎,它已经被重命名为 pug。如果你的应用程序仍然引用了 jade,那么你需要更新这些引用为 pug。此外,确保所有相关的依赖项都已通过 npm 安装。

解决方案

  1. 更新 package.json 文件 首先,你需要在 package.json 文件中将 "jade" 替换为 "pug"。例如:

    "dependencies": {
      "express": "^4.17.1",
      "pug": "^3.0.2"
    }
    
  2. 安装依赖 确保所有依赖项都已安装。你可以通过以下命令来安装它们:

    npm install
    
  3. 更新你的代码 更新你的视图文件中的所有 jade 引用为 pug。例如,如果你有一个 .jade 文件,你需要将其扩展名更改为 .pug。同时,在你的 Express 路由器配置中,也需要相应地更改模板引擎的设置:

    // app.js 或类似文件中
    const express = require('express');
    const app = express();
    
    // 设置模板引擎为 pug
    app.set('views', './views'); // 指定视图文件的路径
    app.set('view engine', 'pug'); // 设置模板引擎为 pug
    
    // 示例路由
    app.get('/', (req, res) => {
      res.render('index'); // 渲染 index.pug 文件
    });
    
    app.listen(3000, () => {
      console.log('Server is running on port 3000');
    });
    
  4. 检查文件路径 确保你的视图文件路径与 app.set('views', './views') 中设置的一致。例如,如果你的视图文件位于 ./views 目录下,则上述代码应该能正常工作。

通过以上步骤,你应该能够解决 cannot find module 'jade' 的问题,并使你的 Node.js 应用程序恢复正常运行。


package.json里配置了吗?

package.json 有加入jade配置吗?

node_moudle 有jade的包吗?

jade模板模块没有安装,使用npm install jade安装

当你在运行 node app.js 时遇到 Error: Cannot find module 'jade' 错误,这意味着你的项目依赖中缺少了 jade 模块。jade 是一个模板引擎,现在已经被 pug 替代。你需要更新你的项目依赖,并将所有使用 jade 的地方改为 pug

解决步骤

  1. 安装 pug 模块 打开命令行工具,进入到你的项目根目录,然后运行以下命令来安装 pug

    npm install pug --save
    
  2. 更新 app.jsserver.js 文件

    假设你在 app.js 中使用了 jade 作为模板引擎,你需要将其更改为 pug。以下是更新前后的对比:

    更新前

    var express = require('express');
    var app = express();
    
    app.set('view engine', 'jade');
    
    app.get('/', function(req, res) {
        res.render('index', { title: 'Hello, World!' });
    });
    
    app.listen(3000);
    

    更新后

    var express = require('express');
    var app = express();
    
    // 设置 pug 为模板引擎
    app.set('view engine', 'pug');
    
    app.get('/', function(req, res) {
        res.render('index', { title: 'Hello, World!' });
    });
    
    app.listen(3000);
    
  3. 更新路由文件(如果需要)

    如果你在路由文件中也使用了 jade,同样需要将其更改为 pug。例如,如果你的 index.js 文件中有类似以下的代码:

    更新前

    exports.index = function(req, res){
        res.render('index.jade', { title: 'Hello, World!' });
    };
    

    更新后

    exports.index = function(req, res){
        res.render('index.pug', { title: 'Hello, World!' });
    };
    

通过以上步骤,你应该可以解决 cannot find module 'jade' 的错误。确保所有的视图文件扩展名也从 .jade 更新为 .pug

回到顶部