npx (npm exec)을 돌리되 커맨드라인에서 prefix, .npm 경로, registry, strict-ssl를 커맨드로 바꿀 수 있어?

가능해. npx(npm v7+에서는 npm exec와 동일)는 거의 모든 npm 설정 값을 “그 실행 한 번”에 한해 CLI 옵션이나 환경변수로 오버라이드할 수 있어. 핵심은 다음 셋 중 하나를 쓰는 것: 1) CLI 플래그, 2) npm_config_* 환경변수, 3) 다른 .npmrc를 지정하는 --userconfig.

아래를 차근차근 보자.

  1. CLI 플래그로 바로 지정
  • registry: --registry=https://registry.npmmirror.com

  • strict-ssl: --strict-ssl=false

  • prefix(글로벌 prefix): --prefix=/opt/npm-global

  • .npm 캐시 경로: --cache=/var/tmp/npm-cache (일반적으로 “~/.npm”이 이 값)

예시들:

# npm v7+ 스타일 (권장): npm exec
npm exec --registry=https://registry.npmmirror.com --strict-ssl=false -- \
  some-binary --help

# 전통적인 npx 스타일 (npm v6~): npx
npx --registry=https://registry.npmmirror.com --strict-ssl=false some-binary --help

# prefix와 cache도 한 번에
npm exec --prefix=/opt/npm-global --cache=/var/tmp/npm-cache -- \
  eslint -v
  1. 환경변수로 지정 (npm_config_*)
    동일한 키를 환경변수로 넘겨도 된다. 한 번 쓰고 끝낼 때 깔끔하다.
# POSIX 쉘
npm_config_registry=https://registry.npmmirror.com \
npm_config_strict_ssl=false \
npm_config_prefix=/opt/npm-global \
npm_config_cache=/var/tmp/npm-cache \
npx eslint -v

Windows PowerShell이라면:

$env:npm_config_registry="https://registry.npmmirror.com"
$env:npm_config_strict_ssl="false"
$env:npm_config_prefix="C:\npm-global"
$env:npm_config_cache="D:\npm-cache"
npx eslint -v
  1. 별도 .npmrc를 지정
    프로젝트/유저 기본 설정 건드리지 않고 특정 실행에만 쓰고 싶을 때 유용하다.
# /tmp/my.npmrc 안에 원하는 설정을 써둔 뒤
npm exec --userconfig=/tmp/my.npmrc -- webpack --version

실무 팁과 주의점

  • npm exec에서 “설정 플래그들은 실행할 바이너리 이름 앞”에 두는 게 안전하다. 그 뒤의 인자는 실행 대상 패키지가 받는다. 애매하면 --로 구분해주기.
    예: npm exec --registry=... -- strict-ssl-checker --verbose

  • prefix는 “글로벌 설치 위치”를 의미한다. npm exec --package=foo 같이 일시 설치 실행할 때는 임시 디렉터리를 쓰므로 prefix보다 cachetmp 관련 설정이 동작에 더 큰 영향을 준다.

  • npm v6의 npx는 옵션 파싱이 다소 까다로웠다. 패키지 인자와 npx 옵션이 섞이면 --로 경계 표시.

  • CA 검증을 끄는 --strict-ssl=false는 편하지만 보안상 위험하니 내부 네트워크 테스트 등 불가피할 때만 쓰자.

  • 자주 쓰는 값은 사용자 홈의 ~/.npmrc나 프로젝트의 .npmrc에 적어 두고, 일시적으로 바꿀 때만 위 방식을 쓰면 깔끔하다.

여기까지면 보통은 충분하다. 필요하면 proxy, https-proxy, cafile, always-auth 같은 다른 npm 설정들도 똑같은 방식으로 한 번 실행용으로 덧입힐 수 있다.

모두 한 번에 설정하는 법

가능. 한 줄에서 전부 덮어씌우는 가장 간단한 방법 3가지를 바로 쓸 수 있어. 각각 “실행 한 번”에만 적용돼서 기본 설정은 안 바뀐다.

  1. CLI 플래그 한 번에(권장: npm v7+에서 npm exec)
npm exec \
  --registry=https://registry.npmmirror.com \
  --strict-ssl=false \
  --prefix=/opt/npm-global \
  --cache=/var/tmp/npm-cache \
  --package=eslint -- \
  eslint -v

포인트: 설정 플래그는 실행할 패키지 이름(여기선 eslint) 앞에 두고, 그 뒤를 --로 구분해 실행 대상의 인자를 전달한다.

  1. 환경변수 한 번에 (npm_config_*)
# POSIX 쉘
npm_config_registry=https://registry.npmmirror.com \
npm_config_strict_ssl=false \
npm_config_prefix=/opt/npm-global \
npm_config_cache=/var/tmp/npm-cache \
npm exec --package=eslint -- eslint -v

PowerShell이라면:

$env:npm_config_registry="https://registry.npmmirror.com"
$env:npm_config_strict_ssl="false"
$env:npm_config_prefix="C:\npm-global"
$env:npm_config_cache="D:\npm-cache"
npm exec --package=eslint -- eslint -v
  1. 임시 .npmrc 파일로 한 번에
cat > /tmp/once.npmrc <<'EOF'
registry=https://registry.npmmirror.com
strict-ssl=false
prefix=/opt/npm-global
cache=/var/tmp/npm-cache
EOF

npm exec --userconfig=/tmp/once.npmrc --package=eslint -- eslint -v

npx를 그대로 쓰는 경우(npm v6 계열)도 동일하게 된다:

npx --registry=https://registry.npmmirror.com --strict-ssl=false \
    --prefix=/opt/npm-global --cache=/var/tmp/npm-cache \
    eslint -v

정리해둔 작동 원칙

  • 우선순위: CLI 플래그 > 환경변수(npm_config_*) > --userconfig로 지정한 .npmrc > 프로젝트/유저 .npmrc

  • prefix는 글로벌 설치 경로를 의미한다. npm exec --package=...처럼 일시 설치 실행에서는 임시 디렉터리를 쓰므로 실제 체감 영향은 cache, tmp가 더 크다.

  • 패키지 이름과 옵션이 섞일 위험이 있으면 --로 경계를 나눠라.

  • --strict-ssl=false는 편하지만 보안상 위험하니 내부 네트워크 테스트 등 꼭 필요할 때만 사용하라.

여기 예시의 registry, 경로들은 네 환경에 맞게 바꿔 꽂으면 된다. 다음 단계로는 proxy, cafile, always-auth 같은 값도 같은 방식으로 함께 묶어 넘겨볼 수 있다.

뭔가 길다? 내가 원래 치려던 건 npx -y @modelcontextprotocol/server-everything이야

좋아, 그 한 줄짜리로 끝내자. 네가 치려던 명령에 옵션 4종을 “한 번 실행용”으로 덮어씌우는 최소 예시는 아래 둘 중 하나로 된다.

CLI 플래그로 한 방에:

npx --registry=https://registry.npmmirror.com --strict-ssl=false \
    --prefix=/opt/npm-global --cache=/var/tmp/npm-cache \
    -y @modelcontextprotocol/server-everything

환경변수로 깔끔하게(추천, 쉘 이식성 좋음):

npm_config_registry=https://registry.npmmirror.com \
npm_config_strict_ssl=false \
npm_config_prefix=/opt/npm-global \
npm_config_cache=/var/tmp/npm-cache \
npx -y @modelcontextprotocol/server-everything

참고 메모

  • -- 구분자는 패키지에 추가 인자를 넘길 때만 붙이면 된다. 여기선 불필요.

  • prefix는 전역 설치 경로지만, npx는 일시 설치·실행이라 실제로는 cache와 네트워크 설정(registry, strict-ssl)이 더 영향이 크다.

  • strict-ssl=false는 위험하니 내부망 테스트처럼 꼭 필요할 때만 쓰자.

같은 방식으로 proxy, https-proxy, cafile, always-auth도 한 줄에 얹을 수 있다.

유저 파일로 저장해두고 돌리기

좋아. “매번 옵션 치기 귀찮다 → 사용자 설정 파일(~/.npmrc)에 저장해두고 그냥 실행”으로 가자. 아래 순서대로 하면 된다.

  1. 기존 설정 백업
# macOS/Linux
[ -f ~/.npmrc ] && cp -a ~/.npmrc ~/.npmrc.bak.$(date +%Y%m%d-%H%M%S)

# Windows PowerShell
if (Test-Path $HOME\.npmrc) { Copy-Item $HOME\.npmrc "$HOME\.npmrc.bak.$((Get-Date).ToString('yyyyMMdd-HHmmss'))" }
  1. 사용자 .npmrc 작성
    원하는 값으로 바꿔 넣자. strict-ssl=false는 보안상 위험하니 정말 필요한 경우에만 쓴다.
# macOS/Linux
cat > ~/.npmrc <<'EOF'
registry=https://registry.npmmirror.com
strict-ssl=false
prefix=/opt/npm-global
cache=/var/tmp/npm-cache
EOF

PowerShell이라면:

@"
registry=https://registry.npmmirror.com
strict-ssl=false
prefix=C:\npm-global
cache=D:\npm-cache
"@ | Set-Content -Encoding UTF8 $HOME\.npmrc
  1. prefix 경로 만들기 및 PATH에 추가
    prefix를 바꿨다면 그 위치의 bin이 PATH에 있어야 실행 파일이 보인다.
# macOS/Linux
mkdir -p /opt/npm-global
# 셸에 PATH 추가(예: bash/zsh)
echo 'export PATH="/opt/npm-global/bin:$PATH"' >> ~/.bashrc
echo 'export PATH="/opt/npm-global/bin:$PATH"' >> ~/.zshrc
# 적용
source ~/.bashrc 2>/dev/null || source ~/.zshrc 2>/dev/null || true

PowerShell:

New-Item -ItemType Directory -Force C:\npm-global | Out-Null
$env:Path = "C:\npm-global;$env:Path"
[Environment]::SetEnvironmentVariable("Path", "C:\npm-global;" + [Environment]::GetEnvironmentVariable("Path","User"), "User")
  1. 캐시 디렉터리 준비(선택)
# macOS/Linux
mkdir -p /var/tmp/npm-cache

# PowerShell
New-Item -ItemType Directory -Force D:\npm-cache | Out-Null
  1. 잘 저장됐는지 확인
npm config list -l | grep -E 'registry|strict-ssl|prefix|cache'
# PowerShell
npm config list -l | findstr /r "registry strict-ssl prefix cache"
  1. 이제 그냥 실행
    이제부터는 옵션 없이 네가 쓰려던 명령을 그대로 쓰면 된다.
npx -y @modelcontextprotocol/server-everything

실무 메모

  • 사용자 수준(~/.npmrc)은 현재 사용자에게만 적용된다. 프로젝트 전용 설정을 원하면 프로젝트 루트에 .npmrc를 두면 해당 디렉터리에서만 우선 적용된다.

  • 빠른 대안으로 CLI에서 영구 저장도 가능하다:
    macOS/Linux:

    npm config set registry https://registry.npmmirror.com
    npm config set strict-ssl false
    npm config set prefix /opt/npm-global
    npm config set cache /var/tmp/npm-cache
    

PowerShell: powershell npm config set registry https://registry.npmmirror.com npm config set strict-ssl false npm config set prefix C:\npm-global npm config set cache D:\npm-cache

이 방식은 같은 내용이 ~/.npmrc에 기록된다.

  • strict-ssl=false는 중간자 공격에 취약해진다. 보안이 필요한 환경에선 내부 CA를 쓰거나 cafile 설정을 권장한다.

  • 설정을 되돌리려면 백업한 ~/.npmrc.bak.* 파일을 다시 덮어쓰면 된다.

Categories:

Updated: