That’s because you’re not using a server. Many (if not all) browsers block requests that come from different origins, even if that origin is the local file system.
Create a basic server that just serve that page and it should work. Or upload the project to a hosting (github pages, for instance).
Nevermind, I got the server to work, and…it didn’t work. CORS error is gone but I still get the error about not being able to use import statement outside a module
I am trying to make…*drumroll please*…a content management system (don’t ask why)!
I have written a program that makes an ajax request to a php file that retrieves the contents of another html file. I want to put that html in a div so that the user can see the rendering of the other html file in real time while they add or delete content in a custom terminal I made with JavaScript. The problem is I don’t want to put all the html in the div because I figure the dom interpreter might get confused (with 2 different sets of < html> tags and whatnot), so I wanted to use JavaScript dom manipulation on said html to retrieve what’s important before putting it in the div. Does that make sense?
(And yes, I’m aware that there’s [apparently] a dom interpreter library in php. It doesn’t work for me so far. I’m using WAMP).
I’m kind of surprised that what I need doesn’t exist for front end…
Anyway, using the syntax browserify main.js -o theFileWithRequireInIt.js
I got a browserified version of jsdom into main.js, and it has a lot of statements like this:
exports.EncoderBuffer = EncoderBuffer;
I assumed there might be some sort of export object that is exported at the end…otherwise what’s the point of having used browserify? They mean to tell me I have to go through this 205,847 line file and export each function manually? Ridiculous!
So, perhaps you could offer some insight.
Is it possible to use npm modules in the browser? If so, how?
How would you go about parsing the html? I thought JavaScript dom manipulation would be easier for this…
It does, but the normal answer is use an iframe. I understand why you’re doing this, it just seems the more difficult way to do it (definitely much more complex). You could just render the HTML as an HTML page, and view that page in the iframe
But that difficulty kinda pales in comparison to trying to use JS modules in the browser in the way you are. You are making life staggeringly difficult for yourself. It’s a good thing to try out, but this technology is in its infancy. Nothing is really currently written to make this easy.
There is, that’s what the require/module.exports is, but that’s CommonJS. JSDom (or basically any Node module) isn’t written as an uncompiled ES6 module. There would be no point – Node didn’t really support them fully up until a few months ago, and even then that support comes with caveats. To work out-of-the-box in a browser as a script type="module", a package needs to be all ES6, with import and export statements throughout. Because most libraries are written using ES6 module syntax nowadays, if you can get at the source (rather than the compiled distribution version) it can sometimes be fudged, but that’s often as good as you’ll get
Yes, Browserify was designed to do that, and nowadays Webpack or Parcel (or Rollup for libraries) are generally used to do the same job. They take a load of modules, some written in ES6 module syntax and some in CommonJS, and bundle them up into one file (or several chunked files) that you can then use in a browser. They aren’t designed to take arbitrary modules, translate them to ES6 if necessary, then spit out the results as a set of modules you can then use in-browser as set of scripts of type module. As far as I know nothing exists that provides a toolchain to automate that process.
Personally? Ajax request sent → backend generates HTML page to your specs at some URL → page is rendered in an iframe pointing at that URL → force refresh the iframe.
You.may have some complex requirements that make using JSDom very important, but afaics above should fit the bill