Javascript 如何将aws iot设备sdk NPM模块与Angular 11一起使用?(它在生成时抛出fs、path和tls错误)

Javascript 如何将aws iot设备sdk NPM模块与Angular 11一起使用?(它在生成时抛出fs、path和tls错误),javascript,angular,amazon-web-services,iot,Javascript,Angular,Amazon Web Services,Iot,我维护了一个5.6版本的基于浏览器的Angular应用程序。该应用程序有两个组件,它们订阅物联网主题并侦听更新(没有发布,只是订阅) 我们需要升级到Agnular 11.0.3。我已经完成了那项工作。但现在,当我尝试运行本地web服务器或运行构建时,aws iot device sdk NPM模块会将这些错误汇总为: Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/common/lib' Error:

我维护了一个5.6版本的基于浏览器的Angular应用程序。该应用程序有两个组件,它们订阅物联网主题并侦听更新(没有发布,只是订阅)

我们需要升级到Agnular 11.0.3。我已经完成了那项工作。但现在,当我尝试运行本地web服务器或运行构建时,aws iot device sdk NPM模块会将这些错误汇总为:

Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/common/lib'
Error: Can't resolve 'path' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device'
Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device'
Error: Can't resolve 'tls' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device/lib'

import { AwsIotService } from '../../shared/awsIot/awsIot.service';

this.awsIotService.getNewIotConnection(this.iotTopic, this.iotUrl, (iotClient) => {
    //cache the iotClient
    this.iotClient = iotClient;

    //handle the iotClient's message event
    iotClient.on('message', (topic, message) => {
        // process the message here:
        console.log('IOT Message received:');
        console.dir(message);
    });
});
我在这个问题上花了很多时间。我们的前端(浏览器)物联网代码在过去几年中一直运行良好。但是必须升级到Angular 11.0.3,这个问题是一个大障碍。


来自我们应用程序的示例代码:

Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/common/lib'
Error: Can't resolve 'path' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device'
Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device'
Error: Can't resolve 'tls' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device/lib'

import { AwsIotService } from '../../shared/awsIot/awsIot.service';

this.awsIotService.getNewIotConnection(this.iotTopic, this.iotUrl, (iotClient) => {
    //cache the iotClient
    this.iotClient = iotClient;

    //handle the iotClient's message event
    iotClient.on('message', (topic, message) => {
        // process the message here:
        console.log('IOT Message received:');
        console.dir(message);
    });
});
(我们的package.json位于末尾)

xxxx-app.component.ts文件:

Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/common/lib'
Error: Can't resolve 'path' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device'
Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device'
Error: Can't resolve 'tls' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device/lib'

import { AwsIotService } from '../../shared/awsIot/awsIot.service';

this.awsIotService.getNewIotConnection(this.iotTopic, this.iotUrl, (iotClient) => {
    //cache the iotClient
    this.iotClient = iotClient;

    //handle the iotClient's message event
    iotClient.on('message', (topic, message) => {
        // process the message here:
        console.log('IOT Message received:');
        console.dir(message);
    });
});
awsIotService.ts:

Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/common/lib'
Error: Can't resolve 'path' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device'
Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device'
Error: Can't resolve 'tls' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device/lib'

import { AwsIotService } from '../../shared/awsIot/awsIot.service';

this.awsIotService.getNewIotConnection(this.iotTopic, this.iotUrl, (iotClient) => {
    //cache the iotClient
    this.iotClient = iotClient;

    //handle the iotClient's message event
    iotClient.on('message', (topic, message) => {
        // process the message here:
        console.log('IOT Message received:');
        console.dir(message);
    });
});
在下面的代码中,“iotUrl”指向我们编写的自定义服务,该服务提供基于浏览器的Angular应用程序连接和订阅所需的详细信息:

awsIot = require('aws-iot-device-sdk'); //simply requiring that NPM module causes the errors stated above

getNewIotConnection (topic, iotUrl, callback) {
    this.httpClient.get(iotUrl).subscribe(responseData => {

         const client = this.awsIot.device({
                port: 443,
                protocol: 'wss',
                region: responseData.region,
                host: responseData.iotEndpoint,
                secretKey: responseData.secretKey,
                accessKeyId: responseData.accessKey,
                sessionToken: responseData.sessionToken,
            });

            // subscribe to the topic
            client.on('connect', () => client.subscribe(topic));

            // call the callback
            callback(client);
     });
}

我尝试过的最新建议:

Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/common/lib'
Error: Can't resolve 'path' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device'
Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device'
Error: Can't resolve 'tls' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device/lib'

import { AwsIotService } from '../../shared/awsIot/awsIot.service';

this.awsIotService.getNewIotConnection(this.iotTopic, this.iotUrl, (iotClient) => {
    //cache the iotClient
    this.iotClient = iotClient;

    //handle the iotClient's message event
    iotClient.on('message', (topic, message) => {
        // process the message here:
        console.log('IOT Message received:');
        console.dir(message);
    });
});
1-我已尝试使用

..但我认为文档没有帮助。。。我希望我错过了一些东西

2我尝试了AWS amplify并遵循了本教程:

…但使用AWS amplify解决此问题需要对我们构建的后端进行重新工作,并且似乎需要为爆胎获得新的传输(即,只需要订阅物联网主题)


我尝试过的较老的解决方法:

Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/common/lib'
Error: Can't resolve 'path' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device'
Error: Can't resolve 'fs' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device'
Error: Can't resolve 'tls' in '[PATH_TO]/node_modules/aws-iot-device-sdk/device/lib'

import { AwsIotService } from '../../shared/awsIot/awsIot.service';

this.awsIotService.getNewIotConnection(this.iotTopic, this.iotUrl, (iotClient) => {
    //cache the iotClient
    this.iotClient = iotClient;

    //handle the iotClient's message event
    iotClient.on('message', (topic, message) => {
        // process the message here:
        console.log('IOT Message received:');
        console.dir(message);
    });
});
3我尝试将此添加到package.json:

"browser": {
  "fs": false,
  "path": false,
  "os": false
}
"dependencies": {
  ...other packages
  "mqtt": "2.15.1",
  "minimist": "1.2.0",
  "websocket-stream": "^5.0.1",
  "crypto-js": "3.1.6"
}
…那没用

4我尝试将此添加到package.json:

"browser": {
  "fs": false,
  "path": false,
  "os": false
}
"dependencies": {
  ...other packages
  "mqtt": "2.15.1",
  "minimist": "1.2.0",
  "websocket-stream": "^5.0.1",
  "crypto-js": "3.1.6"
}
…那没用

5我尝试了这里详述的patch.js方法:

…那没用

6我尝试将此添加到webpack.config.js:

resolve: {
  fallback: {
    fs: false
  }
}
node: {
  fs: 'empty'
}
…那没用

7我尝试将此添加到webpack.config.js:

resolve: {
  fallback: {
    fs: false
  }
}
node: {
  fs: 'empty'
}
…不起作用(仅在旧的网页中起作用。)


我们的package.json(仅依赖项和依赖项):

有帮助吗?作者似乎和你有/避免了同样的麻烦。他提到:

   "browser": {
       "fs": false,
       "tls": false,
       "path": false
   },
有帮助吗?作者似乎和你有/避免了同样的麻烦。他提到:

   "browser": {
       "fs": false,
       "tls": false,
       "path": false
   },

听起来你好像想要一个NodeJS包。。?这在浏览器中是行不通的。@MikeOne-问题中的代码已经完美地工作了好几年(字面意思)。aws iot设备sdk模块的一个依赖项似乎依赖于“fs”模块,Angular不再允许该模块。我不明白的是,如何解决这个问题,没有一条捷径。使用AWS Amplify看起来真的有点过头了。Fs是文件系统。仅在节点中可用,在浏览器中不可用。你可能弄错了package@MikeOne-不确定您是否理解我的观点:是的,我理解fs模块是什么。我想说的是,实现aws iot设备sdk模块运行良好,并且已经运行了多年。Angular的最新版本不允许这种依赖关系。有很多解决办法(在我的问题中有详细说明),但它们现在都过时了。我希望能找到一个有效的。听起来你好像想要一个NodeJS包。。?这在浏览器中是行不通的。@MikeOne-问题中的代码已经完美地工作了好几年(字面意思)。aws iot设备sdk模块的一个依赖项似乎依赖于“fs”模块,Angular不再允许该模块。我不明白的是,如何解决这个问题,没有一条捷径。使用AWS Amplify看起来真的有点过头了。Fs是文件系统。仅在节点中可用,在浏览器中不可用。你可能弄错了package@MikeOne-不确定您是否理解我的观点:是的,我理解fs模块是什么。我想说的是,实现aws iot设备sdk模块运行良好,并且已经运行了多年。Angular的最新版本不允许这种依赖关系。有很多解决办法(在我的问题中有详细说明),但它们现在都过时了。我希望能找到一个有效的。