Last updated: Apr 7, 2024
Reading time·4 min
The React.js ESLint error "Unexpected use of X no-restricted-globals" occurs for 2 main reasons:
window.
object.Here is an example of how the error occurs.
function App() { // ⛔️ Unexpected use of 'location'. eslint(no-restricted-globals) console.log(location); return ( <div style={{margin: 50}}> <h2>bobbyhadz.com</h2> </div> ); } export default App;
We tried to access the location
global variable directly which caused the
error.
location
property should be accessed on the window
object.Here is the complete stack trace.
Compiled with problems: ERROR [eslint] src/App.js Line 4:15: Unexpected use of 'location' no-restricted-globals Search for the keywords to learn more about each error.
window
objectIf you are trying to access a global variable, e.g. location
, confirm
or
alert
, access the variable on the window
object.
Here are some examples.
function App() { console.log(window.location); console.log(window.localStorage); console.log(window.sessionStorage); console.log(window.confirm); console.log(window.alert); return ( <div style={{margin: 50}}> <h2>bobbyhadz.com</h2> </div> ); } export default App;
If I open the application in my browser, I can see that the global variables are printed to the console and the error is resolved.
You can view all of the global variables that are available on the window
object in
this section of the
MDN docs.
Some of the commonly used variables are:
window.document
window.history
window.innerHeight
and window.innerWidth
window.location
window.localStorage
and window.sessionStorage
window.scrollX
and window.scrollY
window.top
window.clearInterval
and window.clearTimeout
window.alert
, window.confirm
and window.prompt
window.scrollTo
window.setTimeout
and window.setInterval
You can view all of the available properties on the window
object in
this section.
Another common cause of the error is defining a variable whose name clashes with
a global, e.g. location
, alert
, confirm
, etc.
Make sure that the name of the variable that causes the error doesn't clash with the name of a predefined global variable.
Make sure that the variable you're trying to access has been declared.
// 1) Defining the variable const myVariable = 'some value'; // 2) Accessing the variable console.log(myVariable);
If you try to access a variable that hasn't been defined, you will likely get the 'X' is not defined react/jsx-no-undef error.
In some cases, you might get the ESLint error for no good reason.
You can suppress the error by disabling the ESLint rule for a single line.
function App() { // eslint-disable-next-line no-restricted-globals console.log(location); return ( <div style={{margin: 50}}> <h2>bobbyhadz.com</h2> </div> ); } export default App;
The following comment has to be added above the line that causes the error.
// eslint-disable-next-line no-restricted-globals console.log(location);
The comment disables the no-restricted-globals
ESLint rule for the next line.
If you need to disable the no-restricted-globals
rule for an entire file, use
the following comment instead.
/* eslint-disable no-restricted-globals */ function App() { console.log(location); console.log(location); console.log(location); return ( <div style={{margin: 50}}> <h2>bobbyhadz.com</h2> </div> ); } export default App;
Make sure to add the comment at the top of your file.
/* eslint-disable no-restricted-globals */
If you need to disable the no-restricted-globals
ESLint rule for your entire
project, you have to edit the rules
section of your .eslintrc
config file.
{ "rules": { "no-restricted-globals": "off" } }
Add the no-restricted-globals
key to the rules
object in your .eslintrc
file and set the key's value to off
.
If you instead want to specify which globals should be restricted by the rule,
set the no-restricted-globals
key to an array of globals.
{ "rules": { "no-restricted-globals": ["error", "event", "fdescribe"] } }
If you get the error "'X' is not defined react/jsx-no-undef", check out the following article.
To solve the React.js ESLint error "Unexpected use of X no-restricted-globals", make sure:
window
object, e.g. window.location
.You can learn more about the related topics by checking out the following tutorials: