最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I resolve the issue of including module in JS? - Stack Overflow

programmeradmin0浏览0评论

I am trying to import Gemini API into my JavaScript module using the following line:

import { GenerativeModel, GoogleGenerativeAI } from "@google/generative-ai";

However, when I run my script containing this, Chrome Developer Tools console throws the following error:

Uncaught TypeError: Failed to resolve module specifier "@google/generative-ai". Relative references must start with either "/", "./", or "../".`

I was expecting the module to be included and the rest of the code to be executed.

I am trying to import Gemini API into my JavaScript module using the following line:

import { GenerativeModel, GoogleGenerativeAI } from "@google/generative-ai";

However, when I run my script containing this, Chrome Developer Tools console throws the following error:

Uncaught TypeError: Failed to resolve module specifier "@google/generative-ai". Relative references must start with either "/", "./", or "../".`

I was expecting the module to be included and the rest of the code to be executed.

Share Improve this question edited Jan 19 at 17:33 Daniel Manta 6,74317 gold badges42 silver badges48 bronze badges asked Jan 18 at 11:15 ShayeqShayeq 51 bronze badge 1
  • please don't post images of messages or code. Can you replace the image by a code block with the error message itself? – ruud Commented Jan 18 at 15:56
Add a comment  | 

1 Answer 1

Reset to default 1

From the error screenshot format i deduced that you're running your code in a browser. However, the format with @something (the anisation import), although correct for NodeJS, can't be recognized by browser scripts.
The best way to address this problem would be to use a bundler like Webpack or Vite which is able to convert imports like yours into ESM-compliant ones.
Alternatively, you may use a CDN for a different style of imports:

import { GenerativeModel, GoogleGenerativeAI } from "https://cdn.skypack.dev/@google/generative-ai";
发布评论

评论列表(0)

  1. 暂无评论