记录一个ES6导入模块的小bug

2,968 阅读1分钟

问题

在导入css文件jq的时候,说了这个

import './app1.css'
import './reset.css'
import $ from 'jquery'

index.html:1 Uncaught TypeError: Failed to resolve module specifier "jquery". Relative references must start with either "/", "./", or "../".

或者

Access to script at 'file:///C:/Users/A.。。。。。。。。。。。。。。。。main.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

或者

GET file:///C:/User。。。。。。。。。。。。。。main.js net::ERR_FAILED

是因为直接打开的这个文件,或者在vscode里右键Live Server打开才导致的这个

解决方法:

用parcel打开

parcel src/index.html

总结

网页如果是通过浏览器直接打开和步入到服务器中,

在服务器打开实际上是不一样的,

如果把网页部署到服务器中再打开就会避免很多不必要的问题,所以平常一定要养成用服务器打开的习惯。