When `.npmrc` contains comments, the `nuxi module add` command fails.
When the .npmrc file contains a comment on the same line as a registry setting, the nuxi module add command fails.
.npmrc:
registry=https://registry.npmjs.org # test comment
Steps to reproduce:
$ git clone https://github.com/odan-sandbox/nuxt-module-npmrc-comment-error-repro
$ cd nuxt-module-npmrc-comment-error-repro
$ npm i
$ npx nuxi module add pinia
[2:16:01] ERROR [GET] "https://registry.npmjs.org # test comment/@pinia/nuxt": <no response> Failed to parse URL from https://registry.npmjs.org # test comment/@pinia/nuxt
at async $fetchRaw2 (node_modules/nuxi/dist/shared/nuxi.BzZJv2UC.mjs:371:14)
at async $fetch2 (node_modules/nuxi/dist/shared/nuxi.BzZJv2UC.mjs:417:15)
at async resolveModule (node_modules/nuxi/dist/chunks/add2.mjs:315:22)
at async Promise.all (index 0)
at async Object.setup (node_modules/nuxi/dist/chunks/add2.mjs:150:34)
at async runCommand$1 (node_modules/nuxi/dist/shared/nuxi.BlFGnQYG.mjs:1739:5)
at async runCommand$1 (node_modules/nuxi/dist/shared/nuxi.BlFGnQYG.mjs:1758:11)
at async runCommand$1 (node_modules/nuxi/dist/shared/nuxi.BlFGnQYG.mjs:1758:11)
at async runMain$1 (node_modules/nuxi/dist/shared/nuxi.BlFGnQYG.mjs:1896:7)
[cause]: Failed to parse URL from https://registry.npmjs.org # test comment/@pinia/nuxt
at node:internal/deps/undici/undici:13510:13
at async $fetchRaw2 (node_modules/nuxi/dist/shared/nuxi.BzZJv2UC.mjs:359:26)
[cause]: Invalid URL
at new URL (node:internal/url:818:25)
at new Request (node:internal/deps/undici/undici:9586:25)
at fetch (node:internal/deps/undici/undici:10315:25)
at fetch (node:internal/deps/undici/undici:13508:10)
at fetch (node:internal/bootstrap/web/exposed-window-or-worker:75:12)
at node_modules/nuxi/dist/shared/nuxi.BzZJv2UC.mjs:451:58
at $fetchRaw2 (node_modules/nuxi/dist/shared/nuxi.BzZJv2UC.mjs:359:32)
at onError (node_modules/nuxi/dist/shared/nuxi.BzZJv2UC.mjs:280:16)
at $fetchRaw2 (node_modules/nuxi/dist/shared/nuxi.BzZJv2UC.mjs:371:20)
at async $fetch2 (node_modules/nuxi/dist/shared/nuxi.BzZJv2UC.mjs:417:15)
[2:16:01] ERROR [GET] "https://registry.npmjs.org # test comment/@pinia/nuxt": <no response> Failed to parse URL from https://registry.npmjs.org # test comment/@pinia/nuxt
Expected behavior:
The command should ignore comments in .npmrc and proceed with the module installation.
Cause of the issue
It is likely that the .npmrc file is not correctly removing comments when parsing the registry setting.
There is a bug in the regular expression used in the getRegistryFromFile function.
https://github.com/nuxt/cli/blob/54e9850d39d44162bc2207ce22ef43518daf1000/packages/nuxi/src/commands/module/add.ts#L402
Possible solutions:
- Modify the regular expression to account for the presence of comments.
- Introduce confbox for parsing
.npmrc.
I am interested in resolving this issue. If you can suggest a preferred solution, I would like to create a pull request to fix this bug.
using confbox is good idea. Also we might introduce a standard util for unjs/nypm to read npmrc config. (PR more than welcome)
Thank you for the suggestion. Since modifying unjs/nypm might be a bit too time-consuming at this point, I would like to proceed with directly using confbox to handle .npmrc parsing in nuxi. I will proceed with this approach and create a pull request. Feel free to review it.