0

I am looking for some help understanding something on a project I am working on. I have written some code that is functioning, though I am not sure why.

In a Node.js server, in /public/js there are two scripts. One (file1.js) has a function func(). file2.js, in the same directory, calls func() successfully. There is no module.exporting or requireing anywhere, yet the two files work together. They are both referenced in the index.ejs file, however. Is this where they become able to communicate?

//file1.js
function func() {
    console.log("foo")
}
//file2.js

func()
//index.ejs
...
<script src="public/js/file1.js"></script>
<script src="public/js/file2.js"></script>
...

I've spent all day reading and cannot find anything on this topic.

6
  • 3
    The script tag it causing the logic to retrieve and include the sources of the files in the file, as if they were inline. Once the file1.js is processed, all the resources that it defines globally will be available from that point onwards. Commented May 27, 2019 at 17:12
  • 1
    "Is this where they become able to communicate?". Absolutely. Commented May 27, 2019 at 17:13
  • 2
    I think the confusion here is the "called from another file". It's not called from another file. That file is retrieved and processed in your main file. The actual execution of the call is from the main file. Commented May 27, 2019 at 17:15
  • Terrific, thank you! Commented May 27, 2019 at 17:15
  • Follow up, then why is module.exports ever necessary? Commented May 27, 2019 at 17:15

2 Answers 2

2

Your question is about how JavaScript works in a browser.

Node.js isn't relevant here. All it is doing is running an HTTP server program that gives static files to the browser.

When you load a script into a browser using a script element (and don't use type="module"), any variable in the outer most scope of the script file (e.g. which isn't let inside a block or var inside a function) becomes a global and is accessible to any other script loaded into the same HTML document that way.

Globals are messy and a good way for different bits of code to accidentally interfere with each other so modern JavaScript generally avoids using them. This style of JavaScript programming wasn't common when JS was first implemented in browsers: Hence the behaviour described above.

This is why people started to use techniques like the revealing module pattern and why AMD and Node modules were designed before standard JavaScript modules were added to the specification.

Sign up to request clarification or add additional context in comments.

Comments

1

You must understand how the global space behaves in Javascript.

This Code:

<script src="public/js/file1.js"></script>
<script src="public/js/file2.js"></script>

Is the same as this:

<script>
    //file1.js
    function func() {
        console.log("foo");
    }

    func();
</script>

Because soon as file1.js is loaded, everything that is defined inside of it, becomes available anywhere in that page from where it was included.

Since file2.js uses contents of file1.js, it will work because func is available to be used anywhere below file1.js inclusion.

6 Comments

Thanks! Then why is module.exports() and requiring a local file ever necessary? Is it because those sorts of files aren't listed in the index.ejs file?
I think you're confusing something here. With module exports you want the contents you're exporting to be available using the 'import' call. So this answer is barely just for your normal application and not associated to any modules.
Those modules you're referring to are based on the server. and Not on the browser. So you can't put a file you're needing to use on the browser on service modules.
@jsarbour You should check my comment in the question. It has a link to an article which might help you understand these concepts.
Yes require() not import, Sorry man. lol I do a lot of Type Script with Angular that's why. SOrry about that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.