rollup / rollup/rollup

Uncaught ReferenceError: exports is not defined when bundling a node module

Open
#5,293 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
26.3k
Forks
1.8k
Avg merge
2d 11h
Merged PRs (30d)
20

Description

I have a simple ts application that I run in the browser. To bundle the ts files I am using rollup js.
I installed diffusion package
npm install diffusion

And import the module to my ts file app.ts:

import * as diffusion from 'diffusion';
diffusion.connect({/* options */});

Now when I start the application in the browser I get the following:

diffusion.js:5 Uncaught ReferenceError: exports is not defined
    at diffusion.js:5:23

This is the line that causes the error:
Object.defineProperty(exports, "__esModule", { value: true });

The question is how to bundle a node module that uses commenJs correctly with rollup.

This is my current rollup.config.mjs file:

import { createSpaConfig } from '@open-wc/building-rollup';
import typescript from '@rollup/plugin-typescript';
import merge from 'deepmerge';
import nodeResolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";

const baseConfig = createSpaConfig({
  outputDir: 'dist',
  nodeResolve: { browser: true, dedupe: ['lit-html'] },
  developmentMode: process.env.ROLLUP_WATCH === 'true',
  html: {
    extractAssets: false,
    minify: false,
  },
  injectServiceWorker: false,
  legacyBuild: false,
  workbox: false,
});


export default merge(baseConfig, {
  input: 'index.html',
  plugins: [
    typescript({
      declaration: false,
      sourceMap: false,
      inlineSources: false,
      compilerOptions: {
        target: 'esnext',
      },
    }),
    nodeResolve(),
    commonjs({
      include: /node_modules/,
      sourceMap: false,
    })
  ]
});

I tried to reproduce the app here, using parcel (Unfortunately I was not able to start the app with web-dev-server and rollup inside the codesandbox.io, but I provided there the same app structure I am using locally).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with app.ts and rollup.config.mjs, then reproduce the browser build using the listed TypeScript, node-resolve, and commonjs plugins. Inspect the generated diffusion.js at the Object.defineProperty(exports, "__esModule", ...) line; done means the diffusion module bundles and runs in the browser without the ReferenceError.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, typescript
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.