Pavel Mikhaylov
Pavel Mikhaylov

Reputation: 11

Why the error message "SyntaxError: let is a reserved identifier require.js:52:2" appears when developing firefox addon?

I am writing an firefox addon which uses Worker for background task. In browserOverlay.js:

var myWorker = new Worker("chrome://sample_xpcom/content/worker.js"); 
myWorker.postMessage({type: 10});

In worker.js:

importScripts("resource://gre/modules/workers/require.js")
//importScripts("resource://gre/modules/osfile.jsm")

onmessage = function(e) {
 console.log('Message received from main script:' + e.data);
 if (e.data.type == 10) {
   console.log('data:' + e.data.type);
}
}

When first two lines is commented out it works perfectly. Otherwise I am getting an error "SyntaxError: let is a reserved identifier require.js:52:2" in Browser console. According to MDN docs, OS.File for Workers this way is correct. What am I doing wrong? Firefox version is 39.0.3.

Upvotes: 0

Views: 2596

Answers (2)

mars2020
mars2020

Reputation: 11

I was getting the same issue but was able to make my addon work by using ChromeWorker instead of normal worker. try using

var myWorker = new ChromeWorker("chrome://sample_xpcom/content/worker.js"); 

Upvotes: 1

Josh Beam
Josh Beam

Reputation: 19802

EDIT

According to your comment:

there is the line "use strict" and then "let require = (function(){...})

From MDN:

First, in strict mode a short list of identifiers become reserved keywords. These words are implements, interface, let, package, private, protected, public, static, and yield. In strict mode, then, you can't name or use variables or arguments with these names.

To answer your other question:

Do I need to "clean" or reinstall firefox?

No, you need to use a different version of require.js.

Original Answer

Most modern browsers allow you to use let, which provides for block-scoped variables.

For example:

let a = 5;

What is block scope? For example, in JavaScript if you declare a var inside an if...else block, the variable gets hoisted to the top of the scope. However, if you were to declare a let inside an if...else, it stays scopes to that if...else block.

require.js is likely using let as a key or somewhere else in the code, so the browser is yelling at you saying that let is a reserved keyword (which it is... it is a block-scoped variable declaration.)

It's akin to doing something like var for = 5. for is a reserved keyword, so you can't do it.

I just tried to do var let = 5 in Firefox 39, and it gave an error as well.

Upvotes: 0

Related Questions