Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

require.resolve is undefined in compiled code #6925

ascorbic opened this issue Sep 14, 2021 · 7 comments

require.resolve is undefined in compiled code #6925

ascorbic opened this issue Sep 14, 2021 · 7 comments
Stale Inactive issues


Copy link

ascorbic commented Sep 14, 2021

馃悰 bug report

In code that contains require.resolve, the function is replaced with undefined

馃帥 Configuration (.babelrc, package.json, cli command)

  "name": "parcel-bug",
  "version": "1.0.0",
  "description": "",
  "source": "src/index.js",
  "main": "lib/main.js",
  "module": "lib/module.js",
  "scripts": {
    "build": "parcel build"
  "devDependencies": {
    "parcel": "^2.0.0-rc.0"
  "repository": ""

The source:

export function handler() {
  const pad = require.resolve("left-pad");
  console.log({ pad });

馃 Expected Behavior

Line 4 is compiled to const pad = require.resolve("left-pad");

馃槸 Current Behavior

It is compiled to const pad = undefined("left-pad");

馃拋 Possible Solution

馃敠 Context

I am writing a build plugin for Netlify and need to find the path to a dependency in the user's site.

馃捇 Code Sample


Compiled output:

function $fd0e4966f38fc4e4$export$151edcd6c2cd6dd7() {
    const pad = undefined("left-pad");
        pad: pad

export {$fd0e4966f38fc4e4$export$151edcd6c2cd6dd7 as handler};

馃實 Your Environment

Software Version(s)
Parcel 2.0.0-rc.0
Node v16.2.0
npm/Yarn npm 7.13.0
Operating System macOS 11.2.3
Copy link

amiller-gh commented Dec 1, 2021

As a note, both whatwg and Node.js have been working on import.meta.resolve: nodejs/node#31032

Parcel may want to deal with this API as well.

Copy link

Workaround: re-create your require object with the module module.

import { createRequire } from 'module';
const req = createRequire(__dirname);

Copy link

Would really like to see this properly resolved :)

Copy link

Would really like to see this properly resolved :)


Copy link

ran into this today. The code in question is in a 3rd party package. I'm going to attempt to patch it using the above workaround

Copy link

MBelniak commented May 25, 2023

Any plans on working on that? It makes me want to ditch parcel, as this is kinda basic functionality IMO. AFAIK webpack provides __non_webpack_require__ for such things

Copy link

github-actions bot commented Dec 4, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.

@github-actions github-actions bot added the Stale Inactive issues label Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Stale Inactive issues
None yet

No branches or pull requests

7 participants