aws / aws-iot-device-sdk-js

SDK for connecting to AWS IoT from a device using JavaScript/Node.js
Apache License 2.0
964 stars 384 forks source link

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default #418

Closed ymwjbxxq closed 1 year ago

ymwjbxxq commented 1 year ago

Describe the bug

I just created a simple hello world react page because I want to subscribe to a topic but react cannot even load because the

import DeviceSdk from 'aws-iot-device-sdk';

I also added config-overrides.js without success

const webpack = require('webpack');
module.exports = function override(config) {
  const fallback = config.resolve.fallback || {};
  Object.assign(fallback, {
    "crypto": require.resolve("crypto-browserify"),
    "stream": require.resolve("stream-browserify"),
    "assert": require.resolve("assert"),
    "http": require.resolve("stream-http"),
    "https": require.resolve("https-browserify"),
    "os": require.resolve("os-browserify"),
    "url": require.resolve("url")
    "path-browserify": require.resolve("path-browserify")
  })
  config.resolve.fallback = fallback;
  config.plugins = (config.plugins || []).concat([
    new webpack.ProvidePlugin({
      process: 'process/browser',
      Buffer: ['buffer', 'Buffer']
    })
  ])
  return config;
}

Expected Behavior

not crashing

Current Behavior

Compiled with problems:X

ERROR in ./node_modules/aws-iot-device-sdk/common/lib/tls-reader.js 17:14-27

Module not found: Error: Can't resolve 'fs' in '/test/iot-ui/hello/node_modules/aws-iot-device-sdk/common/lib'

ERROR in ./node_modules/aws-iot-device-sdk/device/index.js 29:11-26

Module not found: Error: Can't resolve 'path' in '/test/iot-ui/hello/node_modules/aws-iot-device-sdk/device'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
    - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "path": false }

ERROR in ./node_modules/aws-iot-device-sdk/device/index.js 30:9-22

Module not found: Error: Can't resolve 'fs' in '/test/iot-ui/hello/node_modules/aws-iot-device-sdk/device'

ERROR in ./node_modules/aws-iot-device-sdk/device/lib/tls.js 17:10-24

Module not found: Error: Can't resolve 'tls' in '/test/iot-ui/hello/node_modules/aws-iot-device-sdk/device/lib'
Did you mean './tls'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules, /test/iot-ui/hello/node_modules).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.

Reproduction Steps

  1. npx create-react-app hello
  2. add import DeviceSdk from 'aws-iot-device-sdk'; inside App.js

Possible Solution

No response

Additional Information/Context

node --version v18.13.0
npm --version 9.4.1

package.json

{
  "name": "hello",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "web-vitals": "^2.1.4",
    "aws-iot-device-sdk": "^2.2.12"
  },
  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "assert": "^2.0.0",
    "buffer": "^6.0.3",
    "crypto-browserify": "^3.12.0",
    "https-browserify": "^1.0.0",
    "os-browserify": "^0.3.0",
    "path-browserify": "^1.0.1",
    "process": "^0.11.10",
    "react-app-rewired": "^2.2.1",
    "stream-browserify": "^3.0.0",
    "stream-http": "^3.2.0",
    "url": "^0.11.0"
  }
}

SDK version used

aws --version aws-cli/2.8.8 Python/3.9.11 Darwin/22.3.0 exe/x86_64 prompt/off

Environment details (OS name and version, etc.)

Mac

jmklix commented 1 year ago

https://github.com/aws/aws-iot-device-sdk-js-v2/issues/331

jmklix commented 1 year ago

We are not planning on adding react support to this v1 sdk. Please check out the feature request for adding support to react to aws-iot-device-sdk-js-v2 above.

github-actions[bot] commented 1 year ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.