Skip to content

🐛 BUG: dev server fails with "Uncaught TypeError: require$$0 is not a function" due to incorrect require() transform #3911

@ncpa0cpl

Description

@ncpa0cpl

Quick checklist

  • I am using the latest version of Snowpack and all plugins.

What package manager are you using?

yarn

What operating system are you using?

Linux

Describe the bug

Dev server fails with an Uncaught TypeError: require$$0 is not a function. This error occurs in a transformed file of a define-properties package.

It appears to me that this bug occurs due to incorrect transformation of that package require() call. In the package the following line:

var hasPropertyDescriptors = require('has-property-descriptors')();

is being transformed to

import * as hasPropertyDescriptors$1 from '/_snowpack/pkg/has-property-descriptors.v1.0.0.js';

function getDefaultExportFromNamespaceIfNotNamed (n) {
	return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
}

var require$$0 = /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(hasPropertyDescriptors$1);

var hasPropertyDescriptors = require$$0();

in the original code the require() returns a function, in the transformed code however, the getDefaultExportFromNamespaceIfNotNamed()is returning the module with that function under a "default" property, leading to this error. I suspect this is because that module has more than one property and Object.keys(n).length === 1 condition returns false.

This error does occur only when using a development server.

Steps to reproduce

  1. npx create-snowpack-app
  2. install define-properties (npm i define-properties)
  3. import that package and use it
  4. start the dev server
  5. open the page in the browser
  6. it will fail with a Uncaught TypeError: require$$0 is not a function error in the console

Link to minimal reproducible example (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions