[英]Can't generate Nuxt website with @googlemaps/js-api-loader
I am using @googlemaps/js-api-loader
in my Nuxt 3 website.我在我的 Nuxt 3 网站中使用
@googlemaps/js-api-loader
。 Everything works fine in local development, but when I try to build the project with nuxt generate
(no matter if locally or on Vercel) I'm getting following error:在本地开发中一切正常,但是当我尝试使用
nuxt generate
构建项目时(无论是在本地还是在 Vercel 上)我收到以下错误:
[nuxt] [request error] Named export 'Loader' not found. The requested module 'file:///path/to/website/node_modules/@googlemaps/js-api-loader/dist/index.umd.js' 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:
The important part of loading script looks like this:加载脚本的重要部分如下所示:
import { Loader } from '@googlemaps/js-api-loader';
const loader = new Loader({
apiKey: config.googleMapsApiKey,
version: 'weekly',
});
onMounted(async() => {
await loader
.load()
...
so I tried to import this package differently, eg:所以我尝试以不同的方式导入这个包,例如:
import * as gmaps from '@googlemaps/js-api-loader';
const { Loader } = gmaps;
and the previous error disappeared, but now I'm getting以前的错误消失了,但现在我得到了
[Vue warn]: Unhandled error during execution of setup function
at <DynamicLocations class="contact__map" locations= [
{
id: 1,
...
[nuxt] [request error] gmaps.Loader is not a constructor
at setup (./.nuxt/prerender/chunks/app/server.mjs:5536:20)
at _sfc_main$t.setup (./.nuxt/prerender/chunks/app/server.mjs:5582:25)
at callWithErrorHandling (./.nuxt/prerender/chunks/renderer.mjs:2654:23)
at setupStatefulComponent (./.nuxt/prerender/chunks/renderer.mjs:9548:30)
at setupComponent (./.nuxt/prerender/chunks/renderer.mjs:9503:12)
at renderComponentVNode (./.nuxt/prerender/chunks/renderer.mjs:12068:17)
at Object.ssrRenderComponent (./.nuxt/prerender/chunks/renderer.mjs:12504:12)
at ./.nuxt/prerender/chunks/app/server.mjs:5628:36
at renderComponentSubTree (./.nuxt/prerender/chunks/renderer.mjs:12149:13)
at renderComponentVNode (./.nuxt/prerender/chunks/renderer.mjs:12084:16)
I also can't import package by default export.我也无法默认导出导入包。 Do you have any ideas what's going on and how can I fix this?
你有什么想法发生了什么,我该如何解决这个问题?
I found a documentation page related to this problem and there is the following information:我找到了与此问题相关的文档页面,并且有以下信息:
If you encounter these errors, the issue is almost certainly with the upstream library.
如果您遇到这些错误,几乎可以肯定是上游库的问题。 They need to fix their library to support being imported by Node.
他们需要修复他们的库以支持被 Node.js 导入。
Although they provide a solution to get rid of errors by adding the package to build.transpile
:尽管他们通过将包添加到
build.transpile
来提供解决错误的解决方案:
build: {
transpile: ['@googlemaps/js-api-loader'],
},
It worked for me它对我有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.