npm / cli

the package manager for JavaScript

Home Page:https://docs.npmjs.com/cli/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] config prefix cannot be changed from project config in ~/.npmrc

Konfekt opened this issue · comments

commented

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

With a prefixsetting in ~/.npmrc pointing to a custom directory to install the user's global packages to with npm 10.5.0 on Ubuntu 22.04 and Opensuse 15.5, the error message introduced in #6685 shows up every time.

Expected Behavior

No error message as previously or instructions how to fix the prefix

Steps To Reproduce

Add a prefixsetting in ~/.npmrc pointing to a custom directory to install the user's global packages to.
Now, with npm 10.5.0 on Ubuntu 22.04 and Opensuse 15.5, this error message shows up every time.

Environment

  • npm: 10.05
  • Node.js: 20.13.1
  • OS Name: Ubuntu 20.04
  • System Model Name: Dell Latitude
  • npm config:
prefix=/home/konfekt/.local/share/npm-packages

@Konfekt value for prefix can not be changed from the project level .npmrc. if you want to change location of user's global packages, consider changing prefix value for user level .npmrc file. Checkout this documentation for more information on .npmrc https://docs.npmjs.com/cli/v10/configuring-npm/npmrc

commented

But does the linked documentation not refer to ~/.npmrc as per-user config file ?

yes it does. I am guessing that you might have ~/.npmrc already as per user config file. Do you also have .npmrc at your project level? Please share output of npm config ls from the project directory where you are getting this error.

commented

Please do not abbreviate when making a bug report. The actual output of the error message tells us exactly what file is trying to set prefix. The first line of npm config list also should show that error. It's impossible to triage this issue without that info.

commented

No, I only use npm to install executables, but do not use it for a project.

$ npm


npm ERR! config prefix cannot be changed from project config: /home/konfekt/.npmrc.
npm <command>

Usage:

npm install        install all the dependencies in your project
npm install <foo>  add the <foo> dependency to your project
npm test           run this project's tests
npm run <foo>      run the script named <foo>
npm <command> -h   quick help on <command>
npm -l             display usage info for all commands
npm help <term>    search for help on <term>
npm help npm       more involved overview

All commands:

    access, adduser, audit, bugs, cache, ci, completion,
    config, dedupe, deprecate, diff, dist-tag, docs, doctor,
    edit, exec, explain, explore, find-dupes, fund, get, help,
    help-search, hook, init, install, install-ci-test,
    install-test, link, ll, login, logout, ls, org, outdated,
    owner, pack, ping, pkg, prefix, profile, prune, publish,
    query, rebuild, repo, restart, root, run-script, sbom,
    search, set, shrinkwrap, star, stars, start, stop, team,
    test, token, uninstall, unpublish, unstar, update, version,
    view, whoami

Specify configs in the ini-formatted file:
    /home/konfekt/.config/.npmrc
or on the command line via: npm <command> --key=value

More configuration info: npm help config
Configuration fields: npm help 7 config

npm@10.5.0 /usr/lib64/node_modules/npm18
commented

Is there a package.json file in /home/konfekt?

commented

Hello! No, there is none. The home folder is rather limpid.

commented

This seems to originate from

return this.#loadFile(projectFile, 'project')

So resolve(this.localPrefix, '.npmrc') != this.#get('userconfig') would be satisfied.

commented

The localPrefix seems in

this.localPrefix = this.cwd
to be set to the current working directory, ~/, say, so that the project config and user config collide.

In other directories, different from ~/, npm no longer shows the warning introduced in #6685

Why this warning inside ~/ specifically ?

commented

what does npm config list show. In full. There is still not enough here to reproduce the error. Please note that the issue template asks for

; copy and paste output from npm config ls here

commented

Here we go.
Regarding the referred to code lines, I think this issue is to be expected, though.
Are you sure you cannot reproduce, setting prefix in ~/.npmrc?

$ npm config ls -l

; "default" config from default values

_auth = (protected) 
access = null 
all = false 
allow-same-version = false 
also = null 
audit = true 
audit-level = null 
auth-type = "web" 
before = null 
bin-links = true 
browser = null 
ca = null 
cache = "/home/konfekt/.npm" 
cache-max = null 
cache-min = 0 
cafile = null 
call = "" 
cert = null 
cidr = null 
color = true 
commit-hooks = true 
cpu = null 
depth = null 
description = true 
dev = false 
diff = [] 
diff-dst-prefix = "b/" 
diff-ignore-all-space = false 
diff-name-only = false 
diff-no-prefix = false 
diff-src-prefix = "a/" 
diff-text = false 
diff-unified = 3 
dry-run = false 
editor = "vim" 
engine-strict = false 
expect-result-count = null 
expect-results = null 
fetch-retries = 2 
fetch-retry-factor = 10 
fetch-retry-maxtimeout = 60000 
fetch-retry-mintimeout = 10000 
fetch-timeout = 300000 
force = false 
foreground-scripts = false 
format-package-lock = true 
fund = true 
git = "git" 
git-tag-version = true 
global = false 
global-style = false 
globalconfig = "/home/konfekt/.local/share/npm-packages/etc/npmrc" 
heading = "npm" 
https-proxy = null 
if-present = false 
ignore-scripts = false 
include = [] 
include-staged = false 
include-workspace-root = false 
init-author-email = "" 
init-author-name = "" 
init-author-url = "" 
init-license = "ISC" 
init-module = "/home/konfekt/.npm-init.js" 
init-version = "1.0.0" 
init.author.email = "" 
init.author.name = "" 
init.author.url = "" 
init.license = "ISC" 
init.module = "/home/konfekt/.npm-init.js" 
init.version = "1.0.0" 
install-links = false 
install-strategy = "hoisted" 
json = false 
key = null 
legacy-bundling = false 
legacy-peer-deps = false 
libc = null 
link = false 
local-address = null 
location = "user" 
lockfile-version = null 
loglevel = "notice" 
logs-dir = null 
logs-max = 10 
; long = false ; overridden by cli
maxsockets = 15 
message = "%s" 
node-options = null 
noproxy = [""] 
npm-version = "10.5.0" 
offline = false 
omit = [] 
omit-lockfile-registry-resolved = false 
only = null 
optional = null 
os = null 
otp = null 
pack-destination = "." 
package = [] 
package-lock = true 
package-lock-only = false 
parseable = false 
prefer-dedupe = false 
prefer-offline = false 
prefer-online = false 
; prefix = "/usr" ; overridden by project
preid = "" 
production = null 
progress = true 
provenance = false 
provenance-file = null 
proxy = null 
read-only = false 
rebuild-bundle = true 
registry = "https://registry.npmjs.org/" 
replace-registry-host = "npmjs" 
save = true 
save-bundle = false 
save-dev = false 
save-exact = false 
save-optional = false 
save-peer = false 
save-prefix = "^" 
save-prod = false 
sbom-format = null 
sbom-type = "library" 
scope = "" 
script-shell = null 
searchexclude = "" 
searchlimit = 20 
searchopts = "" 
searchstaleness = 900 
shell = "/bin/zsh" 
shrinkwrap = true 
sign-git-commit = false 
sign-git-tag = false 
strict-peer-deps = false 
strict-ssl = true 
tag = "latest" 
tag-version-prefix = "v" 
timing = false 
umask = 0 
unicode = true 
update-notifier = true 
usage = false 
user-agent = "npm/{npm-version} node/{node-version} {platform} {arch} workspaces/{workspaces} {ci}" 
; userconfig = "/home/konfekt/.npmrc" ; overridden by env
version = false 
versions = false 
viewer = "man" 
which = null 
workspace = [] 
workspaces = null 
workspaces-update = true 
yes = null 

; "user" config from /home/konfekt/.config/.npmrc

; prefix = "/home/konfekt/.local/share/npm-packages" ; overridden by project

; "project" config from /home/konfekt/.npmrc

prefix = "/home/konfekt/.local/share/npm-packages" 

; "env" config from environment

userconfig = "/home/konfekt/.config/.npmrc" 

; "cli" config from command line options

long = true

commented

Here's what's happening

  • You have set userconfig to /home/konfekt/.config/.npmrc using environment variables
  • You have reset the prefix from there to /home/konfekt/.local/share/npm-packages
  • Because the .npmrc file in /home/konfekt/.npmrc is no longer the user level config, when you run npm from that directory npm loads it up and considers it the project level config.

I think at this point you need to pare down your config chain, this is not a set up we can support.

commented

Understood, thank you very much for your time and clearing this up. I forgot about this environment variable