question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Browser Friendly Relative Imports with .js extension

See original GitHub issue

All 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:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
heruancommented, Jun 13, 2018

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!

2reactions
finelinecommented, Mar 4, 2019

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 as graphql. 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found