-
-
Notifications
You must be signed in to change notification settings - Fork 491
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
Usage in ESM environment #341
Comments
I’m also having ESM issues in a Gatsby app: // eslint error: Worker not found in 'flexsearch'
import { Worker } from "flexsearch";
// Build error: WebpackError: TypeError: flexsearch__WEBPACK_IMPORTED_MODULE_3__.Worker is not a constructor
const searchWorker = new Worker({ preset: "match" }); The weird thing is, Worker does exist in dev mode, and it’s a function. So there may be something going on during the build step with Webpack and this package’s module configuration. I’ve tried pinning to import FlexSearch from "flexsearch"; // or…
import * as FlexSearch from "flexsearch"; // or…
const FlexSearch = require("flexsearch");
const { Worker } = FlexSearch; and they all have the same problem. EDIT I had some success by preventing import { Worker } from "flexsearch";
export default function ExampleComponent () {
const searchWorker = useRef(null);
useEffect(() => {
searchWorker.current = new Worker({ preset: "match" });
}, []);
<div>
<button onClick={() => searchWorker.current?.add("id", "value")}>Example</button>
</div>
} |
I'm having the same issue.. I'm using import Index from "flexsearch/dist/module/index.js"; but on build I'm receiving using vite to create the bundle.. (node:10776) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. |
If I add {
"name": "flexsearch",
"version": "0.7.31",
"type": "module",
...
} |
Not sure if this answers your questions, but this is how I import for example import pkg from "flexsearch";
const { Document } = pkg; |
it also brakes when I try to build. |
@ts-thomas would replacing If you think yes, I can look into how to do it... I had some other problem with so if replacing this with something else would solve two issues at once, I'm willing to invest some time,let me know |
maybe some more modern tool can replace both also is Babel needed at all? It is also obsolete, I believe |
It doesn't work for me either, I couldn't succeed somehow |
Thanks for publishing this! @ts-thomas, do you think any of @Akryum’s changes could find their way upstream? |
I am getting
|
@Tasin5541 The code doesn't export a |
@ts-thomas do you plan to support ESM? At this point, it seems the package isn't usable with most of the modern web frameworks if ESM is not a supported environment, unless I'm missing something. EDIT: Apparently the following imports do work, at least in a Vite + Svelte project I'm working on: import Index from "flexsearch/dist/module";
import Worker from "flexsearch/dist/module/worker";
import Document from "flexsearch/dist/module/document"; EDIT 2: I've created a PR #398 that updates the docs to explain how to import the ESM modules. |
The existing docs show a relative path, not the actual npm module bundle path. I believe this should be correct for ESM/ES6 type environments and will hopefully prevent confusion as found in nextapps-de#341 etc.
I had a hard time to make it running in a Typescript project, but importing the modules from import Document from "flexsearch/src/document";
interface IndexedDocument {
id: string;
firstName: string;
lastName: string;
}
const docIndex = new Document<IndexedDocument, true>({
document: {
id: "id",
index: [ "firstName", "lastName" ],
store: true,
},
}); |
* chore: Initial vite migration * lint: Fix vite eslint * chore: Migrate HtmlWebpackPlugin * chore: Migrate CopyPlugin * chore: Replace webpack script with vite * ci: Trigger for refactor/ branches * fix: Snippets * chore: Rename index.ejs -> index.html * ci: `static/` -> `dist/` * chore: Migrate ServiceWorker to Vite * chore: Import locales at compile time * chore: Exported locales should contain `data` property * chore: Register SW * ci: Keep it `static/` * refactor: Set output dir from vite config * chore: Add `require(...)` support back * feat: Resolve and stuff * fix: Move index.html * fix: Resolve ambigous import * fix: Browserify path * chore: Remove crypto related stuff I don't do crypto * fix: Use `@akryum/flexsearch-es` REF: nextapps-de/flexsearch#341 * fix: webpack -> vite-ignore * fix: Vite env * chore: Ignore some vite temp files * revert: Vite env * revert: Vite env * chore: More vite env stuff * fix: CSP Imported CSS still doesn't work * fix: Entrypoint shouldn't be a relative path * fix: Adjust initial nonce * chore: TS Checker * fix: Adjust eslintignore * lint: Adjust eslintignore * fix: Adjust lint command * chore: Disable eslint checker for now For some reason it keep ignoring eslintignore (ironic) * chore: It's actually ts checker that keep checking node_modules * fix: lint command * chore: Disable stylelint * fix: Yarn can't be used for lint command * chore: Temporarily disable eslint * fix: CSP in dev mode * chore: CSP adjustment CSP may or may not be fine on prod... we'll see... * fix: Slight adjustment * fix: CSP forr vite-plugin-checker * fix: Resolve warnings * fix: More warning fixing * fix: Yet another warning fixes * fix: SW typing * fix: It's webworker not dom * chore: Remove webpack * chore: Disable eslint and stylelint * fix: Hopefully one last error fix * chore: Update immer to v10 * fix: Regex * chore: Update more deps * chore: Downgrade toast to v2.4.0 * chore: Remove dangerfile * fix: Dynamic locale import * fix: Try use __webpack_nonce__ just in case * fix: vite-plugin-require * revert: Revert "fix: vite-plugin-require" This reverts commit 7c4e15f. * chore: Remove keymap temporarily * fix: Goober * fix: nonce * chore: Temporarily disable hotkey * fix: Properly null check textarea * fix: NODE_ENV
I'm not able to use the latest version (0.7.31) in an ESM environment, such as a Vite app (browser-side) or a native-ESM node app (with TypeScript). The document doesn't also doesn't have anything about this.
The situation was a little bit better in version 0.7.21 with imports like this:
Vite:
Node ESM:
(Notice we need a module loader such as jiti or vite-node to be able to load other files which are not marked as ESM).
The text was updated successfully, but these errors were encountered: