Multiple scripts without leaking into eachother.

Is it possible to have multiple script tags/files with separate require statements without raising an error that the thing is already defined. having all the imports in one script works but then the autocomplete doesn't work. I suppose changing it to let would work but sounds like a bad way to do things for example:
<script src="1.js"/>
<script src="2.js"/>
<script src="1.js"/>
<script src="2.js"/>
1.js:
const process = require('process')
1.js:
const process = require('process')
2.js:
const process = require('process')
2.js:
const process = require('process')
'error process already defined in 1.js'
'error process already defined in 1.js'
9 Replies
MartynasXS
MartynasXSOP•3y ago
Don't know how else to put it or how to search for it
Jochem
Jochem•3y ago
isn't this what build tools are made to solve? I've never really looked into configuring build tools, I kinda skipped that step and went from basic vanilla JS straight to using frameworks with build tools preconfigured, but I'm pretty sure that's what build tools would do
MartynasXS
MartynasXSOP•3y ago
I have no idea, would prefer not to use a framework
Jochem
Jochem•3y ago
build tools aren't frameworks though 🙂
MartynasXS
MartynasXSOP•3y ago
yeah ik
Jochem
Jochem•3y ago
as for googling it, I'd suggest searching for "javascript require once". PHP has a function for this called require_once, that detects if a file has already been included, and there's a lot of PHP devs writing javascript too, so there's some suggestions If I had to roll my own, and controlled all the code, I'd put each require in an if-statement that checked if the variable for that require was already defined
if (!process) const process = require('process');
if (!process) const process = require('process');
or even maybe
const process = process || require('process');
const process = process || require('process');
MartynasXS
MartynasXSOP•3y ago
this might be the best solution i guess
Jochem
Jochem•3y ago
it's not something I'd do if I didn't write all the code that might have those requires, but it seems like you do not sure if process || require('process'); or process ?? require('process'); would work better though, I'd probably try the nullish coalescing operator first
MartynasXS
MartynasXSOP•3y ago
ill try giving rollup a try first
Want results from more Discord servers?
Add your server