Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript- ReferenceError: require is not defined

I'm trying to bundle and run a TypeScript project using Webpack. One of the TypeScript files requires and external node depenedency. Webpack bundles the file without problem, but when I try to run it on the browser it gives me the following error.

Error message

webpack.config.js

const path = require('path');
const nodeExternals = require('webpack-node-externals');

module.exports = {
  entry: './src/index.ts',
  resolve: {
    extensions: [
      '.js',
      '.jsx',
      '.json',
      '.ts',
      '.tsx'
    ]
  },
   externals: [nodeExternals()],
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
  target: 'web',
  module: {
    loaders: [
      { test: /\.ts(x?)$/, loader: 'ts-loader' },
    ],
  },

};

External node dependency aws-iot-device-sdk

Can someone help me with this?

Update: Found that the following line in the TypeScript file causes the issue. is there a work-around for this?

const client = new IoTClient(true, IoTClientOptions);
like image 938
gayashanbc Avatar asked Sep 01 '26 04:09

gayashanbc


1 Answers

As you are using

require('path');

you have to download RequireJS

Here

RequireJS is a JavaScript file and module loader.

or add Add http://requirejs.org/docs/release/2.2.0/minified/require.js to your project

like image 137
useless'MJ Avatar answered Sep 02 '26 18:09

useless'MJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!