jsr-io / jsr-npm

A cli tool to make installing packages form jsr.io in node easy
https://npmjs.com/package/jsr
MIT License
88 stars 12 forks source link

Package installation error caused by parent directory package.json #56

Closed Hexagon closed 4 months ago

Hexagon commented 4 months ago

The problem

When running npx jsr add @cross/env in a directory without a package.json file, and a package.json file exists in a parent directory, you do receive an error message like this:

➜  examples git:(main) ✗ npx jsr add @cross/env   
Installing @cross/env...
$ npm install @cross/env@npm:@jsr/cross__env
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@jsr%2fcross__env - Not found
npm ERR! 404 
npm ERR! 404  '@jsr/cross__env@*' is not in this registry.
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in: /home/<user>/.npm/_logs/2024-03-10T23_19_06_360Z-debug-0.log

The same goes for bunx jsr

Steps to reproduce

  1. Create /home/user/package.json

  2. Create a new folder /home/user/git/repo/ and cd to it

  3. Run npx jsr add @cross/env

Actual result

Above error message, saying that the package was not found

Expected result

Ideally, JSR should either:

Workround

Create a valid package.json file in the directory where you want to install the package. This will override the parent directory's package.json and ensure that everything goes as planned.

marvinhagemeister commented 4 months ago

@Hexagon I'm unable to reproduce the described issue. It works as expected on my end. I did this:

mkdir my-project
cd my-project
npm init -y # creates package.json
mkdir -p sub/sub1
cd sub/sub1
npx jsr add @cross/env # works as expected
marvinhagemeister commented 4 months ago

Ah nvm I'm not able to reproduce it with npm, but with pnpm

Hexagon commented 4 months ago

Oh, i'll try to narrow it down later, i used regular npm with latest node 21

marvinhagemeister commented 4 months ago

I think I found the cause. The project directory detection code was flawed. Opened #57 to fix this.