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

`.npmrc` needs to be craeted in the workspace root, not the current workspace #72

Closed magurotuna closed 2 months ago

magurotuna commented 2 months ago

Issue

When using npm workspaces and trying to add JSR modules in one of the workspaces, it fails with NOT FOUND error.

Reproduction

Suppose the project structure looks like this, where npm workspaces is used:

.
├── package.json
└── workspace
    ├── index.mjs
    └── package.json

package.json

{
  "name": "simple_npm_workspace",
  "workspaces": ["workspace"]
}

workspace/package.json

{
  "name": "workspace",
  "main": "index.mjs"
}

Then let's say we want to import and use @std/async from JSR in the workspace, so run the following command:

$ cd workspace && npx jsr add @std/async

This errors out:

Setting up .npmrc...ok
Installing @std/async...
$ npm install @std/async@npm:@jsr/std__async
npm WARN ignoring workspace config at /private/tmp/simple_npm_workspace/workspace/.npmrc
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@jsr%2fstd__async - Not found
npm ERR! 404
npm ERR! 404  '@jsr/std__async@*' 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: /Users/yusuktan/.npm/_logs/2024-04-14T02_01_33_513Z-debug-0.log
Child process exited with: 1

Possible solution

The error above was apparently because .npmrc is created in the workspace directory (i.e. workspace/.npmrc), but npm doesn't recognize it. If I move the created .npmrc to the workspace root directory and run the npx jsr add command again, it succeeds (duplicate .npmrc is created again in the workspace directory though).

$ mv workspace/.npmrc ./.npmrc
$ cd workspace && npx jsr add @std/async
Setting up .npmrc...ok
Installing @std/async...
$ npm install @std/async@npm:@jsr/std__async
npm WARN ignoring workspace config at /private/tmp/simple_npm_workspace/workspace/.npmrc

added 4 packages, and audited 6 packages in 1s

found 0 vulnerabilities

Completed in 1s

So one possible solution to this issue would be that when npx jsr add command is invoked it should traverse up the parent directories to check if the current working directory is a npm workspace or not. If it is, .npmrc needs to be created in the workspace root instead of the current working directory.