Skip to content

node: prefix not stripped from added dependency specifier #10052

@klswn

Description

@klswn

🐛 bug report

When importing a node: prefixed builtin, the JS transformer is stripping the prefix in the import statement but the specifier on the added dependency is not mutated. When the packager generates the runtime code, it's mapping the node:{{builtin}} string to the public asset id instead of just {{builtin}}, which throws a Uncaught Error: Cannot find module {{builtin}} runtime error.

🎛 Configuration (.babelrc, package.json, cli command)

Version: 2.13.2

Default configuration running parcel serve

🤔 Expected Behavior

The runtime code should reference the non-prefixed builtin specifier

😯 Current Behavior

The runtime code references the node: prefixed specifier (from the REPL example pasted below):

"51LRJ":[function(require,module,exports,__globalThis) {
  var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
  var _nodeUrl = require("url"); <-- node: prefix is stripped
  var _nodeUrlDefault = parcelHelpers.interopDefault(_nodeUrl);
  function getHost(u) {
      return (0, _nodeUrlDefault.default).parse(u).host;
  }
  let testUrl = "https://test.com";
  document.body.innerHTML = getHost(testUrl);
},{
"node:url":"erk9V", <-- node: prefix is not stripped. throws runtime error.
"@parcel/transformer-js/src/esmodule-helpers.js":"03iyH"
}

💁 Possible Solution

I believe we just need to strip the prefix in dependency_collector.rs before adding the dependency in the JS transformer. But I'm not sure if that has other implications that I'm unaware of. I'm happy to assist with a PR if that path makes sense.

🔦 Context

We've run into a few runtime errors in our application attempting to import node: packages.

💻 Code Sample

REPL Example

🌍 Your Environment

I'm able to replicate in the REPL

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions