[英]vscode live-server extension v5.6.1 fails to resolve 'axios' JavaScript import
I installed axios via npm and when I run the live-server extension it doesn't recognize the axios imports in my.js files.我通过 npm 安装了 axios,当我运行实时服务器扩展时,它无法识别 my.js 文件中的 axios 导入。 When I use the "import axios from 'axios'" command I get this error: "Uncaught TypeError: Failed to resolve module specifier "axios". Relative references must start with either "/", "./", or "../"."
当我使用“从'axios'导入axios”命令时,我收到此错误:“未捕获类型错误:无法解析模块说明符“axios”。相对引用必须以“/”、“./”或“.. /"." any ideas?
有任何想法吗?
package.json: package.json:
{
"name": "footballstatsil",
"version": "1.0.0",
"description": "A website for football statistics",
"default": "controller.js",
"type": "module",
"scripts": {
"dev": "parcel index.html",
"build": "parcel build index.html"
},
"author": "Dror Salomon",
"license": "ISC",
"dependencies": {
"axios": "^0.24.0",
"parcel": "^2.2.0"
}
}
import example:导入示例:
import axios from "axios";
import {
API_URL_PLAYERS,
API_URL_TEAMS,
API_KEY,
RAPIDAPI_HOST,
} from "./config.js";
const createOptionsObject = function (url, params) {
return options = {
method: "GET",
url: url,
params: params,
headers: {
"x-rapidapi-host": `${RAPIDAPI_HOST}`,
"x-rapidapi-key": `${API_KEY}`,
},
};
}
export const loadTeams = async function (leagueID, season) {
try {
const teamOptions = createOptionsObject(API_URL_TEAMS, { league: leagueID, season: season })
axios.request(teamOptions).then(function (response) {
response.data.response.forEach((el) => {
const teamsMarkup = `
<li class="dropdown-li"><a class="dropdown-item">${el.team.name}</a></li>`;
teamsSelector.insertAdjacentHTML('afterbegin', teamsMarkup);
});
});
} catch (err) {
console.error(err);
}
};
Well using skypack solved the problem:使用 skypack 很好地解决了这个问题:
import axios from 'https://cdn.skypack.dev/axios';
instead of代替
import axios from 'axios';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.