Search code examples
javascriptnode.jswebpackes6-module-loader

How to load Nodejs core modules using webpack


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


Solution

  • 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.