Skip to content

Instantly share code, notes, and snippets.

@mendes5
Created February 11, 2022 16:54
Show Gist options
  • Select an option

  • Save mendes5/301497a56fc3e77654bd455b62edd8e0 to your computer and use it in GitHub Desktop.

Select an option

Save mendes5/301497a56fc3e77654bd455b62edd8e0 to your computer and use it in GitHub Desktop.
aws-iot-device-sdk + Nx Webpack config

Packages Needed:

  • aws-iot-device-sdk
  • buffer
  • process
  • util
  • @types/aws-iot-device-sdk(optional)

Override the default webpack config in project.json

While using @nrwl/web:build as the executor for the build target, add a custom webpack config file to your build configurations:

// apps/<your-app-name>/project.json
{
  "targets": {
    "build": {
      "configurations": {
        "local": {
          "webpackConfig": "apps/<your-app-name>/webpack.config.js"
        }
        // Same for "sandbox", "production" and any other configurations
      }
    },
    "serve": {
      "configurations": {
        "local": {
          "buildTarget": "<your-app-name>:build:local"
        },
        // Same for "sandbox", "production" and any other configurations
      }
    }
  }
}

Add the following to your webpack config:

// apps/<your-app-name>/webpack.config.js
const generateConfig = require('@nrwl/react/plugins/webpack');
const webpack = require('webpack');

module.exports = (config) => {
  const newConfig = generateConfig(config);

  newConfig.node = {
    ...newConfig.node,
    global: true,
  };

  newConfig.plugins = [
    new webpack.ProvidePlugin({
      Buffer: ['buffer', 'Buffer'],
    }),
    new webpack.ProvidePlugin({
      process: 'process/browser',
    }),

    ...newConfig.plugins,
  ];

  newConfig.resolve = {
    ...newConfig.resolve,
    fallback: {
      ...newConfig.resolve.fallback,
      fs: false,
      path: false,
      tls: false,
      crypto: false,
      buffer: require.resolve('buffer/'),
    },
  };

  return newConfig;
};

Now you should be able to just import iot from 'aws-iot-device-sdk'; inside your application and any libs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment