Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build with webpack causes "TypeError: fs.readFileSync is not a function" #3554

Closed
inxmail-de-cln opened this issue Oct 30, 2019 · 1 comment

Comments

@inxmail-de-cln
Copy link

I am trying to get uglify running in a browser within a webapp built with webpack. Since you have merged a PR for webpack-compatibility - see #3435 - I am curious how I have to configure webpack to get uglifyjs running.

E.g. my simple webpack.config.js looks like this:

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
  },
  node: {
    fs: 'empty'
  },
  devtool: 'source-map',
};

index.js looks like this:

import { minify  } from 'uglify-js';

console.log(minify('function foo() { console.log("bar") }'));

But when I build and run this in a browser I get an error in the web console:

node.js:20 Uncaught TypeError: i.readFileSync is not a function
    at node.js:20
    at Array.map (<anonymous>)
    at Object.<anonymous> (node.js:19)
...

The fs: 'empty-mock basically replaces the require('fs') with a plain empty object, so that readFileSync is undefined.

So is there any better webpack configuration for running uglify in a browser?

@alexlamsl
Copy link
Collaborator

On the command line:

$ uglify-js --self --wrap exports --output uglify.js

... and you should get a file which is compatible with your usage.

See README.md for more details on how to use CLI etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants