npm / cli

the package manager for JavaScript
https://docs.npmjs.com/cli/
Other
8.46k stars 3.15k forks source link

`npm config` fails in workspaces #6099

Open jeffrson opened 1 year ago

jeffrson commented 1 year ago

Is there an existing issue for this?

This issue exists in the latest npm version

Current Behavior

Since npm@9.3.0 calling npm config list or npm config get registry inside a workspace of a worktree results in an error:

❯ npm config list
npm ERR! code ENOWORKSPACES
npm ERR! This command does not support workspaces.

Here's part of the log:

28 timing npm:load:logFile Completed in 12ms
29 timing npm:load:timers Completed in 0ms
30 timing npm:load:configScope Completed in 0ms
31 timing npm:load Completed in 46ms
32 timing command:config Completed in 1ms
33 verbose stack Error: This command does not support workspaces.
33 verbose stack     at Config.cmdExec (...\node_modules\npm\lib\base-command.js:123:29)
33 verbose stack     at Npm.exec (...\node_modules\npm\lib\npm.js:154:20)
33 verbose stack     at async module.exports (...\node_modules\npm\lib\cli.js:134:5)

It used to work in 9.2.0.

Expected Behavior

Well, show (and use!) all configs.

Steps To Reproduce

Build a worktree with packages. .npmrc in root. In a package call npm config list.

Environment

anilkeshav27 commented 1 year ago

npm config get registry also causes the same error , in the node:lts-buster docker image with npm version 9.3.1

niteshagrawalgmail commented 1 year ago

+1 . We are using node:lts-buster for our builds and are facing same issue.

ferferga commented 1 year ago

This also breaks all the workflows that use actions/setup-node where workspaces are involved and cache is enabled, like this one (source)

john-landgrave commented 1 year ago

Just ran into this on a fresh install on a new machine. npm config set <...> fails with the following:

npm ERR! code ENOWORKSPACES
npm ERR! This command does not support workspaces.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/johnl/.npm/_logs/2023-03-20T21_34_37_668Z-debug-0.log

It looks like a workaround is to go down to 0.9.2? Considering that npm workspaces is what our app uses as it's "monorepo framework" I don't suppose I have a whole lot of choice there. Is this intentional, or this is a bug of some sort?

FWIW, I did also try cding up into the particular package and running the commands directly from there (no -w involved in the command anywhere) and it still failed with the same error I presume because it can tell that it is still in a workspace.

kongmoumou commented 1 year ago

When will this bug be fixed?

vritant24 commented 1 year ago

+1 we're also facing failures in our pipeline after upgrading from node 16 -> 18 due to this issue

davidstenstroem commented 10 months ago

I also ran into this issue today. Using Node.js v18.17.0 and NPM v10.2.5 and v9.6.7

prodkt commented 10 months ago

Running into this issue across multiple environments in office and remote the last 48 hours.

darcyclarke commented 10 months ago

npm config is a bit of a special case since I'm fairly certain the npm team don't want to let users accidentally create local .npmrc files inside workspace directories (because they aren't supported/respected & could end up causing other issues for your project). Thus, failing within a workspace helps prevent that.

That said, if you really want to manage the config of your project from within a workspace, you can by passing --workspaces=false & --include-workspace-root (-ws=false & -iwr are shorthands for these options).

Minimum working reproduction:

npm init -y -iwr -w=a && cd ./a && npm config get registry -ws=false -iwr

And in the case of the original issue:

npm config list -ws=false -iwr # OR
npm config get registry -ws=false -iwr # will work...

If, for some reason, you are trying to create/manage nested .npmrc files in your workspaces, you'll have to get a bit tricky & use npm exec. Example:

npm exec -c 'npm config get registry -ws=false' -ws # OR whatever explicit workspace you want

With the above, you'll still want to include/exclude the root (ie. -iwr) if you're trying to manage both the workspace configs & the root's.

jfly commented 8 months ago

you can by passing --workspace=false & --include-workspace-root (-ws=false & -iwr are shorthands for these options).

Since this tripped me up: it's --workspaces=false (plural).

edit: Just so there's a simple answer that people can actually copy paste, the following commands work for me inside of a npm workspace. Do read @darcyclarke's original post for a more nuanced answer, though.

$ npm config get --workspaces=false --include-workspace-root
$ npm config get -ws=false -iwr
ThePlenkov commented 6 months ago

Seriously? Is it a problem to read the config from workspace? Why is not still fixed?

davidcoleman007 commented 1 week ago

That said, if you really want to manage the config of your project from within a workspace, you can by passing --workspaces=false & --include-workspace-root (-ws=false & -iwr are shorthands for these options).

@darcyclarke we have a scenario where we have a common ci script that uses npm config and this ci job handles many repos. Until today none of them were a workspace project, so there was no issue.

is there some way at the cli i can ask npm if a project IS a workspace project in a bash script so i can use your approach only IF it is one? I've done some light google-ing and cant figure it out.