Unexpected identifier in using Export & Import in Javascript Modules

Jose Daniel Navarro Brito 61 Reputation points
2025-04-29T04:55:15.8233333+00:00

Hi there:

It seems a trivial issue but I'm stuck and don't know why:

I'm using Import/Export in two my JavaScript files. For simplicity sake ...the "mother"js.file has a function

export function ShowAlert(val) {
    alert(val);
}


There isa razor view ( cshtml file) that refers to the "mother" js file above as follows:

<script type="module" src="~/js/mother_file.js"></script>

Now...this cshtml file which is a view in my project, refers also to an external script that for simplicity sake I name child.js file. Up to this point, my script section of my cshtml file reads

<script type="module" src="~/js/mother_file.js"></script>
<script type="module" src="~/js/child_file.js"></script>

My understanding is that when I render the view ( open the cstml file) the mother and child js files talk to each other ...am I correct?

So in the child_file.js I refer to the ShowAlert function that is in the mother_file as follows:

$(document).ready(
     function () {
        import ShowAlert from './js/mother_file.js
})

When I run the project, the browser complains that the function isn't there with an Unexpected identifier error.

What I'm doing wrong?

Thanks

ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
420 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. SurferOnWww 4,391 Reputation points
    2025-04-29T07:28:27.9266667+00:00

    Please confirm if the Content-Type included in the response header is text/javascript.

    For details please read the MDN Document JavaScript modules.

    To get modules to work correctly in a browser, you need to make sure that your server is serving them with a Content-Type header that contains a JavaScript MIME type such as text/javascript. If you don't, you'll get a strict MIME type checking error along the lines of "The server responded with a non-JavaScript MIME type" and the browser won't run your JavaScript.


  2. Bruce (SqlWork.com) 75,051 Reputation points
    2025-04-29T20:39:50.28+00:00

    modules are external code. to use the code in a module it must be imported. in your sample both mother and child are modules. to execute any code in them you need script running in the browser to import from the module(s), and call the code.

    you child.js probably should not be a module and it uses import incorrectly. it should be:

    <base href="~/">
    <script type="module" src="./js/mother_file.js"></script>
    <script src="./js/child_file.js"></script>
    

    child.js

    import { ShowAlert} from "./js/mother_file.js";
    document.addEventListener("DOMContentLoaded", event => ShowAlert("Page Loaded"));
    

    or with jQuery (be sure its loaded before the script call)

    import { ShowAlert} from "./js/mother_file.js";
    $(() => ShowAlert("Page Loaded"));
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.