1

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?

Mark_
  • 113
  • 1
  • 12

1 Answers1

1

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
Basharmal
  • 1,313
  • 10
  • 30
  • Wow that works actually, but isn't there an other way? Because now I have to import my functions everytime I make a new one. – Mark_ Mar 27 '21 at 17:02