0

my Webpack version is v1.15.0.

I just require('fs') in my JS file, but I got the error:

Uncaught Error: Cannot find module "fs"; and when I add node{ fs: 'empty' },

and then I got another error:

fs.readdirSync is not a function

if I add:

externals:{
  "fs": "commonjs fs"
},

I get another error:

Uncaught ReferenceError: require is not defined

Why? How can I fixed it?

EDIT : when I put all my file's names on a file .txt and I get the file .txt with $http.get it's working fine

saminimoi
  • 1
  • 2
  • 1
    Why should you want to access the filesystem in app? Keep in mind, the code you write is supposed to run in the browser, which does not have access to the filesystem. – lipp Jun 06 '18 at 09:40
  • To display a list of json files. Json files are in the public directory, so I possibly can access non ? – saminimoi Jun 06 '18 at 09:49
  • No you can't. Not with fs. You can fetch them though, as shown in the answer (or using $http of course) – baao Jun 06 '18 at 11:48
  • How can I get back my files in another way that 'fs' or $http ? – saminimoi Jun 06 '18 at 12:27

2 Answers2

1

fs should be server side only (nodeJS) you would always make node do the work of accessing files on the server, not the client side.

You would do something like this, on NodeJS (server side) (this isnt word for word correct, but it'll get you started)

fs = require('fs')


router.get('/getdocs', function(req, res, next) {
  // do your filesystem operation here, then return what you want
})    

Then on the client side (angularJS) you would fetch what NodeJS returns

fetch('mydomain/api/getdocs').then(rtrn => {
    console.log(rtrn)
})
baao
  • 71,625
  • 17
  • 143
  • 203
Gaza
  • 427
  • 2
  • 4
  • 17
  • I don't know how to work NodeJs... Can I have a little more explanation please ? – saminimoi Jun 06 '18 at 12:32
  • I would highly recommend reading something like this: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs Its a huge subject to try and explain on here - but this should get you going in the right direction – Gaza Jun 06 '18 at 13:06
0

It is always recommended to have the inbuilt or the function which arent gonna change to use it with constant

const fs = require('fs');

having it constant throws a error when you try to modify it.

manish kumar
  • 4,412
  • 4
  • 34
  • 51