laravel 8 include certain javascrip file into other js file

886 Views Asked by At

I have two files, one is my main js file called app.js and I have a file where I store all my js functions, called functions.js. As you can see on the image below.

enter image description here

But I want to include the functions.js file into the app.js file. So I googled on how to do it and this is what people said:

enter image description here

But my npm run dev says the file doesn't exist. But the path is correct. What am I doing wrong here, is there a other way to do it?

1

There are 1 best solutions below

1
On

You can simply just create the file wherever you want to create it, and then export some properties or methods, and then import them in your app.js file, or in whatever file you need. Something like this :

//new_file.js

export const jokes = function() {

        return ['funny', 'not really funny', 'boring']
}

export const  heading = 'some global heading to be reused.'

And in your app.js file :

import { jokes, heading } from 'new_file.js'//specify actual path here .

console.log(jokes) // ['funny', 'not really funny', 'boring']

console.log(heading)//some global heading to be reused.

This tutorial might be helpful too .

http://www.2ality.com/2014/09/es6-modules-final.html