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

Import maps don't work for mapping npm dependencies of npm packages #23459

Closed
oscarotero opened this issue Apr 19, 2024 · 1 comment
Closed

Comments

@oscarotero
Copy link
Contributor

Version: Deno 1.42.4

I'm trying to replace a npm dependency using import maps, but suprisingly it doesn't seem to work.
Let's say I have this code:

// main.ts

import * as foo from "npm:[email protected]";

If I run deno info main.ts I have the following dependency graph:

file:///Users/oscarotero/test/main.ts (44B)
└─┬ npm:/[email protected] (54.64KB)
  ├─┬ npm:/[email protected] (48.08KB)
  │ └─┬ npm:/[email protected] (15.97KB)
  │   └─┬ npm:/[email protected] (22.4KB)
  │     └── npm:/[email protected] (9.39KB)
  └── npm:/[email protected] (87.84KB)

To replace [email protected] with another version, say [email protected], I add a new entry in the import map:

{
  "imports": {
    "npm:[email protected]": "npm:[email protected]",
  },
}

But after running deno info main.ts again, I see the same result without any change.
I tried different ways without luck:

{
  "imports": {
    "npm:[email protected]": "npm:[email protected]",
    "npm:braces@^3.0.2": "npm:[email protected]",
    "braces": "npm:[email protected]"
  },
  "scopes": {
    "npm:[email protected]": {
      "npm:braces@^3.0.2": "npm:[email protected]",
      "braces": "npm:[email protected]"
    }
  }
}

I saw this issue #23310 and from I understand, I have to enable the unstable byonm feature. So I added it to my deno.json file:

{
  "unstable": ["byonm"],
  "imports": {
    "npm:[email protected]": "npm:[email protected]",
    "npm:braces@^3.0.2": "npm:[email protected]",
    "braces": "npm:[email protected]"
  },
  "scopes": {
    "npm:[email protected]": {
      "npm:braces@^3.0.2": "npm:[email protected]",
      "braces": "npm:[email protected]"
    }
  }
}

Now I have the following error:

file:///Users/oscarotero/test/main.ts (44B)
└── Could not find a matching package for 'npm:[email protected]' in '/Users/oscarotero/test/package.json'. You must specify this as a package.json dependency when the node_modules folder is not managed by Deno. (resolve error)

I don't understand what it means. Should I create a package.json file with this? Anyway, I did it:

{
  "dependencies": {
    "micromatch": "4.0.5"
  }
}

And now I have the following error:

file:///Users/oscarotero/test/main.ts (44B)
└── Could not find '/Users/oscarotero/test/node_modules/micromatch/package.json'. Deno expects the node_modules/ directory to be up to date. Did you forget to run `npm install`? (resolve error)

As far as I understand, I have to install the package with NPM? What's the point of that? Anyway, I run npm i and then deno info main.ts again, which returns the following error:

file:///Users/oscarotero/test/main.ts (44B)
├── file:///Users/oscarotero/test/node_modules/micromatch/index.js (unknown)
└── Failed resolving package subpath for 'npm:[email protected]' in '/Users/oscarotero/test/node_modules/micromatch'. (resolve error)

And now I'm lost. Any idea how to make it work?

@dsherret
Copy link
Member

dsherret commented Apr 23, 2024

This is tracked in #18191 (Import maps currently can't be used to change dependencies of npm packages, but yeah, byonm would be the way to do this atm)

I opened #23507 for the other issue you mentioned.

@dsherret dsherret closed this as not planned Won't fix, can't repro, duplicate, stale Apr 23, 2024
@dsherret dsherret changed the title Import maps don't work for NPM Import maps don't work for mapping npm dependencies of npm packages Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants