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

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:micromatch@4.0.5";

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

file:///Users/oscarotero/test/main.ts (44B)
└─┬ npm:/micromatch@4.0.5 (54.64KB)
  ├─┬ npm:/braces@3.0.2 (48.08KB)
  │ └─┬ npm:/fill-range@7.0.1 (15.97KB)
  │   └─┬ npm:/to-regex-range@5.0.1 (22.4KB)
  │     └── npm:/is-number@7.0.0 (9.39KB)
  └── npm:/picomatch@2.3.1 (87.84KB)

To replace braces@3.0.2 with another version, say braces@2.0.0, I add a new entry in the import map:

{
  "imports": {
    "npm:braces@3.0.2": "npm:braces@2.0.0",
  },
}

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

{
  "imports": {
    "npm:braces@3.0.2": "npm:braces@2.0.0",
    "npm:braces@^3.0.2": "npm:braces@2.0.0",
    "braces": "npm:braces@2.0.0"
  },
  "scopes": {
    "npm:micromatch@4.0.5": {
      "npm:braces@^3.0.2": "npm:braces@2.0.0",
      "braces": "npm:braces@2.0.0"
    }
  }
}

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:braces@3.0.2": "npm:braces@2.0.0",
    "npm:braces@^3.0.2": "npm:braces@2.0.0",
    "braces": "npm:braces@2.0.0"
  },
  "scopes": {
    "npm:micromatch@4.0.5": {
      "npm:braces@^3.0.2": "npm:braces@2.0.0",
      "braces": "npm:braces@2.0.0"
    }
  }
}

Now I have the following error:

file:///Users/oscarotero/test/main.ts (44B)
└── Could not find a matching package for 'npm:micromatch@4.0.5' 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:micromatch@4.0.5' 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