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

516 阅读1分钟

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

<!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>

报错如下:

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

image.png

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