Module "requires" inside Jade file

I need to use the "fs" module inside a Jade file. There is 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"

+3


source to share


2 answers


Two problems

You should send the function to the Jade file as an argument, not try to require it in Jade, i.e.

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, on line 2, you call the "readdirSync" function without defining it anywhere. It should be

fs.readdirSync

      

+6


source


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

      

+2


source







All Articles