vanval vanval - 20 days ago 7
Node.js Question

Nodejs core module fs is not working with webpack and angular2 cli

The fs is an empty object in my app.

I am using Angular2 cli which uses webpack to bundle up the application.
I am using the file-exists package in my app - https://www.npmjs.com/package/file-exists.
So in my component.ts file, I have this line:

let fileExists = require('file-exists');


This file-exists package in turn has this line:

fs.statSync(fullpath).isFile();


But this fs is always just an empty object. Am not sure what is going on..
i.e is Webpack not including in fs which is node's core package ?
Any help would be greatly appreciated.

Answer

file-exists, depends on fs. There is no webpack polyfill for fs and there wouldn't be a good way to add one either. Therefore webpack can't export file-exists for client-side web usage. See this list for supported, native, node modules that can be polyfilled.