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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require.resolve is undefined in compiled code #6925

Closed
ascorbic opened this issue Sep 14, 2021 · 7 comments
Closed

require.resolve is undefined in compiled code #6925

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

Comments

@ascorbic
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": "[email protected]:ascorbic/parcel-bug.git"
}

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

Repo: https://github.com/ascorbic/parcel-bug/

Compiled output:

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


export {$fd0e4966f38fc4e4$export$151edcd6c2cd6dd7 as handler};
//# sourceMappingURL=module.js.map

🌍 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
@amiller-gh
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.

@amiller-gh
Copy link

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

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

@aariacarterweir
Copy link

Would really like to see this properly resolved :)

@conorzhong
Copy link

Would really like to see this properly resolved :)

Same

@chrisdrackett
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

@MBelniak
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
Labels
Stale Inactive issues
Projects
None yet
Development

No branches or pull requests

7 participants