how to require js file in a src file inside – Devinfo_DH Nov 22 '21 at 14:14

  • @Andy Well, it's look like it's working, but even spliting it like this: ```html ``` I still have pb – Devinfo_DH Nov 22 '21 at 14:14
  • @Devinfo_DH I don't see the difference between your question and this one: you want to require a second JS file from inside the first one; the linked duplicate discusses all the possible ways of doing that. – IMSoP Nov 22 '21 at 14:15
  • @IMSoP So I must have miss something, sorry. I'll read it again Or maybe it's because it's not importing a js file into another one, but the `require` is working, but not when I'm using the 1st JS file in script tag – Devinfo_DH Nov 22 '21 at 14:17
  • Maybe _I'm_ missing something. What does `require("another/file.js")` mean _other than_ importing a JS file into another one? And where are you running it _other than_ in a script tag? – IMSoP Nov 22 '21 at 14:22
  • I'm using a JS class in my html file, and I need to import it. So I've to use `src` attribute. But in this class, I also need to use module from another JS file, so there is a `require()` at line 1 The problem is the `require()` that trigger the error msg – Devinfo_DH Nov 22 '21 at 14:30
  • @Devinfo_DH So, you have a JS file (the one you reference in the `src` attribute) and it needs to use another JS file (the one in the `require` line). That's exactly what the linked question is talking about. The error message is triggered because there is no function in standard JS called "require". What I don't understand is what you mean by "the require is working" - _when_ is that line working? – IMSoP Nov 22 '21 at 14:51
  • 2 Answers2

    1

    require is a built-in function provided by JS environments that support a couple of different kinds of modules, so how you load your JS file into a browser depends on what type of module system it is written to use.

    The most likely cases are:

    • It is is a AMD module (very unlikely in 2021) in which case you can probably load it with RequireJS
    • It is a CommonJS module that depends on Node.js-specific APIs (in which case it can't run in a browser and to interact with it in a browser you would need to build it into a web service and make HTTP requests to it (e.g. via Ajax)). Some things that depend on Node.js-specific APIs include:
      • Making HTTP requests to sites which don't grant permission for browser JS to access them using CORS
      • Non-HTTP network requests (like direct access to a MySQL database)
      • Reading (or doing anything with) files from a file path expressed as a string (as opposed to reading files from a <input type="file">)
      • Spawning other processes
    • It is a CommonJS module that doesn't depend on Node.js-specific APIs and you can convert it to run in a browser using a bundler tool such as Webpack or Parcel.

    Find out which of those options it is before you start trying to implement one of these solutions (all of which will take some time and effort that you don't want to waste).

    Reading the documentation for a module will usually tell you. If you get it from NPM and it doesn't mention being browser compatible then it is probably Node.js only.

    Quentin
    • 914,110
    • 126
    • 1,211
    • 1,335
    • I won't lie, it's a project made by people more experts than me, and I don't really know the different type of modules. I think it's a CommonJS module, but I'm not 100% sure and I can't say if it depends on Node.js-specific APIs – Devinfo_DH Nov 22 '21 at 14:32
    • Try reading its documentation. – Quentin Nov 22 '21 at 14:47
    • I'll read it when I'll be at home, for my own, thx ^^ Someone help me with my problem, I won't be able to say 100% of what we did in SO, but it's similar to your answer. So I'll mark your answer as correct. I can't explain how we do without showing some private content, so I'm sry. – Devinfo_DH Nov 22 '21 at 15:10
    -1

    This might be because require() is not part of the standard JavaScript API. Your code might be using Nodejs, which is where require() might be used. Also, for you to choose an src file, you might also want to include the type which is <script type="text/javascript">.

    gskhaled
    • 34
    • 4