Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"lunr is not defined" when strict parsing is enabled #145

Open
sagar-lcm opened this issue Nov 23, 2022 · 2 comments
Open

"lunr is not defined" when strict parsing is enabled #145

sagar-lcm opened this issue Nov 23, 2022 · 2 comments

Comments

@sagar-lcm
Copy link

sagar-lcm commented Nov 23, 2022

Hi! 👋

Firstly, thanks for your work on this project! 🙂
When the strict parsing option is enabled for typescript, we are getting the following error:
"errorType": "ReferenceError", "errorMessage": "lunr is not defined", "trace": [ "ReferenceError: lunr is not defined", " at /var/task/xxx.js:45:12", " at ../../node_modules/elasticlunr/elasticlunr.js (/var/task/xxx.js:1184:7)", " at __require (/var/task/etl.js:9:51)", " at Object.<anonymous> (/var/task/etl.js:61807:34)", " at Module._compile (node:internal/modules/cjs/loader:1105:14)", " at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)", " at Module.load (node:internal/modules/cjs/loader:981:32)", " at Function.Module._load (node:internal/modules/cjs/loader:822:12)", " at Module.require (node:internal/modules/cjs/loader:1005:19)", " at require (node:internal/modules/cjs/helpers:102:18)" ] }

Here is the diff that solved my problem:

diff --git a/node_modules/elasticlunr/elasticlunr.js b/node_modules/elasticlunr/elasticlunr.js
index 37d9986..034f0ce 100644
--- a/node_modules/elasticlunr/elasticlunr.js
+++ b/node_modules/elasticlunr/elasticlunr.js
@@ -87,7 +87,7 @@ elasticlunr.version = "0.9.5";
 
 // only used this to make elasticlunr.js compatible with lunr-languages
 // this is a trick to define a global alias of elasticlunr
-lunr = elasticlunr;
+var lunr = elasticlunr;
 
 /*!
  * elasticlunr.utils
@nekketsuuu
Copy link

This error seems to be fixed at current master branch (at least be7fdaa).

@hakeem-plutoflume
Copy link

We found a solution to this for our codebase. We migrated from Webpack to Vite v4.5.3 and because Vite doesn't use a bundle based web server, and serves dependencies directly to the browser as native ESM, we encountered this problem due to it running in strict mode.

For anyone that encounters this problem with Vite, this is the config that fixed it for us -

  /**
     *
     * In development mode, variables defined in `define` will be added as globals to the code.
     *
     * In production mode, variables defined would be replaced (find and replace) in the build output. Similar to a string find and replace.
     * It is used for example, replacing `process.env.NODE_ENV` with the actual value in the dependent bundles like react
     *
     */
    define:
      command === 'serve'
        ? {
            lunr: {},
          }
        : {
            // https://vitejs.dev/config/shared-options#define
            // elasticlunr fails in strict mode because lunr is declared as a global variable, without using 'var'
            // This is a workaround to make it work in strict mode during build. Does not work for dev
            'lunr = elasticlunr;': 'var lunr = elasticlunr;',
          },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants