1

I'm having trouble loading the core modules of nodejs to the browser.

My xlsx library depends on fs which is a core module and loading my app produces cannot find module "fs" error.

I've done some googling repeatedly ended up here - saying I should add target:node to my webpack.config.js file BUT, done as he suggested produces another error -> process is not defined

I was wondering if there's any module-loader for that need? i couldnt find any.

webpack.config.js:

module.exports = {
  entry: "./app/boot",
  output: {
    path: __dirname,
    filename: "./dist/bundle.js"
  },
  resolve: {
    extensions: ['', '.js', '.ts']
  },
  module: {
    loaders: [
      { test: /\.ts/,   loaders: ["ts-loader"], exclude: /node_modules/ }
    ],
    preLoaders: [
      // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
      { test: /\.js$/, loader: "source-map-loader", exclude: ['node_modules', 'ext-*', 'lib', 'tools'] }
    ]
  },
  debug: true,
  devtool: 'source-map'
};

Any insights would be much apreciated, thanks in advance and please let me know if any other information would be helpful.

I'm not using babel nor gulp

Community
  • 1
  • 1
Kesem David
  • 2,135
  • 3
  • 27
  • 46

1 Answers1

1

You can't.

There is no equivalent to fs that would work properly in the browser. JavaScript executed in a browser is running in a sandbox for security reasons and you have only very limited access to the client's file system, which is what the module fs is used for in NodeJS.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
  • I see, that makes perfect sense. so what am i supposed to do having the `xlsx` using `fs`? – Kesem David Nov 29 '16 at 19:49
  • That depends on the library. Can you use it without `fs` by e.g. loading the file manually via HTTP requests from your server? Then do that. Otherwise you'll have to look for a different library. – TimoStaudinger Nov 29 '16 at 19:51
  • That probably is out of the scope of the question but more precisely I'm using `ng2-file-uploader` to get a file from the client and the using `node-xlsx` to parse the file to an object, all that on the client without using the server, any suggestions? – Kesem David Nov 29 '16 at 20:03
  • `node-xlsx` seems to have NodeJS's API baked into it and won't be of any help to you. It is based on `js-xlsx` though, which seems to be able to run in a browser as well: https://github.com/SheetJS/js-xlsx – TimoStaudinger Nov 29 '16 at 20:05