Browser Friendly Relative Imports with .js extension
See original GitHub issueAll modern browsers (including firefox) support es modules, but there is not ‘module resolver’ on the web, which means that browsers require relative URLs for module files.
import foo from 'foo' // sad browser 😢
import bar from '../bar/bar.js' // happy browser 😃
For users that do not need a complicated (or any) build step, apollo-client should provide a browser-friendly package with relative imports. This can be mechanically prepared.
Intended outcome: This Just Works:
<script type="module">
import { ApolloClient } from '/node_modules/apollo-client/ApolloClient.js';
</script>
Actual outcome:
Chrome 64:
Uncaught TypeError: Failed to resolve module specifier "apollo-link". Relative references must start with either "/", "./", or "../".
How to reproduce the issue:
Same as Intended outcome
Version
- apollo-client@2.2.5
Notes I was unable to find a rollup plugin which would do this automatically, but I’m reasonably confident that such a plugin would not be hard to implement.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:10 (4 by maintainers)
Top Results From Across the Web
import - JavaScript - MDN Web Docs - Mozilla
This is often a relative or absolute URL to the .js file containing the module. In Node, extension-less imports often refer to packages...
Read more >Appending .js extension on relative import statements during ...
I usually just use the . js extension in import statements in typescript files as well and it works. Not using a file...
Read more >The JavaScript Modules Handbook – Complete Guide to ES ...
A JavaScript module is a file that allows you to export its code. This allows other JavaScript files to import and use the...
Read more >Documentation - Module Resolution - TypeScript
Similarly, a non-relative import will follow the Node.js resolution logic, first looking up a file, then looking up an applicable folder. So import...
Read more >Modules • JavaScript for impatient programmers (ES2022 ...
Relative paths, absolute paths, and URLs work as expected. They all must point to real files (in contrast to CommonJS, which lets us...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’d be very interested in this, since I’m porting many apps from Polymer 2 to Polymer 3 (i.e. ES modules) and I still need to bundle Apollo Client to make it work (see e.g. #3571).
Hope to see this reconsidered to be able to use Apollo Client with ES module as per spec!
For others having this problem, I followed Benny’s link to pikapkg and from there to https://github.com/pikapkg/web, and using this I created a package in web_modules for
apollo-client
, as well asgraphql
. I can now use the former to import ApolloClient in a web module. That’s as much as I have time for now but thought it might help someone.