写了一个简单的js代码,需要引第三方包
# js代码路径:e:\ai\Stable-Diff\src\jsClient\gradio_demo.js
import { client } from "@gradio/client";
async function run() {
const app = await client("https://modelscope.cn/api/v1/studio/AI-ModelScope/Stable_Diffusion_XL_1.0/gradio/");
const result = await app.predict(0, ["Howdy!", "Howdy!", "None", 512, 512, 1, 25, 1]);
console.log(result?.data);
}
运行报错,SyntaxError:Cannot use import statement outside a module
e:\ai\Stable-Diff\src\jsClient\gradio_demo.js:1
import {client }from "@gradio/client";AAAAA
SyntaxError:Cannot use import statement outside a module
解决办法
step1: 把需要引的第三方包放到import的包查找路径
使用console.log(module.paths)看import的找包路径
e:\ai\Stable-Diff\src\jsClient\node_modules,
e:\ai\Stable-Diff\src\node_modules,
e:\ai\Stable-Diff\node_modules,
e:\ai\node_modules,
e:\node_modules,
可以看到,import是从当前js文件目录向上一直找到根目录的node_modules文件夹,所以把需要引的包放到任一查找路径即可