question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple issues when using umd file

See original GitHub issue

Disclaimer

🙋‍♀️ I’m so sorry if I missed something really obvious in JS to remove these errors, but I’m stuck…

With v0.11.0

In my index.html

<script src="https://cdn.jsdelivr.net/npm/meilisearch@0.11.0/dist/bundles/meilisearch.umd.js"></script>

I get in the console:

ReferenceError: require is not defined
    <anonyme> https://cdn.jsdelivr.net/npm/meilisearch@0.11.0/dist/bundles/meilisearch.umd.js:5
Capture d’écran 2020-06-19 à 22 46 30

NB

No error message with:

<script src="https://cdn.jsdelivr.net/npm/meilisearch@0.10.1/dist/bundles/meilisearch.umd.js"></script>

With v0.10.1 (I use it because the v0.11.0 does not work)

I know this version is outdated but maybe there is the same issue in the v0.11.0 too.

In my index.html file:

<script src="https://cdn.jsdelivr.net/npm/meilisearch@0.11.0/dist/bundles/meilisearch.umd.js"></script>
<script src="./src/app.js"></script>

In .src/app.js

const meiliClient = new Meilisearch({
  host: 'http://127.0.0.1:7700',
  apiKey: 'masterKey'
})

I get this error in the console:

TypeError: instance is undefinedmeilisearch.umd.js:151:17
    MeiliAxiosWrapper https://cdn.jsdelivr.net/npm/meilisearch@0.10.0/dist/bundles/meilisearch.umd.js:151
    Meilisearch https://cdn.jsdelivr.net/npm/meilisearch@0.10.0/dist/bundles/meilisearch.umd.js:1098
    <anonyme> file:///Users/curquiza/.../src/app.js:69
Capture d’écran 2020-06-19 à 23 09 33

Plus, you can notice I had to write new Meilisearch to get it worked (and not new MeiliSearch) which is really confusing comparing with the Getting Started.

Conclusion

These issues have to be fixed:

  • The UMD file should be importable in an HTML (see the first point)
  • The UMD file should be usable in a vanilla JS file passed as a script in an HTML file (see the second point)
  • Meilisearch has to be renamed into MeiliSearch in the UMD file
  • Add tests to check both of the first points if this is possible 😇

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
bidoubiwacommented, Jun 22, 2020

The reason why it was not importable between two version is because an overwrite of the umd bundle with the cjs bundle was done in rollup.config.js. This has been fixed.

For the next version this is how import will work in the client:

How to import MeiliSearch in v0.11.1 >

Node - CommonJs

const MeiliSearch = require('meilisearch')

Browser - vanilla JS

<script src="https://cdn.jsdelivr.net/npm/meilisearch@latest/dist/bundles/meilisearch.browser.js"></script>
<script>
  const client = new window.Meiliearch({
    host: 'http://127.0.0.1:7700',
    apiKey: 'masterKey',
  })
  client.listIndexes().then(res => {
    console.log({ res });
  })

EcmaScript module - esm

import MeiliSearch from 'meilisearch'

Important, if your bundler uses browser as primary entry key, please change it to module. Example in webpack:

resolve: {
    mainFields: ["module", "browser"] // the `module` field has priority on the browser field
  },

EcmaScript module Typescript - esm

import MeiliSearch, { Index, SearchResponse } from 'meilisearch'
2reactions
bidoubiwacommented, Jun 22, 2020

To make it work with the UMD of the v0.10.1 you should add the axios CDN like this (order is important):

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meilisearch@0.10.1/dist/bundles/meilisearch.umd.js"></script>

<script>
  console.log(window.Meilisearch);
  const client = new window.Meilisearch({
    host: 'http://127.0.0.1:7700',
    apiKey: 'masterKey',
  })
  client.listIndexes().then(res => {
    console.log({ res });

  })
</script>
Read more comments on GitHub >

github_iconTop Results From Across the Web

University of Maryland Box Service Frequently Asked ...
What is Box? Box is a secure, cloud-based storage and collaboration service that allows customers to upload, download, and share multiple file types....
Read more >
Can't use UMD file in my page - error is "<name> is not a ...
Answering my own question: There were two problems: I didn't understand that Rollup needs to provide a name for a UMD module; I...
Read more >
Output - webpack
webpack will not write output file when file already exists on disk with the same content. ... If multiple modules would result in...
Read more >
rollup.js
You can export an array from your config file to build bundles from several unrelated inputs at once, even in watch mode. To...
Read more >
Compiling and bundling TypeScript libraries with Webpack
The ideal would be to just have two outputs: commonjs and es modules. The UMD file would serve our needs as commonjs if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found