Skip to content
This repository has been archived by the owner on Oct 14, 2022. It is now read-only.

"export 'default' (imported as 'AgoraRTC') was not found in 'agora-rtc-sdk-ng' #143

Open
miguelperez opened this issue Apr 14, 2021 · 11 comments

Comments

@miguelperez
Copy link

Describe the bug

When using webpack one can not access the exported AgoraRTC

SDK Version: latest

To Reproduce

Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Additional context

I have not been able to run the examples using NPM because of the issue.

@plutoless
Copy link
Contributor

how do you see the error? could you pls briefly describe how to reproduce this.

@miguelperez
Copy link
Author

miguelperez commented Apr 17, 2021

Hi sure,

I was following the documentation here: https://docs.agora.io/en/live-streaming/start_live_web_ng?platform=Web

It mentioned the following:

Method 1: Through npm

This method requires npm, see Install npm for details.

Run the following command to install the SDK:

npm install agora-rtc-sdk-ng --save

Add the following code in the JavaScript code in your project to import the module:

import AgoraRTC from "agora-rtc-sdk-ng"
const client = AgoraRTC.createClient({ mode: "live", codec: "vp8" });

But that did not work ☝️ I got this error:

ERROR in ./app/javascript/controllers/video_controller.js 97:25-33
"export 'AgoraRTC' was not found in 'agora-rtc-sdk-ng'
    at HarmonyImportSpecifierDependency._getErrors (/Users/miguelperez/projects/shopify-now/node_modules/webpack/lib/dependencies/HarmonyImportSpecifierDependency.js:109:11)
    at HarmonyImportSpecifierDependency.getErrors (/Users/miguelperez/projects/shopify-now/node_modules/webpack/lib/dependencies/HarmonyImportSpecifierDependency.js:68:16)
    at Compilation.reportDependencyErrorsAndWarnings (/Users/miguelperez/projects/shopify-now/node_modules/webpack/lib/Compilation.js:1463:22)
    at /Users/miguelperez/projects/shopify-now/node_modules/webpack/lib/Compilation.js:1258:10
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/miguelperez/projects/shopify-now/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:13:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/miguelperez/projects/shopify-now/node_modules/tapable/lib/Hook.js:154:20)
    at Compilation.finish (/Users/miguelperez/projects/shopify-now/node_modules/webpack/lib/Compilation.js:1253:28)
    at /Users/miguelperez/projects/shopify-now/node_modules/webpack/lib/Compiler.js:672:17
    at _done (eval at create (/Users/miguelperez/projects/shopify-now/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:7:1)
    at eval (eval at create (/Users/miguelperez/projects/shopify-now/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:30:22)

I had to do the following:

import "agora-rtc-sdk-ng"

and the access it like:

rtc.client = global.AgoraRTC.createClient({ mode: "live", codec: "vp8" });

following the advice on slack from Gustavo Leon. https://agoraiodev.slack.com/archives/CPQP4GMJ9/p1618411117336400 and that did work

@plutoless
Copy link
Contributor

try this.
import { IAgoraRTC } from 'agora-rtc-sdk-ng';

@miguelperez
Copy link
Author

That did not work either.

Failed to compile.

./app/javascript/controllers/receive_video_controller.js 82:31-40
"export 'IAgoraRTC' was not found in 'agora-rtc-sdk-ng'

@plutoless
Copy link
Contributor

could you pls try the demo here
https://github.com/AgoraIO/API-Examples-Web/tree/main/ReactDemo

@miguelperez
Copy link
Author

The app works. Pasting the token app from my account shows a local track with my camera video.

I did the following to run the app.

npm install
npm start

Could it be that the issue is with the JS version and not with the TypeScript version?

@plutoless
Copy link
Contributor

could you pls share your project?

@miguelperez
Copy link
Author

miguelperez commented Apr 21, 2021

It is a brand new rails application with stimulus js that I created to start testing the service.

The packaje.json is the following:

{
  "name": "shopify-now",
  "private": true,
  "dependencies": {
    "@hotwired/turbo-rails": "^7.0.0-beta.5",
    "@rails/actioncable": "^6.0.0",
    "@rails/activestorage": "^6.0.0",
    "@rails/ujs": "^6.0.0",
    "@rails/webpacker": "5.2.1",
    "agora-rtc-sdk-ng": "^4.4.0",
    "stimulus": "^2.0.0"
  },
  "version": "0.1.0",
  "devDependencies": {
    "webpack-dev-server": "^3.11.2"
  }
}

and a sample stimulus js controller for the host:

import { Controller } from "stimulus"
import "agora-rtc-sdk-ng"


export default class extends Controller {
  static values = { token: String, userId: String  }

  connect() {
    this.element.textContent = "Hello World!"
  }

  initialize(){
    var rtc = {
      // For the local client.
      client: null,
      // For the local audio and video tracks.
      localAudioTrack: null,
      localVideoTrack: null,
    };

    var options = {
      // Pass your app ID here.
      appId: "XXXXXXXXXXXXx",
      // Set the channel name.
      channel: "XXXXXXXX",
      // Pass a token if your project enables the App Certificate.
      token: this.tokenValue,
        // Set the user role in the channel.
      role: "host"
    };

    async function startBasicCall(userId) {
      /**
       * Put the following code snippets here.
       */
       rtc.client = global.AgoraRTC.createClient({ mode: "live", codec: "vp8" });
       rtc.client.setClientRole(options.role);

       const uid = await rtc.client.join(options.appId, options.channel, options.token, userId);

       // Create an audio track from the audio sampled by a microphone.
       rtc.localAudioTrack = await global.AgoraRTC.createMicrophoneAudioTrack();
       // Create a video track from the video captured by a camera.
       rtc.localVideoTrack = await global.AgoraRTC.createCameraVideoTrack();
       // Publish the local audio and video tracks to the channel.
       await rtc.client.publish([rtc.localAudioTrack, rtc.localVideoTrack]);

       console.log("publish success!");
    }

    startBasicCall(this.userIdValue);

    rtc.client.on("user-published", async (user, mediaType) => {
      // Subscribe to a remote user.
      await rtc.client.subscribe(user, mediaType);
      console.log("subscribe success");

      // If the subscribed track is video.
      if (mediaType === "video") {
        // Get `RemoteVideoTrack` in the `user` object.
        const remoteVideoTrack = user.videoTrack;
        // Dynamically create a container in the form of a DIV element for playing the remote video track.
        const playerContainer = document.createElement("div");
        // Specify the ID of the DIV container. You can use the `uid` of the remote user.
        playerContainer.id = user.uid.toString();
        playerContainer.style.width = "640px";
        playerContainer.style.height = "480px";
        document.body.append(playerContainer);

        // Play the remote video track.
        // Pass the DIV container and the SDK dynamically creates a player in the container for playing the remote video track.
        remoteVideoTrack.play(playerContainer);

        // Or just pass the ID of the DIV container.
        // remoteVideoTrack.play(playerContainer.id);
      }

      // If the subscribed track is audio.
      if (mediaType === "audio") {
        // Get `RemoteAudioTrack` in the `user` object.
        const remoteAudioTrack = user.audioTrack;
        // Play the audio track. No need to pass any DOM element.
        remoteAudioTrack.play();
      }
    });

    rtc.client.on("user-unpublished", user => {
      // Get the dynamically created DIV container.
      const playerContainer = document.getElementById(user.uid);
      // Destroy the container.
      playerContainer.remove();
    });

    async function leaveCall() {
      // Destroy the local audio and video tracks.
      rtc.localAudioTrack.close();
      rtc.localVideoTrack.close();

      // Traverse all remote users.
      rtc.client.remoteUsers.forEach(user => {
        // Destroy the dynamically created DIV container.
        const playerContainer = document.getElementById(user.uid);
        playerContainer && playerContainer.remove();
      });

      // Leave the channel.
      await rtc.client.leave();
    }

  }
}

this is a sample controller for the atendee:

import { Controller } from "stimulus"
import "agora-rtc-sdk-ng"


export default class extends Controller {
  static values = { token: String, userId: String }

  connect() {
    this.element.textContent = "Hello World!"
  }

  initialize(){
    var rtc = {
      // For the local client.
      client: null,
      // For the local audio and video tracks.
      localAudioTrack: null,
      localVideoTrack: null,
    };

    var options = {
      // Pass your app ID here.
      appId: "xxxxx",
      // Set the channel name.
      channel: "xxxxxx",
      // Pass a token if your project enables the App Certificate.
      token: this.tokenValue,
        // Set the user role in the channel.
      role: "audience"
    };

    async function startBasicCall(userId) {
      /**
       * Put the following code snippets here.
       */
       rtc.client = global.AgoraRTC.createClient({ mode: "live", codec: "vp8" });
       rtc.client.setClientRole(options.role);

       const uid = await rtc.client.join(options.appId, options.channel, options.token, userId);
    }

    startBasicCall(this.userIdValue);

    rtc.client.on("user-published", async (user, mediaType) => {
      // Subscribe to a remote user.
      await rtc.client.subscribe(user, mediaType);
      console.log("subscribe success");

      // If the subscribed track is video.
      if (mediaType === "video") {
        // Get `RemoteVideoTrack` in the `user` object.
        const remoteVideoTrack = user.videoTrack;
        // Dynamically create a container in the form of a DIV element for playing the remote video track.
        const playerContainer = document.createElement("div");
        // Specify the ID of the DIV container. You can use the `uid` of the remote user.
        playerContainer.id = user.uid.toString();
        playerContainer.style.width = "640px";
        playerContainer.style.height = "480px";
        document.body.append(playerContainer);

        // Play the remote video track.
        // Pass the DIV container and the SDK dynamically creates a player in the container for playing the remote video track.
        remoteVideoTrack.play(playerContainer);

        // Or just pass the ID of the DIV container.
        // remoteVideoTrack.play(playerContainer.id);
      }

      // If the subscribed track is audio.
      if (mediaType === "audio") {
        // Get `RemoteAudioTrack` in the `user` object.
        const remoteAudioTrack = user.audioTrack;
        // Play the audio track. No need to pass any DOM element.
        remoteAudioTrack.play();
      }
    });

    rtc.client.on("user-unpublished", user => {
      // Get the dynamically created DIV container.
      const playerContainer = document.getElementById(user.uid);
      // Destroy the container.
      playerContainer.remove();
    });

    async function leaveCall() {
      // Destroy the local audio and video tracks.
      rtc.localAudioTrack.close();
      rtc.localVideoTrack.close();

      // Traverse all remote users.
      rtc.client.remoteUsers.forEach(user => {
        // Destroy the dynamically created DIV container.
        const playerContainer = document.getElementById(user.uid);
        playerContainer && playerContainer.remove();
      });

      // Leave the channel.
      await rtc.client.leave();
    }

  }
}

The code is copied and pasted from the example documentation.

That way the library is imported and I am able to see video and so on.

@plutoless
Copy link
Contributor

plutoless commented Apr 21, 2021

umm i have no experience using your framework. but my guess is the way how it converts import statement somehow works differently comparing to webpack.

@miguelperez
Copy link
Author

it is using webpack.

@jessepinho
Copy link

jessepinho commented Jun 28, 2021

I'm having the exact same issue. When I have this code at the top of one of my JS files:

import AgoraRTC from "agora-rtc-sdk-ng";

...I get the same error as the original poster (using agora-rtc-sdk-ng 4.5.0). Any ideas?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants