KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Serving .mjs (javascript ES Modules) in 4D Web Applications
PRODUCT: 4D | VERSION: 21 | PLATFORM: Mac & Win
Published On: January 27, 2026
Browsers enforce strict MIME type checking for JavaScript ES modules (.mjs), and when such files are not served with a JavaScript MIME type they are rejected with a Failed to load module script error. By default, 4D does not associate the .mjs extension with a JavaScript MIME type and may return application/octet-stream, which is not accepted for module scripts. The recommended solution is to intercept .mjs requests in the On Web Connection method, load the file manually from the WebFolder, and explicitly set the HTTP header to Content-Type: text/javascript before sending the response. This server-side approach works in client/server and Remote contexts and avoids relying on internal MIME mappings. Verification can be done by importing the .mjs file from an HTML page using script type=module and confirming in the browser developer tools that the response header is Content-Type: text/javascript and that no module loading errors occur.

On Web Connection Database Method
#DECLARE($url : Text; $headers : Text; $browserIP : Text; $serverIP : Text; \
$user : Text; $password : Text)

var $mjs : Text
$mjs:="Content-Type: text/javascript"

If (Position(".mjs"; $url)>0)
   var $filePath : Text
   $filePath:=Get 4D folder(HTML Root folder)+"mjs"+\
     Replace string($1; "/"; Folder separator)
   If (Test path name($filePath)=Is a document)
     var $content : Text
     $content:=Document to text($filePath; "UTF-8")
     WEB SET HTTP HEADER($mjs)
     WEB SEND TEXT($content)
   Else
     WEB SEND TEXT("404. Page not found"; "text/plain")
   End if
End if