'Using requirejs instance outside of definition?

I am using requirejs to determine dependencies. I have some code that determines the use of the Video.js:

require.config({
  'paths': {
    'bc': 'https://players.brightcove.net/1507807800001/default_default/index.min'
  },
  waitSeconds: 30
});
require(['bc'], function() {
  var myPlayer = videojs.getPlayers().myPlayerID;
  myPlayer.on('loadstart', function(){
    myPlayer.play();
  })
});

Also I have some code that wants to get an instance and use that Video.js:

if (typeof myPlayer != "undefined") {
  console.log("myPlayer existence true");
  myPlayer.stop();
} else {
  console.log("myPlayer existence false");
}

Is there a way to access outside of the Video.js's definition? This is a working example https://playcode.io/868784/



Solution 1:[1]

You can define your module and later use it:

require.config({
  'paths': {
    'bc': 'https://players.brightcove.net/1507807800001/default_default/index.min'
  },
  waitSeconds: 30
});
define('myPlayer', ['bc'], function() {
  var myPlayer = videojs.getPlayers().myPlayerID;
  myPlayer.on('loadstart', function(){
    myPlayer.play();
  })

  return myPlayer;
});

Video.js:

require(['myPlayer'], function (myPlayer) {
  myPlayer.stop();
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1