Closed mark8044 closed 4 months ago
Hey,
you don't need requirejs. You have to import it as ES module. If your structure does not support modules, you should use the commonjs import.
File A with ES module import:
// this imports the default parser
import bbCodeParser from 'js-bbcode-parser';
// this import the class constructor of the parser
import BBCodeParser from 'js-bbcode-parser/src/index.js';
File B with commonjs import:
// this imports the default parser
const bbCodeParser = require('js-bbcode-parser');
// this import the class constructor of the parser
const BBCodeParser = require('js-bbcode-parser/src/index.js');
Bye :)
Thank you for the direction, I think there is some progress. Since I do not use node.js, It sounds like File A -> Bullet 2
is the correct one for me
Doing:
// this imports the default parser
const bbCodeParser = require('js-bbcode-parser');
// this import the class constructor of the parser
const BBCodeParser = require('js-bbcode-parser/src/index.js');
I end up with the following browser error:
Uncaught SyntaxError: Cannot use import statement outside a module
So then doing what you said it sounds like my core JS that does the module load, itself must be a module, so I added type="module"
to my core JS file
<script type="module" src="index.js"></script> <--- index.js does: import bbCodeParser from 'js-bbcode-parser';
Now I end up with error:
Uncaught SyntaxError: Unexpected identifier 'bbCodeParser'
In case it matters, this is all done in the context of jQuery:
$(document).ready(function() {
//this imports the default parser
import bbCodeParser from '/forums/bpeditor/htmlbbcode/js-bbcode-parser';
//this import the class constructor of the parser
import BBCodeParser from '/forums/bpeditor/htmlbbcode/js-bbcode-parser/src/index.js';
.... etc
});
Ok it looks like loading all this inside jQuery was the issue, I just moved the import lines outside the document.ready and it all works.
Thank you on the education with modules! 👍 🥇
You are welcome.
imports are always at the top of a module and out of any blocks.
But you can use dynamic imports, if you want to import something inside a block
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
Im having a heck of a time getting this to run.
Leads to error:
After some googling it seems I might need
require.js
?Tried it and after more errors I get to this point:
Which then brings me to error:
Totally confused here. Is this an issue that I need to have node.js installed on a server? Never used it before and prefer not to