Search code examples
node.jsubuntugulpfsjs-xlsx

Gulp/NodeJS: Require('fs') fails on Ubuntu, but works on Windows


I've inherited a project that does its build using NodeJS. Everything's been working fine for us for months. We've built a number of production releases with no problem.

Enter xlsx.js.

Since adding this package as a dependency, we've found that our Gulp build works on Windows but not on Ubuntu (which is our build machine). When we do the build on Ubuntu, RequireJS barfs on require('fs'):

johnny@ubun-16:~/dev/eVGM-JavaScript-Client$ node --version
v4.6.1
johnny@ubun-16:~/dev/eVGM-JavaScript-Client$ gulp test
[15:19:35] Using gulpfile ~/dev/eVGM-JavaScript-Client/gulpfile.js
[15:19:35] Starting 'test'...
[15:19:35] Starting 'run-tests'...
07 11 2016 15:19:40.236:WARN [karma]: No captured browser, open http://localhost:9876/
07 11 2016 15:19:40.298:INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
07 11 2016 15:19:40.308:INFO [launcher]: Starting browser PhantomJS
07 11 2016 15:19:41.421:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket Hq69p1CiIhcsCDOGAAAA with id 72071968
07 11 2016 15:19:41.596:WARN [web-server]: 404: /base/jspm_packages/system-polyfills.js
07 11 2016 15:19:43.457:WARN [web-server]: 404: /base/fs.js
PhantomJS 2.1.1 (Linux 0.0.0) ERROR
  Error: (SystemJS) XHR error (404 Not Found) loading /home/johnny/dev/eVGM-JavaScript-Client/fs.js
Error loading /home/johnny/dev/eVGM-JavaScript-Client/fs.js as "fs" from /home/johnny/dev/eVGM-JavaScript-Client/jspm_packages/github/sheetjs/[email protected]/xlsx.js

PhantomJS 2.1.1 (Linux 0.0.0): Executed 0 of 0 ERROR (2.27 secs / 0 secs)

From what I can tell, 'fs' is a built-in package for NodeJS. (right?) And it should be there. Yet it's not according to Gulp.

To make matters even weirder, if I type Node and at the Node prompt, type "require('fs')", I get the flood of text that tells me that Node is finding the package A-OK.

Maybe it's a Gulp issue. Maybe it's a Ubuntu issue. Maybe it's a Jeff's-an-idiot issue. For whatever reason, I can not get our build to find fs from Gulp, and only on Ubuntu.

Any help very, very gratefully received ...

Thanks, Jeff


Solution

  • Ok, i figured it out, digging through some related StackOverflow answers.

    require('fs') is only intended to be used on a server application. After all, what is the file system of a browser? It makes no sense in that context. If I'd been using my brain I would have realized that.

    So I looked at where the library was using fs, and it was only in a single place:

    var fs;
    function readFileSync(filename, options) {
      if(fs === undefined) fs = require('fs');
      return parse(fs.readFileSync(filename), options);
    }
    

    I stuck an alert in there and ran the code we needed; the alert was not called.

    That means that even though fs.js is loaded it is not actually used when executing in the browser for our application.

    So the next step is probably to make a custom version of the library (a fork) and remove that single function. We don't need it and it breaks our build, so we can get rid of it.

    Does this make sense?

    Jeff