Error Blockly is not defined

497 views
Skip to first unread message

Rupak Choudhury

unread,
Jun 8, 2022, 11:10:51 AM6/8/22
to blo...@googlegroups.com
Hi All,

I am updating Blockly version from 2.x to 8.0.2 and i am using blockly_compressed.js.

In new compressed file the below line is written

;(function(root, factory) {
  if (typeof define === 'function' && define.amd) { // AMD
    define([], factory);
  } else if (typeof exports === 'object') { // Node.js
    module.exports = factory();
  } else { // Browser
    var factoryExports = factory();
    root.Blockly = factoryExports;
  }
}(this, function()

In my application i am getting typeof define value = function. So it's never going to else part (browser) and throwing an error Blockly is not defined while loading the page from browser.

But if I comment the if and if else part in blockly_compressed.js than it always going to else part (browser)  and web page , blocks are loading perfectly.

I have tried to create one dummy application there type of defined value is coming undefined so it always going to else part and web page, blocks are loading perfectly.

Could you please suggest how to solve this issue and why typeof define value = function in my application.


Thanks,
Rupak




Neil Fraser

unread,
Jun 8, 2022, 12:21:43 PM6/8/22
to blo...@googlegroups.com
If your application is using Dojo, or RequireJS, or some similar library, then it will inject an instance of AMD into your environment.  Once we detect that AMD exists, we attempt to use it.  However, it would appear that either our attempt is broken, or your instance of AMD is broken.  Can you determine which library your AMD is coming from?  Meanwhile, I'll check with the team to figure out what kind of AMD testing we've got going on.

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/CALyaOYoiRnc5Avfw9Bsp4xA580RGqE6pFPxDRtX2WS7T46u5KA%40mail.gmail.com.


--

Rupak Choudhury

unread,
Jun 8, 2022, 1:11:18 PM6/8/22
to blo...@googlegroups.com
Hi Neil,

Our application is using requireJs  and works fine with all dependent module but when we add blockly_compressed.js and blocks_compressed.js in our bundle config and try to access the page which uses Blockly library we get error as Blockly is not defined.

Thanks,
Rupak

Neil Fraser

unread,
Jun 8, 2022, 1:29:12 PM6/8/22
to blo...@googlegroups.com
Thanks for the info.  We'll look into whether Blockly can be loaded using RequireJS' AMD loading system.

Rupak Choudhury

unread,
Jun 10, 2022, 1:15:16 AM6/10/22
to blo...@googlegroups.com
Hi Neil,

Any update on the issue? We are bit stucked while updating Blockly framework. 

Thanks,
Rupak

Christopher Allen

unread,
Jun 13, 2022, 2:25:32 PM6/13/22
to blo...@googlegroups.com
Hi Rupak,

In my application i am getting typeof define value = function. So it's never going to else part (browser)…

I am not that familiar with requireJS, but If you are using requireJS (or any other AMD loader) then this is working as intended.  The "browser" part only applies if you are not using an AMD loader (like requireJS) and not using a CJS loader (like node.js).

Can you show us:
  1. The code in your application that loads requireJS, and
  2. The code that uses requireJS to load blockly_compressed.js?
and throwing an error Blockly is not defined while loading the page from browser.

Yes, this is probably due to attempting to load blockly_compressed.js via a <script> tag instead of via requireJS.

I suspect that the possible solutions to your problem are:
  • Do not use requireJS; load blockly_compressed.js via a <script> tag.
  • Use requireJS and then load blockly_compressed.js via whatever mechanism requireJS provides for loading modules (probably via calling its require() function).  (This is almost certainly what you want to do, since presumably you are using requireJS for some good reason and, since you're already using it, you might as well take advantage of its asynchronous loading features to load Blockly too.)
  • Load load blockly_compressed.js via a <script> tag before loading requireJS.  This will probably not work if you're using any kind of bundler, like the requireJS optimiser or webpack.
But show us your code and we might be able to offer you more concrete help.


I hope that helps,

Christopher

Reply all
Reply to author
Forward
0 new messages