denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
93.4k stars 5.18k forks source link

an attempted import of Node's built-in "zlib" module is being interpreted as being for a "zlib" NPM module #24274

Open jeremyBanks opened 2 weeks ago

jeremyBanks commented 2 weeks ago

Attempting to import npm:contentful-management produces the following error (the same as denoland/deno_ast#227, but per https://github.com/denoland/deno_ast/issues/227#issuecomment-2179031517 it's a different underlying issue):

> deno
Deno 1.44.2
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> import "npm:contentful-management"
Uncaught TypeError: Could not find npm package 'zlib' referenced by 'contentful-management@11.27.3'.
    at async <anonymous>:1:22
>

It seems like it might be interpreting a locally-ambiguous import of zlib that should be resolved as the built-in node module (node:zlib) instead as if it's an import for an NPM module of the same name (npm:zlib), but from a quick look I wasn't able to identify where the problematic import is actually happening.

jeremyBanks commented 2 weeks ago

I've tried to reduce this by using --node-modules-dir and editing it down locally. If I'm understanding correctly, these imports seem to resolve incorrectly when they're on the right-side of an export assignment.

// error: could not find package 'buffer'
module.exports = require("buffer");

// error: could not find package 'buffer'
exports = require("buffer");

// error: could not find package 'buffer'
function f() {
  exports = require("buffer");
}

//error: could not find package 'node:buffer'
module.exports = require("node:buffer");

If we make the export indirect, the imports are successful and we're able to use the re-exported value successfully.

// successful
const buffer = require("buffer");
module.exports = buffer;

// successful
const nodeBuffer = require("node:buffer");
module.exports = nodeBuffer;