BenyaminRmb
BenyaminRmb

Reputation: 163

Function is not defined while exist

I'm using a function (ShowPopUpProject) in home.html that defined in javascript.js.

its never mind what my function is . I just add it in my webpack.config.js :

var path = require('path');
var webpack = require('webpack');

module.exports =
    {
        mode: 'production',
        entry: [

            './js/javascript.js',
            './js/javascript2.js',

        ],
        output: {
            path: path.resolve(__dirname, 'dist'),
            filename: "bundle.js"
        }

    };

I used ShowPopUpProject in other functions at javascript2.js , when i run run npm build or webpack i got error in my bundle.js file :

ReferenceError: ShowPopUpProject is not defined

As you see i defined the function in javascript.js and when use in javascript2.js and build the webpack file receive this error. how can i fix this ?

Upvotes: 0

Views: 764

Answers (1)

laenkeio
laenkeio

Reputation: 512

Webpack entries are the root of your dependency tree, so what you are telling Webpack is:

  • Create a file called bundle.js with ./js/javascript.js and all its dependencies (requires/imports)
  • Create a file called bundle.js with ./js/javascript2.js and all its dependencis (requires/imports)

If javascript2.js never imports javascript.js then it will not know about the function in javascript.js because it is a separate dependency tree.

If you're really are trying to make two bundles, then use something like [name].js in output, and use import/require in the files to make sure the functions are defined where they are needed.

Upvotes: 1

Related Questions