mishoo / UglifyJS

JavaScript parser / mangler / compressor / beautifier toolkit
http://lisperator.net/uglifyjs/
Other
13.15k stars 1.25k forks source link

Uncaught Error: Cannot find module '../lib/utils.js' #3312

Closed DrLambs closed 5 years ago

DrLambs commented 5 years ago

I'm building a vue app and using webpack to bundle it.

This line var UglifyJS = require("uglify-js"); causes the following:

tools sync?6ded:2

Uncaught Error: Cannot find module '../lib/utils.js' at Function.webpackEmptyContext [as resolve] (eval at ./node_modules/uglify-js/tools sync recursive (app.js:5165), :2:10) at eval (node.js?c35f:17) at Array.map () at eval (node.js?c35f:16) at Object../node_modules/uglify-js/tools/node.js (app.js:5176) at webpack_require (app.js:724) at fn (app.js:101) at eval (util.js?c276:4) at Module../src/libs/util.js (app.js:6383) at webpack_require (app.js:724)

and how can fix it

homerjam commented 5 years ago

FWIW I think this may be to do with this code which loads modules dynamically, presumably webpack would be picking them up if it weren't for them being in map??

// node.js

exports.FILES = [
    "../lib/utils.js",
    "../lib/ast.js",
    "../lib/parse.js",
    "../lib/transform.js",
    "../lib/scope.js",
    "../lib/output.js",
    "../lib/compress.js",
    "../lib/sourcemap.js",
    "../lib/mozilla-ast.js",
    "../lib/propmangle.js",
    "../lib/minify.js",
    "./exports.js",
].map(function(file) {
    return require.resolve(file);
});
alexlamsl commented 5 years ago

Not an issue with this project, please contact & consult with webpack instead.

jpknoll commented 5 years ago

@homerjam Yeah, I'm guessing this would all work if it just exported an array of require statements...

tschoffelen commented 4 years ago

I feel like this is actually an issue that should be solved here. There are many different packaging and transpilation tools that all struggle with dynamic imports/requires.

In some cases it's necessary to do a dynamic import, but in this case it is not, so it should be relatively easy to fix.

I'm willing to put in a PR, if that helps?

OBrown92 commented 4 years ago

Any progress here? With this issue I can't webpack email-templates

PaulKushch commented 3 years ago

Any news in this issue?

OBrown92 commented 3 years ago

I solved it by adding all the packages that use uglify or any other package that can't parsed by webpack to the externals. See this issue.

PaulKushch commented 3 years ago

@OBrown92 If someone does use next.js I solved it like the below:

const path = require('path');
const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
});
module.exports = withBundleAnalyzer({
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    config.module.rules.push({
      test: path.resolve(__dirname, 'node_modules/uglify-js/tools/node.js'),
      loader: 'null-loader',
    });
    // Important: return the modified config
    return config;
  },
  ...
}
MarkAndrewJohnson commented 2 years ago

@alexlamsl I disagree with your comment that it is not an issue with this project. Failing to build with all the popular build tools is a problem.

jerbear2008 commented 1 year ago

I was trying to use uglify-js in Deno with esm.sh, and this also causes the same issue: (error code for Googlers with this problem)

error: Uncaught NotFound: The system cannot find the path specified. (os error 3): readfile '../lib/utils.js'      
    at Object.readFileSync (ext:deno_fs/30_fs.js:704:14)
    at Object.readFileSync (ext:deno_node/_fs/_fs_readFile.ts:43:23)
    at https://esm.sh/v126/uglify-js@3.17.4/denonext/uglify-js.mjs:2:143432
    at Array.map (<anonymous>)
    at https://esm.sh/v126/uglify-js@3.17.4/denonext/uglify-js.mjs:2:143407
    at https://esm.sh/v126/uglify-js@3.17.4/denonext/uglify-js.mjs:4:4
    at https://esm.sh/v126/uglify-js@3.17.4/denonext/uglify-js.mjs:2:546
    at https://esm.sh/v126/uglify-js@3.17.4/denonext/uglify-js.mjs:7:718

A workaround is using npm:uglify-js instead of https://esm.sh/uglify-js, but this is definitely the fault of uglify-js being incompatible with esbuild. @alexlamsl This project does not work with many common tools, and there isn't really any reason for this. Can you take another look, and consider writing slightly less elegant code so that we can use this awesome tool in more places? Thanks!

hichemfantar commented 1 year ago

@OBrown92 If someone does use next.js I solved it like the below:

const path = require('path');
const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
});
module.exports = withBundleAnalyzer({
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    config.module.rules.push({
      test: path.resolve(__dirname, 'node_modules/uglify-js/tools/node.js'),
      loader: 'null-loader',
    });
    // Important: return the modified config
    return config;
  },
  ...
}

@PaulKushch I tried with nextjs 13.4.17 with the pages api but It's not working. mjml works fine without any workarounds when i install directly in next app but it breaks when I import mjml from a different custom package in a monorepo