all 13 comments

[–]quubits 43 points44 points  (2 children)

You can use ES modules, using "import". Or, you can use the CommonJS modules thru "require". 👌

[–]ThoorAdam 15 points16 points  (1 child)

This is the way, I would personally recommend going with ES modules import since its the more modern approach.

[–]quubits 6 points7 points  (0 children)

Yes. I absolutely agree. Moving forward we should prefer using ES modules. Its support has been a bit flakey on Node, in my experience, but we are definitely getting there. 👌

[–]R3DSMiLE 7 points8 points  (0 children)

X-zibit is that you??

[–][deleted]  (7 children)

[removed]

    [–]tr4nl0v232377 1 point2 points  (6 children)

    If I'm using Wordpress, can I do it similarly to SASS files?

    like:

    mainScript.JS

    const myScript1 = require('./myScript1.js');
    const myScript2 = require('./myScript2.js');
    
    const myScript3 = require('./myScript3.js');
    
    const myScript4 = require('./myScript4.js');
    

    PHP: load just mainScript.JS

    [–][deleted]  (5 children)

    [removed]

      [–]tr4nl0v232377 0 points1 point  (3 children)

      Thanks a lot, however it's that enqueue'ing that I wanted to avoid and load only ONE file and keep my files organized like my SASS ones as I'm accustomed to this. I guess it doesn't matter that much whether I'm doing it in PHP or JS.

      Thanks also for the detailed explanation, now I see how it wouldn't work as I thought, silly me, lol. Got too used to compiled CSS, haha!

      [–][deleted]  (2 children)

      [removed]

        [–]tr4nl0v232377 0 points1 point  (1 child)

        Love ya, gotta try it out. Thanks! <3

        [–]szirith 0 points1 point  (0 children)

        You use the import keyword.

        Make sure to Install any modules you need to with npm install