ReferenceError: localStorage is not defined - requireJS, r.js optimizer and localStorage

10,137

First off, you have a typo in your code above.

I didn't want to just edit your code, but I assume that:

 return localStorage.getItemfootv') ? JSON.parse(localStorage.getItem('foo')) : {};

Is supposed to be:

return localStorage.getItem('foo') ? JSON.parse(localStorage.getItem('foo')) : {};



With that change, I copied your code into a project of mine and it ran through the optimizer just fine. What version are you using? I tried with the latest version, 2.1.1.

If it's not an issue with the optimizer version you're using, I would try changing references of localStorage to window.localStorage and see if that make a difference.

Share:
10,137

Related videos on Youtube

akluth
Author by

akluth

Former Lead Software Engineer and (still) DevOps Evangelist, developing mainly with PHP, Go and Rust at work, bringing eCommerce shops to live, profiling performance bottlenecks and get rid of them and build up the complete CI/CD pipeline. For my new journey I decided to dive into the role of a Scrum Master and Agile Coach in order to empower teams to get the most out of them and the agile process. In my private time I hack in any language which comes accross my mind, currently I'm getting into SMW Rom Hacking and finally Haskell. I'm the Co-Maintainer of CGI.pm.

Updated on September 15, 2022

Comments

  • akluth
    akluth over 1 year

    I'm trying to optimize my requireJS JavaScript app with r.js. Unfortunately it stops when it rans over the function "localStorage" (the browser-builtin localStorage-function).

    The error message:

    ReferenceError: localStorage is not defined

    The function where the error occures:

    function getFromLocalStorage()
    {
        return localStorage.getItem('foo') ? JSON.parse(localStorage.getItem('foo')) : {};
    }
    

    How to fix this kind of error? Is is possible to use localStore within the r.js optimizer?

    More information:

    The build script:

    ({
        appDir: "../",
        baseUrl: "js/",
        dir: "./build",
        mainConfigFile: "app.js",
        paths: {
            jQuery: "empty:"
        },
        name: "app",
        findNestesDependencies: true
    })
    

    Application structure:

    /
    ---> /js (where app.js lies)
         ---> /app (here lies the file with the localStore-call)
         ---> /lib
    
  • akluth
    akluth over 11 years
    Thank you, this was just a typo, this typo wasn't in the original code. I'm using version 2.1.1 either and tried to use window.localStorage - unsuccessfully, now 'window' is not defined.
  • Waxen
    Waxen over 11 years
    I'm not sure exactly what to make of that, but that at least indicates that it's not a problem with using localStorage, but rather with the optimization process. Some quick googling turned up this: groups.google.com/forum/?fromgroups=#!msg/requirejs/Zt9FOu74‌​NqA/…. Check the command you're invoking the optimizer with and make sure it's correct.