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

Does not work when loaded as an ESM module #84

Closed
kirlat opened this issue May 6, 2018 · 4 comments
Closed

Does not work when loaded as an ESM module #84

kirlat opened this issue May 6, 2018 · 4 comments

Comments

@kirlat
Copy link

kirlat commented May 6, 2018

The plugin works great when loaded as require('clean-webpack-plugin'). However, we need to run it in a node.js build script that uses an ESM import syntax, with --experimental-modules flag (node.js 10). This is how we load it: import CleanWebpackPlugin from 'clean-webpack-plugin'. Unfortunately, it returns the following error: TypeError: Cannot read property 'filename' of undefined. I've checked the plugin code, and the only place where filename is used is module.parent.filename. I checked, and module.parent is undefined when plugin is loaded using ESM syntax.

What would be the best way to fix this? It seems that module.parent.filename is used to determine Webpack location. However, plugin takes a root option that is a Webpack root path. Can we check if module.parent is undefined and if it is, use a root option instead of it?

This plugin is really helpful and I would love to be able to use it in an ESM environment.

@i8degrees
Copy link

Hi, I just wanted to throw it out there that I suffer the same problem as @kirlat above. I use @std/esm for an ESM environment and decided to take a stab at resolving the issue with using the suggestion made. I forget now why exactly I was not able to pass the existing root option as-is, so I added an additional option under the name of context.

new CleanWebpackPlugin(['dist'], {
      root: __dirname,
      context: __dirname,
    })

I have been using this successfully for the past few months now without hiccup. Here's to hoping that somebody with more time might be able to nail this problem in its coffin! :-)

Thanks!

@riri
Copy link

riri commented Nov 17, 2018

I got exactly the same problem than @kirlat, using ESM, TypeError: Cannot read property 'filename' of undefined, as module.parent is undefined.

@MeiKatz
Copy link
Contributor

MeiKatz commented Dec 7, 2018

I am having the same problem currently and I think the solution is to make the check against module.parent conditionally: if it is not defined then webpackDir should be undefined.

webpackDir = module.parent && path.dirname(module.parent.filename);

And so the problem is solved.

@MeiKatz MeiKatz mentioned this issue Dec 8, 2018
@chrisblossom
Copy link
Collaborator

Closed via #95 for ^1.0.0.

v2.0.0 via #99 has been released. See johnagan/clean-webpack-plugin#usage. Please open a new issue if you continue to have problems.

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

5 participants