javascript – How to fix this jade error?

Question:

I'm just getting started in nodeJS and I'm using JADE as a view engine , I'm just doing tests, nothing extraordinary, and yet it throws the following error:

C:\Users\Familia\Documents\node\expres\node_modules\promise\index.js:1
(function (exports, require, module, __filename, __dirname) { 


SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\Familia\Documents\node\expres\node_modules\jstransformer\index.js:5:15)

My code is the following app.js :

var express = require("express");

var app = express();

app.set("view engine", "jade");

app.get("/",(req, res)=>{
    res.render("index");
    res.end();
}).listen(8080);

And this is the index.jade :

html
    head
        title "mi primera página con jade"
    body
        h1 "Hola Mundo"

Answer:

A couple of details about your code

  1. jade is a template engine that is discontinued and in the near future you should try Pug which is the version that was born
  2. You need to declare the variable that will contain the path (this is needed so that the call to your files is made as an absolute path regardless of changing the name of the folder)
  3. I didn't see you installed jade, so first you need to do npm i -S jade
  4. Declare a jade variable that contains the jade module call
  5. In the res.render add the path.join

Look at this code for your .js file

var express = require("express");
var jade = require("jade")
var path    = require("path");

var app = express();

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

app.get("/", function(req, res){
     res.render(path.join(__dirname+'/index'));
}).listen(8080);

For your Jade file, I leave you this example, where, as you can see, your declaration of the tags is incomplete and considering that jade compiles them and then displays an HTML, that is why it also gives an error

doctype html
html(lang="en")
  head
    title= pageTitle
  body
    h1 Jade 
      p.
        Hola a Todos

If, for example, you copy and paste this code just like that, from the console at the level of the folder that contains your project, you must do an npm install to run and install the dependencies that you need

Regarding the PUg template engine, I leave you 2 links; the first is to see its download and installation process

https://www.npmjs.com/package/pug

The second is for you to see from its official site how it is used

https://pugjs.org/api/getting-started.html

Scroll to Top