Saurabh Sharma
Saurabh Sharma

Reputation: 503

Can only have one anonymous define call per script file

I'm creating monaco editor using loader.js but getting the error "Can only have one anonymous define call per script file" 2 times in console.

<script src="/monaco-editor/min/vs/loader.js"></script>

Code to create editor

require.config({ paths: { 'vs': '/monaco-editor/min/vs' }});
 require(['vs/editor/editor.main'], function() {                
    monacoEditor= monaco.editor.create(document.getElementById('coding-editor'), {
        value: [
        'function x() {',
        '\tconsole.log("Hello world!");',
        '}'
    ].join('\n'),
    language: 'javascript'
    });
 });

I tried to search the issue and found below related answer:

Some piece of code that you are loading is invoking define with an anonymous module id. You could:

load that code through the AMD loader (i.e. manually require it) such that the AMD loader creates the <script> tag.

load that code before the AMD loader (i.e. define will not be available to that piece of code)

unset define for the duration of evaluation of that script (i.e. if you load it with a <script> tag, then unset define before and restore it afterwards)

try to unset define.jquery, AFAIK jquery might be checking for that on the define function

This page has lot of jquery already and I understand this because of jQuery. Please help some to make me understood by example. Thanks

Upvotes: 9

Views: 7683

Answers (2)

Adele
Adele

Reputation: 59

I had tried to create script by tags, but got aler:'Can only have one anonymous define' So I just overwrite it :

        this.temp_define = window['define'];
        head.appendChild(loaders);
        window['define'] = undefined;

Upvotes: -1

Alberto Castillo G
Alberto Castillo G

Reputation: 21

I had the same issue this morning and I applied the second solution.

load that code before the AMD loader (i.e. define will not be available to that piece of code)

This works because define is being called from inside jQuery anonymously, as the error says. Explained further in the require.js website, which happens to use loader function (define, require) similar to loader.js.

In my case I simply made sure to include my loader after jQuery so the defines don't collide.

Upvotes: 2

Related Questions