npm安装anywhere( 随启随用的静态文件服务器),分分钟解决浏览器请求本地js文件跨域问题

以下是一个html文件, chrome浏览器打开该文件;

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <link rel="icon" href="/favicon.ico">
    <meta name="viewport" content="width=600, initial-scale=1.0">
    <title>Vite App</title>
    <script type="module" crossorigin src="/assets/index-GxicIufr.js"></script>
    <link rel="stylesheet" crossorigin href="/assets/index-U9LxSyeS.css">
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>

报错如下:

js 复制代码
Access to script at 'file:///Users/***/Documents/code/learning/vue/vue-project/dist/assets/index-GxicIufr.js' 
from origin 'null' has been blocked by CORS policy: 
Cross origin requests are only supported for protocol schemes: 
http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.
index.html:8 
 Access to CSS stylesheet at 'file:///Users/shuangguiyu/Documents/code/learning/vue/vue-project/dist/assets/index-U9LxSyeS.css' 
 from origin 'null' has been blocked by CORS policy:
 Cross origin requests are only supported for protocol schemes:
 http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.
index.html
        

全局安装anywhere npm install -g anywhere,再前往html文件所在目录执行anywhere -p 8081

Anywhere 随启随用的静态文件服务器。 www.npmjs.com/package/any...

相关推荐
美酒没故事°10 分钟前
npm源管理器:nrm
前端·npm·npm源
用户221520442780011 分钟前
vue3组件间的通讯方式
前端·vue.js
三十_A29 分钟前
【实录】使用 patch-package 修复第三方 npm 包中的 Bug
前端·npm·bug
下位子37 分钟前
『AI 编程』用 Claude Code 从零到一开发全栈减脂追踪应用
前端·ai编程·claude
tyro曹仓舒37 分钟前
Vue单文件组件到底需不需要写name
前端·vue.js
用户479492835691537 分钟前
面试官:讲讲2FA 双因素认证原理
前端·后端·安全
乐影38 分钟前
TS 模板字符串类型:从基础到进阶的类型编程魔法
前端·typescript
龙在天39 分钟前
CSS 属性值的计算与过程
前端
云鹤_40 分钟前
【Amis源码阅读】组件注册方法远比预想的多!
前端·低代码
xinfei42 分钟前
ES6 新特性 从 ECMAScript 2015(ES6)到 ECMAScript 2025
前端