microsoft / iot-cardboard-js

iot-cardboard-js is a React library for building experiences for IoT applications
https://main--601c6b2fcd385c002100f14c.chromatic.com/
MIT License
82 stars 27 forks source link

The application cannot be launched after including this library. #976

Open K1vs opened 7 months ago

K1vs commented 7 months ago

The application cannot be launched after including this library. Initially we get a problem of this type.

ERROR in ./node_modules/@microsoft/iot-cardboard-js/index.js 21:0-21
Module not found: Error: Can't resolve 'querystring' in 'C:\Git\AutomotiveDigitalTwinPoC\src\AmDtClient\am-dt-ui\node_modules\@microsoft\iot-cardboard-js'

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: { "querystring": require.resolve("querystring-es3") }'
    - install 'querystring-es3'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "querystring": false }
ERROR in ./node_modules/@microsoft/iot-cardboard-js/index.js 25:0-16
Module not found: Error: Can't resolve 'stream' in 'C:\Git\AutomotiveDigitalTwinPoC\src\AmDtClient\am-dt-ui\node_modules\@microsoft\iot-cardboard-js'

...

Full text of problem: initial_iot_error.txt

This can be fixed by adding a plugin node-polyfill-webpack-plugin to the webpack. (Using Craco, react-app-rewired or react-scripts eject) E.g.

const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");

module.exports = {
  webpack: {
    configure: (config, { env, paths }) => {
      config.plugins = (config.plugins || []).concat([
        new NodePolyfillPlugin(),
      ]);
      return config;
    },
  },
};

But then I get a new problem:

ERROR in ./node_modules/@microsoft/iot-cardboard-js/index.js 29:0-12
Module not found: Error: Can't resolve 'fs' in 'C:\Git\AutomotiveDigitalTwinPoC\src\AmDtClient\am-dt-ui\node_modules\@microsoft\iot-cardboard-js'
ERROR in ./node_modules/@microsoft/iot-cardboard-js/internal/axios-1bc8e67b.js 7:0-28
Module not found: Error: Can't resolve 'fs' in 'C:\Git\AutomotiveDigitalTwinPoC\src\AmDtClient\am-dt-ui\node_modules\@microsoft\iot-cardboard-js\internal'

And fix it using fallback:

    configure: (config, { env, paths }) => {
      const fallback = config.resolve.fallback || {};
      Object.assign(fallback, {
        fs: false,
      });
      config.resolve.fallback = fallback;
      config.plugins = (config.plugins || []).concat([
        new NodePolyfillPlugin(),
      ]);
      return config;
    },

But unfortunately, a new problem arises, and I can no longer solve it.

util__WEBPACK_IMPORTED_MODULE_0__.TextEncoder is not a constructor
TypeError: util__WEBPACK_IMPORTED_MODULE_0__.TextEncoder is not a constructor
    at ./node_modules/@microsoft/iot-cardboard-js/internal/axios-1bc8e67b.js (http://localhost:3000/static/js/bundle.js:666576:21)
    at options.factory (http://localhost:3000/static/js/bundle.js:882495:31)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:881888:33)
    at fn (http://localhost:3000/static/js/bundle.js:882152:21)
    at ./node_modules/@microsoft/iot-cardboard-js/internal/HierarchyNode-05d12d93.js (http://localhost:3000/static/js/bundle.js:63906:76)
    at options.factory (http://localhost:3000/static/js/bundle.js:882495:31)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:881888:33)
    at fn (http://localhost:3000/static/js/bundle.js:882152:21)
    at ./node_modules/@microsoft/iot-cardboard-js/internal/ADT3DSceneAdapter-0423f1cf.js (http://localhost:3000/static/js/bundle.js:16631:84)
    at options.factory (http://localhost:3000/static/js/bundle.js:882495:31)

The last problem also occurs if I use the template https://github.com/Azure-Samples/cra-template-iot-cardboard-js.

At the moment, the use of this library is completely blocked. Which significantly reduces the attractiveness of Azure as a platform for building digital twins.

lorenpelli commented 6 months ago

I downgraded the version with npm install @microsoft/iot-cardboard-js@v1.0.0-beta.235 and it worked

K1vs commented 6 months ago

I was able to solve the problem; to do this I had to add these polyfills. And installed the packages corresponding to these polyfills.

module.exports = {
  webpack: {
    configure: (config, { env, paths }) => {
      const fallback = config.resolve.fallback || {};
      Object.assign(fallback, {
        assert: require.resolve("assert"),
        buffer: require.resolve("buffer"),
        console: require.resolve("console-browserify"),
        constants: require.resolve("constants-browserify"),
        crypto: require.resolve("crypto-browserify"),
        domain: require.resolve("domain-browser"),
        events: require.resolve("events"),
        http: require.resolve("stream-http"),
        https: require.resolve("https-browserify"),
        os: require.resolve("os-browserify/browser"),
        path: require.resolve("path-browserify"),
        punycode: require.resolve("punycode"),
        process: require.resolve("process/browser"),
        querystring: require.resolve("querystring-es3"),
        stream: require.resolve("stream-browserify"),
        string_decoder: require.resolve("string_decoder"),
        sys: require.resolve("util"),
        timers: require.resolve("timers-browserify"),
        tty: require.resolve("tty-browserify"),
        url: require.resolve("url"),
        util: require.resolve("util"),
        vm: require.resolve("vm-browserify"),
        zlib: require.resolve("./szlib"),
        fs: false,
      });

      config.resolve.fallback = fallback;
      config.plugins = (config.plugins || []).concat([]);
      return config;
    },
  },
};

Pay attention to line zlib: require.resolve("./szlib"), Content of file szlib.js

"use strict";

const bzlib = require("browserify-zlib");

const zlib = bzlib;

zlib.constants = zlib; //TODO select only constants

Object.assign(exports, zlib);

without this we will have an error

ERROR
Cannot read properties of undefined (reading 'Z_SYNC_FLUSH')
TypeError: Cannot read properties of undefined (reading 'Z_SYNC_FLUSH')
    at ./node_modules/@microsoft/iot-cardboard-js/internal/axios-1bc8e67b.js (http://localhost:3000/static/js/bundle.js:666676:67)
    at options.factory (http://localhost:3000/static/js/bundle.js:880420:31)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:879813:33)
    at fn (http://localhost:3000/static/js/bundle.js:880077:21)
    at ./node_modules/@microsoft/iot-cardboard-js/internal/HierarchyNode-05d12d93.js (http://localhost:3000/static/js/bundle.js:63903:76)
    at options.factory (http://localhost:3000/static/js/bundle.js:880420:31)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:879813:33)
    at fn (http://localhost:3000/static/js/bundle.js:880077:21)
    at ./node_modules/@microsoft/iot-cardboard-js/internal/ADT3DSceneAdapter-0423f1cf.js (http://localhost:3000/static/js/bundle.js:16630:84)
    at options.factory (http://localhost:3000/static/js/bundle.js:880420:31)
lotus-2601 commented 6 months ago

@K1vs Thanks for recommending the corresponding polyfills. It resolved the dependency errors to a great extent, but I found one more error on my terminal, when using this as a template in my React app. I am using node v16.

Module not found: Error: Can't resolve 'process/browser' in 'C:\Users\0007CI744\Documents\Projects\cardboard-demo\node_modules\@headlessui\react\dist\components\combobox' Did you mean 'browser.js'?

For anyone else facing this, please note I had to make the below additions to my config-overrides.js config.module.rules.unshift({ test: /\.m?js$/, resolve: { fullySpecified: false, // disable the behavior }, }); and to the fallback object, I added "process/browser": require.resolve("process/browser"),

lotus-2601 commented 6 months ago

After successful code compilation, I am getting this error on my browser. Any idea, how to fix this?

Uncaught TypeError: util__WEBPACK_IMPORTED_MODULE_0__.TextEncoder is not a constructor

namratanakhale commented 1 month ago

After successful code compilation, I am also getting error like "_microsoft_iot_cardboard_js__WEBPACK_IMPORTED_MODULE_3__.k is not a constructor" @lotus-2601 do you know how to resolve?