3

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);
6
  • Why are you setting output.libraryTarget to 'commonjs', have you tried to set it to libraryTarget: "umd"? Commented Mar 23, 2018 at 7:20
  • I have no clear idea about the libraryTarget option and I tried with the option you mentioned, but the error remains the same. Commented Mar 23, 2018 at 7:25
  • What's inside ./src/index.ts? Commented Mar 23, 2018 at 7:32
  • The following line: const client = new IoTClient(true, IoTClientOptions); Commented Mar 23, 2018 at 8:40
  • Can you provide more content in index.ts, I have no idea what IoTClient and IoTClientOptions are. Commented Mar 23, 2018 at 9:08

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

The following Webpack configuration solved my issue.

const path = require('path');

module.exports = {
  entry: {
    chatUI: './src/chat-ui.ts',
    chatLogic: './src/chat-logic.ts',
  },
  resolve: {
    extensions: [
      '.js',
      '.jsx',
      '.json',
      '.ts',
      '.tsx'
    ]
  },
  output: {
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
  node: {
    fs: 'empty',
    tls: 'empty'
  },
  target: 'web',
  module: {
    loaders: [
      {
        test: /\.tsx?$/,
        loader: 'awesome-typescript-loader'
      }
    ],
  },

};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.