简体   繁体   English

vscode live-server extension v5.6.1 无法解析 'axios' JavaScript 导入

[英]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.

相关问题 为什么实时服务器会注入这些 javascript 代码 - why is live-server injecting these javascript codes 如何在 VSCode chrome live-server 中运行 npm 模块(puppeteer)? - How to run npm module (puppeteer) in a VSCode chrome live-server? 如何在运行实时服务器的情况下在 Visual Studio Code 中调试 JavaScript - How to Debug JavaScript in Visual Studio Code with live-server Running 如何在LIVE-SERVER上使用Javascript查找当前的html文件页面名称 - How can I find the current html filepage name using Javascript, on a LIVE-SERVER 使用npm live-server时是否清除localStorage? - Does the localStorage get cleared when working with npm live-server? 当我启动 live-server 时,TailwindCss 类没有反映出来 - TailwindCss classes are not reflected when i start live-server yarn eg:&#39;live-server&#39; 不被识别为内部或外部命令 - yarn eg:'live-server' is not recognized as an internal or external command 关闭和打开应用程序的实时服务器会删除我的本地存储 - closing and opening live-server for an app deletes my local storage 为什么我的按钮在 vscode 的实时服务器中无法正常运行?(javascript) - Why my buttons doesnt behave properly in live server in vscode?(javascript) 用于Javascript类型的VSCode扩展 - VSCode extension for Javascript types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM