'Svelte: Uncaught ReferenceError: exports is not defined

Background

I am trying to use dgraph-js-http in a Svelte application.

Error

The initial error I was facing was:

Uncaught ReferenceError: buffer is not defined
    <anonymous> http://localhost:5000/build/bundle.js:6947

I'm including this as maybe my solution to this was not the best, and that hopefully there is a way to solve both this issue, and the current blocker issue.

However, after a trial-and-error approach (see below for the various things I've tried) I'm now stuck.

Current error

After solving the above error, I am now blocked on this error:

Uncaught ReferenceError: define is not defined

Attempts to resolve

None of the following have worked.

  • Add <script src="https://bundle.run/[email protected]"></script> to index.html
  • Run npm install stream
  • Add import * as Stream from "stream" to the App.svelte file
  • Add import * as Stream from "stream" to the stores.js file
  • Add preferBuiltins:true to rollup.config.js
  • Add npm install --save-dev rollup-plugin-node-polyfills
  • Add preferBuiltins:false to rollup.config.js. This resolved prior errors and resulted in the new error of: Uncaught ReferenceError: define is not defined
  • Add npm install --save define. This, now results in the current blocker:
Uncaught ReferenceError: exports is not defined


Solution 1:[1]

I think you missed type: "module" attribute in package.json. Here, the type attribute represents whether the type of javascript interpreter is commonjs or babel. type: "module" indicates that you are going to use babel as the ECMA script interpreter.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Exonnix Kai