How can I create node_module in laravel?

Viewed 78

I create a node module file in my project

ressources/js/fc.js

function test(nbp, varElt){
    if(nbp > 1)
        varElt.innerText = nbp.toString();
    else
        varElt.innerText = nbp.toString();
}
module.exports = { test }

In my ressources/layouts/app.blade.php I add my module like that :

<script src="{{ asset('js/fc.js') }}" type="module"></script>

but when in a page section view I want to import my module i have an error

ressources/views/tests/test.blade.php

@extends('layouts.app')
@section('content')

    <div class="container">
        <div class="row justify-content-center">
            <div class="card">
                <div class="card-body" style="min-width: 700px;text-align: center">
                    <h2 id="test1"></h2><br>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('script')
    <script>
        import { test } from "../../../public/js/impFc";

        $(function(){   
            const title = document.getElementById("test1") ;
            test.test(1,title)
        })

    </script>
@endsection

My error :

Uncaught SyntaxError: Cannot use import statement outside a module

SOmeone have any idea to resolve this ?

1 Answers

In your <script> tag you need to add type="module". So it would look something like this :

@section('script')
    <script type="module" >
        import { test } from "../../../public/js/impFc";

        $(function(){   
            const title = document.getElementById("test1") ;
            test.test(1,title)
        })

    </script>
@endsection

Related