Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module "require" inside a Jade file

Tags:

node.js

pug

I need to use "fs" module inside a Jade file. No other JS file.

When I try:

- var js = require('fs')
- var downloadfiles = readdirSync("download") 
for f in downloadfiles
  url
    loc= D + "/download" + f
    lastmod= buildDate
    changefreq daily
    priority 1.0

I got the error "undefined is not a function"

like image 728
Tomek Wyderka Avatar asked Dec 25 '22 01:12

Tomek Wyderka


2 Answers

Two problems

You should send the function to the Jade file as an argument rather than trying to require it in Jade ie.

var js = require('fs');
res.render('myjadefile', {fsFunction: fs});

myjadefile.jade

- var downloadfile = fsFunction.readdirSync('download');
for f in downloadfiles
   // rest of your code


Also, in line 2 you are calling function "readdirSync" without defining it anywhere. It should be

fs.readdirSync
like image 160
takinola Avatar answered Jan 19 '23 01:01

takinola


res.render(index, {data:data,
                   title:'Jade Require'
                   require:require})

Now in your jade.

extends layout
!{fs=require('fs')}


body
  - var downloadfiles = readdirSync("download") 
  for f in downloadfiles
    url
      loc= D + "/download" + f
      lastmod= buildDate
      changefreq daily
      priority 1.0
like image 21
Jason Mitchell Avatar answered Jan 19 '23 03:01

Jason Mitchell