3

I've got an error('ReferenceError: require is not defined')
in the following syntax, and I using '@babel/preset-env' now.
How do I solve this problem?

const _ = require("lodash.template");

(I am trying to use the lodash library in the es6 development environment.)


package.json

"@babel/preset-env": "^7.0.0"

webpack.config.js

presets: ['@babel/preset-env']

The entire Webpack config looks like this:

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

module.exports = {
    entry: './src/js/App.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/dist/',
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                include: path.join(__dirname),
                exclude: /(node_modules)|(dist)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }
            }
        ]
    }
};

some context of of the usage:

let litemplate  =`<li class="id">[<%=data[i].id%>]</li>`;
let listemplate = _.template(litemplate);
jini
  • 373
  • 1
  • 5
  • 15

1 Answers1

0

You probably need to install CommonJS, RequireJS, or some other manager, see Client on node: Uncaught ReferenceError: require is not defined

Stefano Gogioso
  • 236
  • 2
  • 6
  • What do I do if I'm not using node, but just run the javascript modules in the browser? – Dimage Mar 11 '21 at 12:24
  • browsers don't understand "require", however modern browsers do support modules: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules – Mali Naeemi Dec 28 '21 at 22:58