Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack : Uncaught ReferenceError: require is not defined

This error comes when in webpack target = node but i have done target=web(default)

also i am not loading reactjs externally

this error comes on loading app in browser

what i am doing wrong ?

In Console

enter image description here

File

enter image description here enter image description here

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');
const nodeExternals = require('webpack-node-externals');

const config = {
    target: 'web',
    externals: [nodeExternals()],
    entry: './src/index.js',
    output: {
        filename: '[name].bundle.js',
        path: __dirname + '/build'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            {
                test: /\.(png|svg|jpe?g|gif)$/,
                use: [{
                        loader: 'file-loader',
                        options: {
                            name: '[path][name].[ext]'
                        }
                    }
                ]
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                use: ['file-loader']
            }
        ]
    },
    devtool: 'inline-source-map',
    plugins: [
        new HtmlWebpackPlugin({
            title: 'Instarem.com'
        })
    ]
};

module.exports = config;

.babelrc using

babel-preset-env

{
    "presets": [
        "react",
        [
            "env",
            {
                "targets": {
                    "browsers": ["last 2 versions"]
                },
                "debug": true,
                "modules": "commonjs"
            }
        ]
    ],
    "plugins": [
        "transform-object-rest-spread",
        "transform-class-properties"
    ]
}

thanks :)

I found Clue


In facebook's create react app generator bundle it shows

module.exports = __webpack_require__(/*! ./lib/React */ "./node_modules/react/lib/React.js");

but in my case it shows only

module.exports = require("react");
like image 363
vijay Avatar asked Aug 22 '17 13:08

vijay


People also ask

How do you fix uncaught ReferenceError require is not defined?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.

Why require is not working in JavaScript?

This usually happens because your JavaScript environment doesn't understand how to handle the call to require() function you defined in your code. Here are some known causes for this error: Using require() in a browser without RequireJS.

What is require () in JavaScript?

1) require() require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object. require() statement not only allows to add built-in core NodeJS modules but also community-based and local modules.

How do you use Web packs?

As you can see, to activate a webpack plugin, we need to include it in the file and then add it to the plugins array. If needed, we also pass options to the plugin. See the html-webpack-plugin repo for all available options and the ability to write and use your own templates.

How do I fix the ReferenceError require is not defined error?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package.json file and rename any files that have a .mjs extension to have a .js extension. Alternatively, you can use the ES6 module syntax with the import and export keywords.

What does require is not defined mean in JavaScript?

Sometimes, JavaScript may suddenly give you a require is not defined error like this: Uncaught ReferenceError: require is not defined. This usually happens because your JavaScript environment doesn’t understand how to handle the require () function reference.

Why is require() not working on my website?

If the require () function is not defined on the server, you have set the type property to module in your package.json file, or your files have an extension of .mjs instead of .js.

Why can't I see my source map in Webpack?

Could occur because of mode: "development" in webpack.config.js. A possible solution is to also add devtool: "inline-source-map", else, set mode: "production" It could be because of a number of reasons. If you don't mind sharing your code (through GitHub or similar).


4 Answers

You should not use

externals: [nodeExternals()], 

in web app. According to https://github.com/liady/webpack-node-externals it is only for backend. Since you use nodeExternals in web app you get CommonJS modules, that expects built in node require function. So just remove it to fix error.

like image 88
Maxim Kuzmin Avatar answered Sep 28 '22 06:09

Maxim Kuzmin


Maybe it will be helpful for someone. Add into call nodeExternals option importType with value 'umd'.

nodeExternals({    importType: 'umd' }) 
like image 34
Hovorun Art Avatar answered Sep 28 '22 06:09

Hovorun Art


My issue was in my package.json, I had to remove

"type":"module"

It was trying to load the file as an es6 module

https://nodejs.org/api/packages.html#packages_type

like image 24
shoop Avatar answered Sep 28 '22 06:09

shoop


I encountered this problem a few minutes ago. This usually happens when you mix up the target property for the bundle in webpack.config file. E.g. if you are creating a bundle for React the target should be target: 'web' and should not include anything relating to node like 'async-node' or 'node'.

const reactConfig = {
   name: 'React Bundle',
   target: 'web',
   ...
}
like image 43
Khalil Avatar answered Sep 28 '22 08:09

Khalil