rollup
rollup copied to clipboard
Uncaught ReferenceError: exports is not defined when bundling a node module
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).