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

webpack bundling and "module has no exports" #162

Closed
skratchdot opened this issue Sep 29, 2022 · 14 comments
Closed

webpack bundling and "module has no exports" #162

skratchdot opened this issue Sep 29, 2022 · 14 comments

Comments

@skratchdot
Copy link

Hey! First off thanks for making this great library!

I just tried upgrading filesize from v9 to v10.

After changing my imports from:

import filesize from 'filesize'

to:

import { filesize } from 'filesize'

I thought things would work, but when I ran my app, I saw the following webpack error when visiting my site:

export 'filesize' (imported as 'filesize') was not found in 'filesize' (module has no exports)

I was able to "fix" the issue by adding the following resolve alias to my webpack config:

    resolve: {
      alias: {
        filesize: require.resolve('filesize'),
      },
    },

My project is using typescript, and I think a fairly standard webpack setup.

I did a little searching, and I've seen a few people mention that changing the "browser" field from a string to a map might force webpack to pick the "module" over the "browser" field?

I think my build was using the browser field by default and that doesn't have exports?

webpack/webpack#4674 (comment)

https://github.com/defunctzombie/package-browser-field-spec

@avoidwork
Copy link
Owner

Sorry about that, the named export was wrong on the UMD file. This is fixed in 10.0.2.

@skratchdot
Copy link
Author

Thanks for the super fast turnaround!

I upgraded to 10.0.2, and removed the alias, and I still see:

image

Adding the alias back in "fixed" things again.

I'll try to come up w/ a codesandbox to show the issue.

@avoidwork avoidwork reopened this Sep 29, 2022
@skratchdot
Copy link
Author

skratchdot commented Sep 29, 2022

Here's the issue:

https://codesandbox.io/s/reverent-resonance-8sus4p?file=/src/index.ts

image

All I did was clone the typescript-webpack template, the I added filesize as a dependency, and changed index.ts from:

console.log('hello world!@');

to:

import { filesize } from "filesize";

document.body.innerHTML = "hello world 1";

document.body.innerHTML = "hello world 2" + filesize(200);

If the error wasn't happening, I would expect to see: hello world 2 200 B as the html content.

NOTE: that project's webpack config is different than mine, but the common thing between the two is I use typescript, webpack 5, and see the same error when importing filesize. No need to rush a fix on this, just wanted to let people know of a temporary workaround!!! Thanks again!

@orestisioakeimidis
Copy link

I am having the same issue. Thanks for the workaround @skratchdot!

@akhmyrov
Copy link

@avoidwork Hi! I created a pull request which should help with this problem

@paulschreiber
Copy link

@avoidwork I am getting the problem on compile with 10.0.2. We are using react-scripts build.

Creating an optimized production build...
Failed to compile.

Attempted import error: 'fileSize' is not exported from 'filesize' (imported as 'fileSize').

@skratchdot
Copy link
Author

@avoidwork I am getting the problem on compile with 10.0.2. We are using react-scripts build.

Creating an optimized production build...
Failed to compile.

Attempted import error: 'fileSize' is not exported from 'filesize' (imported as 'fileSize').

@paulschreiber -
This looks like a case-sensitivity issue.

Change:

import { fileSize } from 'filesize'

to:

import { filesize } from 'filesize'

after you do that, you may or may not run into the error from this issue.

@paulschreiber
Copy link

paulschreiber commented Sep 29, 2022

ah, that was a typo. Everything is lowercase and we still have e error. Source: techmatters/terraso-web-client@main...fix/dependenecies-filesize

$ npm run build

> [email protected] build
> react-scripts build

Creating an optimized production build...
Failed to compile.

Attempted import error: 'filesize' is not exported from 'filesize' (imported as 'filesize').```

@avoidwork
Copy link
Owner

avoidwork commented Sep 29, 2022

can anyone explain the issue? the module changed from a default export to a named export; is the umd the problem for web pack? would an iife solve the issue?

I'm happy to do something to solve the issue, but is returning the default and removing the named the best solution? I can undo that change.

@avoidwork
Copy link
Owner

avoidwork commented Sep 29, 2022

Re-opening because I don't know if it's solved by returning the default export; 10.0.3 has the default export.

@avoidwork avoidwork reopened this Sep 29, 2022
@avoidwork
Copy link
Owner

@skratchdot thanks for that demo above, i'll see about making that a test case.

amcclain added a commit to xh/hoist-react that referenced this issue Sep 29, 2022
@avoidwork
Copy link
Owner

avoidwork commented Sep 29, 2022

webpack appears to be incompatible with the amd/umd/iife output from rollup, so as a quick fix i've removed the 'browser' key from package.json which lets webpack default to a better file to work with, and added test-webpack script to do a build with the test repo to see if it errors.

this is 10.0.4.

@skratchdot
Copy link
Author

awesome! thank you @avoidwork . Removing "browser" was the only solution I knew of, but I figured you had some users that made use of it. It's fairly complicated to support all the different options I feel like. I tried looking for "standard" rollup configs, but didn't find much.

i think due to how complicated it is to support everyone's setup, some people have just started supporting ESM only, and link to something like:
https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

you'd probably still get support requests if you did that though 🤣- so i like your solution of supporting commonjs and esm :) and umd people can still find those files in your dist folder.

@skratchdot
Copy link
Author

I also wanted to give you confirmation that v10.0.5 is working for me (without any workarounds). Thank you for all your work!!!

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