googlemaps / js-api-loader

Load the Google Maps JavaScript API script dynamically.
Apache License 2.0
357 stars 64 forks source link

Remix support #821

Open mikecousins opened 10 months ago

mikecousins commented 10 months ago
import { Loader } from "@googlemaps/js-api-loader";
         ^^^^^^
SyntaxError: Named export 'Loader' not found. The requested module '@googlemaps/js-api-loader' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@googlemaps/js-api-loader';
const { Loader } = pkg;

Getting this error using Remix/ESM. Any idea how to get around this? The suggested config changes for nuxt/svelte don't work in Remix.

wangela commented 10 months ago

If you would like to upvote the priority of this issue, please comment below or react on the original post above with :+1: so we can see what is popular when we triage.

@mikecousins Thank you for opening this issue. 🙏 Please check out these other resources that might help you get to a resolution in the meantime:

This is an automated message, feel free to ignore.

ahockersten commented 9 months ago

@mikecousins you can put @googlemaps/js-api-loader in your serverDependenciesToBundle in remix.config.js in order to use this in Remix to get around this, see: https://remix.run/docs/en/main/file-conventions/remix-config#serverdependenciestobundle

mikecousins commented 9 months ago

@ahockersten that's not available in certain Remix setups like Vite/SPA mode.

mikecousins commented 9 months ago

Here's a good linter you can use to help fix the package: https://publint.dev/@googlemaps/js-api-loader

johny commented 4 months ago

Until this is properly fixed, this worked for me:

import * as pkg from "@googlemaps/js-api-loader";
usefulthink commented 1 month ago

I'm using it all the time with exactly this import {Loader} from '...'; syntax without any problems.

An interesting point is, the error-message says The requested module '@googlemaps/js-api-loader' is a CommonJS module, which tells me that whatever bundler you're using isn't loading the ESM-version of the package but instead (presumably) the UMD-version.

I'm not sure if there is anything we can do about this, since it has probably something to do with the configuration of the bundler.

soungrong commented 1 week ago

@ahockersten that's not available in certain Remix setups like Vite/SPA mode.

Not sure about support at the time of your reply, but it's possible with Vite/SPA mode https://remix.run/docs/en/main/guides/spa-mode#cjsesm-dependency-issues