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

Vite errors on import of @googlemaps/js-api-loader #15620

Open
7 tasks done
apparchsysad opened this issue Jan 16, 2024 · 12 comments
Open
7 tasks done

Vite errors on import of @googlemaps/js-api-loader #15620

apparchsysad opened this issue Jan 16, 2024 · 12 comments

Comments

@apparchsysad
Copy link

apparchsysad commented Jan 16, 2024

Describe the bug

I'm trying to use the Google Maps api-loader library to inject the Google Maps api script into my sveltekit webapp. The library was installed in my project with "npm i @googlemaps/js-api-loader" and is invoked in my webapp code with "import { Loader } from '@googlemaps/js-api-loader';".

When I run "npm run dev", the procedure fails with the following error :

22:04:04 [vite] Error when evaluating SSR module /src/routes/+page.svelte: failed to import "@googlemaps/js-api-loader"
|- SyntaxError: [vite] 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.

The message goes on to explain how I might try using a default export :

_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;_

But when I try this, the webapp itself fails telling me that the api-loader package doesn't provide a default export:

app.js:16 SyntaxError: The requested module '/node_modules/.vite/deps/@googlemaps_js-api-loader.js?v=5fcf71f8' does not provide an export named 'default'

Others have reported similar issues with 'npm run build' but as far as I can see I'm the only one with an 'npm run dev' problem

Reproduction

https://github.com/apparchsysad/test

Steps to reproduce

The test repository at https://github.com/apparchsysad/test was created as a skeleton webapp with the following npm commands:

npm create svelte@latest .
npm install
npm i @googlemaps/js-api-loader

The skeleton +page.svelte file was then modified to import the loader and display a Google map

System Info

System is a terminal session in VSCode on a Windows 11 PC

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red
Copy link
Member

sapphi-red commented Jan 17, 2024

The error happens with named imports because in SSR, the package is externalized and Node uses the main field that is a UMD file. Only default import can be used for UMD files.
The error happens with default imports because in non-SSR, the package is not externalized and module field is used and that is a ESM file. Default import doesn't exist in that ESM file.

There's three ways I can think of to use this dependency:

  1. Set ssr.noExternal: ['@googlemaps/js-api-loader'] in vite.config.ts
  2. Write a separate file for server and client
  3. Patch @googlemaps/js-api-loader

vite-plugin-cjs-interop might work as well.

@gajus
Copy link
Contributor

gajus commented Jan 19, 2024

I am facing similar issue.

Setting ssr.noExternal then produces error:

error:
  name:    ReferenceError
  message: module is not defined
  stack:
    """
      ReferenceError: module is not defined
          at eval (/Users/gajus/Developer/contra/gaia/node_modules/.pnpm/relay-runtime@16.1.0/node_modules/relay-runtime/index.js:12:1)
          at instantiateModule (file:///Users/gajus/Developer/contra/gaia/node_modules/.pnpm/vite@5.1.0-beta.2_@types+node@20.10.0_sass@1.69.0/node_modules/vite/dist/node/chunks/dep-BzxBS-ea.js:54170:15)
    """

@sapphi-red
Copy link
Member

@gajus That one is different from this issue. I think it's #14158.

@kelbyfaessler
Copy link

I also have this issue. Has anyone been successful with workarounds?

@kelbyfaessler
Copy link

ok so

Set ssr.noExternal: ['@googlemaps/js-api-loader'] in vite.config.ts

worked for me after I remembered to revert my import back to import { Loader } from '@googlemaps/js-api-loader';

@bshesh7
Copy link

bshesh7 commented Mar 23, 2024

were you able to come up with the solution?

@apparchsysad
Copy link
Author

apparchsysad commented Mar 23, 2024 via email

@bshesh7
Copy link

bshesh7 commented Mar 23, 2024

owed how a script in app.html could inject the Google map api into the browser so that Svelte code can

Can you please mail me @ bshesh7@gmail.com please

@apparchsysad
Copy link
Author

apparchsysad commented Mar 24, 2024 via email

@sam-w
Copy link

sam-w commented Apr 18, 2024

The fix that worked for me was quite simple. Applies to SvelteKit specifically, but may also apply to Vite users in general:

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

This worked for me for both dev and build, SSR, prerendered, whatever.

Interesting side note: for js-api-loader 1.16.6, at least, doing the above results in a slightly different (and by the looks of it, more up-to-date) type for Loader:

import { Loader as OldLoader } from '@googlemaps/js-api-loader';

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

(new OldLoader(someParams)).loadCallback(callback); // fine
(new Loader(someParams)).loadCallback(callback); // deprecation warning

@Keagel
Copy link

Keagel commented Apr 21, 2024

The fix that worked for me was quite simple. Applies to SvelteKit specifically, but may also apply to Vite users in general:

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

This worked for me for both dev and build, SSR, prerendered, whatever.

Are you saying that worked for you without adding ssr.noExternal: ['@googlemaps/js-api-loader']? Personally I get a TypeError: Loader is not a constructor message when using your example (also using SvelteKit). Adding the noExternal to vite.config.ts does the trick, but at that point the import * as gmapsLoader workaround is no longer needed.

@elton971
Copy link

elton971 commented Jul 9, 2024

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

No branches or pull requests

8 participants