Sorry for late reply, but just realised I’d written some stuff and not posted:
The CORS error isn’t stupid. It’s frustrating if you don’t know why you’re getting it (here I would strongly suggest getting the basics of how browser networking works down), but you shouldn’t be getting it.
CORS stands for Cross Origin Request Sharing: it is a mechanism that is implemented on a server. At core, it’s just a way to specify allowed addresses that a computer can share things with when a browser makes an HTTP request from them. Not on the list, your browser don’t get the thing.
More specifically, the server specifies
- which domains are allowed. Here, the domain is
freecodecamp
. Then forum
is a subdomain, and org
is a top-level domain. So if freecodecamp
was specifically allowed, then [unless specified otherwise] any requests from forum.freecodecamp
would also be allowed.
- which schemes are allowed. The scheme in turn specifies which protocol is being used – eg
http://my-site.com
, “http” is the scheme, from which is can be inferred that the protocol being used is HTTP.
- which ports are allowed. The port is a number, like
http:://localhost:4000
, the port is 4000.
So a computer on a network makes a request to another computer, with a different address, for a resource: in this case, a JSON file.
The computer to whom the request is made must be configured to allow it to complete the request and send that JSON file. In that case, w/r/t CORS, the first computer, the one making the request, it’s address must be whitelisted to allow the data to be transferred.
jsonkeeper is a hosting service for JSON files. It’s CORS policy will be very forgiving: it’s likely to be basically “allow anyone who requests a JSON file have it”.
in your case you are using JS modules. You have to run a server locally when you are developing with them. If you aren’t running a server, the browser will look at that import
and see that the protcol is not HTTP or similar, and throw a CORS error:
- if you running a local server, and the file you’re importing is local to what’s being served (ie it is in the same folder, either adjacent to or in a subdirectory), then
import "./my-module.js"
will be read as something likeimport "http://localhost:4000/my-module.js"
- if you are not running a local server and you try to just open the HTML file referencing the JS in a browser, it will be read as
import "file:://some/path/from/the/root/to/my-module.js"
This will not work, you cannot just import
arbitrary files from a computer using the file
protocol because that would be an insane security risk.
- if you are running a local server, and the file you’re importing is not local to whatever is being served – eg you are serving whatever is in a folder “projects/my-project/”, but
my-module.js
is in “projects/”, so a directory above – that will also fail, because, again, trying to import a file from the computer’s local filesystem, outside of what is being served.
The error message when you tried to import a local file matches 2 or 3, but because it’s not been explained what you’re doing in development, it’s not possible to determine. Because the import from jsonkeeper is also failing, I would assume 2 (ie you are not running a server), but there may be some other issue.