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

Cannot import local forage through Javascript ES6 #976

Closed
Legomite opened this issue Jul 15, 2020 · 11 comments
Closed

Cannot import local forage through Javascript ES6 #976

Legomite opened this issue Jul 15, 2020 · 11 comments
Labels

Comments

@Legomite
Copy link

Legomite commented Jul 15, 2020

I'm attempting to import localForage in vanilla Javascript using

import localforage from "localforage";

which results in the error

SyntaxError: Importing binding name 'default' cannot be resolved by star export entries.

Doing it this way,

TypeError: undefined is not a function (near '...localforage.config...')

will not error initially but I cannot use the config() function as the error occurs

TypeError: undefined is not a function (near '...localforage.config...')

@Legomite

This comment has been minimized.

@Legomite Legomite changed the title keys() does not call function in .then on new localForage instance Cannot import local forage through Javascript ES6 Jul 15, 2020
@tofumatt
Copy link
Member

Can you post a full example—perhaps a repo we can use to diagnose the problem? Sounds like you might have a webpack config that isn't working or something, as importing localForage with that import syntax has been known to work without issue for some time.

@blueandhack
Copy link

blueandhack commented Jul 21, 2020

Can you post a full example—perhaps a repo we can use to diagnose the problem? Sounds like you might have a webpack config that isn't working or something, as importing localForage with that import syntax has been known to work without issue for some time.

I also encounter the error when I upgraded the newest version (1.8.0) (React Project)
Please see the reproduce with TypeError
Edit determined-hopper-juyn0

Also, I created an old version localForage (1.7.4) project, it works great (React Project)
Edit awesome-goodall-t13vz

@tofumatt
Copy link
Member

I think that bug is unrelated, see #979. If it still happens with 1.8.1 please let us know 😄

@djytw
Copy link

djytw commented Feb 2, 2021

Example in 1.9.0, all vanilla ES6 import, using a single HTML file:

<script type="module">
  import localforage from "../node_modules/localforage/dist/localforage.js";
</script>

result:
Uncaught SyntaxError: The requested module '../node_modules/localforage/dist/localforage.js' does not provide an export named 'default'

<script type="module">
  import * as localforage from "../node_modules/localforage/dist/localforage.js";
  localforage.setItem('key', 'value')
</script>

result:
Uncaught TypeError: localforage.setItem is not a function

<script type="module">
  import localforage from "../node_modules/localforage/src/localforage.js";
</script>

result:
localforage.js:1 GET http://xxx/main/node_modules/localforage/src/drivers/indexeddb net::ERR_ABORTED 404 (Not Found) localforage.js:2 GET http://xxx/main/node_modules/localforage/src/drivers/websql net::ERR_ABORTED 404 (Not Found) localforage.js:3 ...
(currently browser cannot import without file extension)

@madeInLagny
Copy link

A mere import 'localforage' works.

@qgustavor
Copy link

@madeInLagny It works if you use a module loader that handles node_modules, like webpack, Parcel, Vite or Rollup with the plugin @rollup/plugin-node-resolve. Browsers do not handle node_modules and, as many people nowadays are developing applications without using the tools above, would be nice if it was possible to import the module directly from the browser.

You can run import localforage from 'https://unpkg.com/localforage@1.9.0/src/localforage.js' in a browser, but that's because unpkg redirects paths without extension to the correct path with extension, it's not possible if you're fetching the module from a server without this feature.

One thing focalForage contributors can do is releasing a browser compatible ES version of the library bundled into a single file. Vite does that to improve performance. By doing is possible to import the module directly in a modern browser fetching just one file and anyone using a bundler can still use the library as usual. The hard thing is setting up the package.json so it don't break webpack, as happened with some libraries (I remember Mithril) when those started releasing ESM builds.

@nivle
Copy link

nivle commented May 11, 2021

Why is this still an issue...
djytw explained it well.
This should be fixed already. Just export the module, somewhere below..., and try the simple tests djytw did in her comment.

@tofumatt
Copy link
Member

Agreed—this is not a bug so I'm closing it. 👍🏻

@nivle
Copy link

nivle commented May 12, 2021

While it is not a bug, it would be nice to be able to import it in Javascript ES6. This is not possible atm as the package is not exported for ES6.(unless you already did in meantime)

check this link for more details on exporting for ES6...

@mikemaccana
Copy link

Still happening in current localForage, see #831 for some workarounds import localforage from "localforage/src/localforage.js" works for me.

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

No branches or pull requests

8 participants