Dawid Zbiński Dawid Zbiński - 2 months ago 30
Node.js Question

No 'Access-Control-Allow-Origin' header on https://npmcdn.com/ng2-img-cropper/index.js website

While I was working on my app I began getting this error:

XMLHttpRequest cannot load https://npmcdn.com/ng2-img-cropper/index.js. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

I was thinking about what's causing the error. I know there is a https://npmcdn.com/** url, so it's probably not in my computer, but I just wanted to be sure.

Also, I've tried accessing the site directly from the browser and everything works as it should. It gives me back the script etc. It's just weird for me because I was actually working with Access-Control-Allow-Origin on my back-end when I began getting this one, but I've tried to comment out everything and I still get it. I was considering something happened because of my .htaccess file, that I was editing too. May I know your opinion what's going on here? Is it possible my computer or connection is causing it?


The problem I have is with node_module ng2-img-cropper. I didn't want to publish it before I wasn't sure it's not an issue on my side. I found out that this node_module is using the old npmcdn.com website to load it's files. It always calls npmcdn.com which redirects it to unpkg.com. Next, if you try GET Request on npmcdn.com it redirect you to the unpkg.com and even though it gives me all the data, and headers are set, it probably checks the headers from the npmcdn.com site and says that I'm unable to catch it. Any idea how to solve this? I was thinking about changing the config in npm, so that every node_module would call unpkg.com, and not the older one npmcdn.com. But where to find this config file?


My only solution was not the best, but this way it worked. I copied the whole module from node_modules folder and put into my src/app/ location. Then in component I've just imported component from it's path in my app folder and added the /index so it's grabbing exports from index.ts.