0

I'm using Node.JS and Nunjuck to make my templates. When I change something on my .njk files, I have to stop the Node.JS server and start it back again so that the changes reflect on my browser (localhost:80).

Here is how my app.js (server) looks like:

var app = require("express")(),
nunjucks = require('nunjucks'),
server = require("http").createServer(app),
io = require("socket.io").listen(server)
path = require("path");

nunjucks.configure('views', 
{
autoescape: true,
express: app
});

app.get("/", function (req, res) 
{
res.render(__dirname + "/views/index.njk");
});

server.listen(80);

And in my index.njk, I have two lines: {% set x = 5 %} and {{ x }}.

For example, when I change the value of 5 to 7, I have to restart my node.JS server and then refresh the page in order to get a 7. If I just refresh the page without restarting the server, I still get a 5.

William Tang
  • 53
  • 1
  • 11
  • use `nodemon` or any tool that will watch for changes in any of your project files and then auto restart the server for you – 0.sh Dec 18 '18 at 19:16

3 Answers3

0

Are you using nodemon? If not do this:

npm i -D nodemon

than on your package.json do the following:

"scripts": {
    "start": "nodemon LOCATION/OF/index.js"
  }

now you have to run npm rum start to build you project.
On "LOCATION/OF/index.js" should be something like: nodemon src/index.js
Nodemon https://www.npmjs.com/package/nodemon

Mateus Gomes
  • 45
  • 2
  • 4
0

The problem is that nodemon doesn't watch for changes in .njk files.

0

You can use nodemon
By default, watching extensions for changes: js,json,md
Here's how to add .njk
Then run these commands:

npm install nodemon -g
nodemon app.js