znvlcm 2022. 5. 19. 09:52

이 글은 npm help exec 로 확인한 문서 내용을 해석한 내용입니다. 

사용법 

npm exec -- <pkg>[@<version>] [args...]
npm exec --package=<pkg>[@<version>] -- <cmd> [args...]
npm exec -c '<cmd> [args...]'
npm exec --package=foo -c '<cmd> [args...]'
npm exec [--ws] [-w <workspace-name] [args...]

npx <pkg>[@<specifier>] [args...]
npx -p <pkg>[@<specifier>] <cmd> [args...]
npx -c '<cmd> [args...]'
npx -p <pkg>[@<specifier>] -c '<cmd> [args...]'
Run without --call or positional args to open interactive subshell

alias: npm x, npx

common options:
--package=<pkg> (may be specified multiple times)
-p is a shorthand for --package only when using npx executable
-c <cmd> --call=<cmd> (may not be mixed with positional arguments)

내용 

원문

This command allows you to run an arbitrary command from an npm package (either one installed locally, or fetched remotely), in a similar context as running it via npm run.
Run without positional arguments or --call, this allows you to interactively run commands in the same sort of shell environment that package.json scripts are run. Interactive mode is not supported in CI environments when standard input is a TTY, to prevent hangs.
Whatever packages are specified by the --package option will be provided in the PATH of the executed command, along with any locally installed package executables. The --package option may be specified multiple times, to execute the supplied command in an environment where all specified packages are available. 
If any requested packages are not present in the local project dependencies, then they are installed to a folder in the npm cache, which is added to the PATH environment variable in the executed process. A prompt is printed (which can be suppressed by providing either --yes or --no).
Package names provided without a specifier will be matched with whatever version exists in the local project. Package names with a specifier will only be considered a match if they have the exact same name and version as the local dependency.
If no -c or --call option is provided, then the positional arguments are used to generate the command string. If no --package options are provided, then npm will attempt to determine the executable name from the package specifier provided as the first positional argument according to the following heuristic:
If the package has a single entry in its bin field in package.json, or if all entries are aliases of the same command, then that command will be used.If the package has multiple bin entries, and one of them matches the unscoped portion of the name field, then that command will be used.If this does not result in exactly one option (either because there are no bin entries, or none of them match the name of the package), then npm exec exits with an error.
To run a binary other than the named binary, specify one or more --package options, which will prevent npm from inferring the package from the first command argument.

이 명령어를 사용하면 npm run을 통해 실행하는 것과 유사한 컨텍스트에서 npm 패키지(로컬로 설치되거나 원격으로 가져온)에서 임의의 명령을 실행할 수 있습니다.
셸에 대한 지정없이 실행하거나 --call을 사용하면 package.json 스크립트가 실행되는 것과 동일한 종류의 셸 환경에서 명령을 대화식으로 실행할 수 있습니다. 표준 입력이 TTY인 경우 중단을 방지하기 위해 CI 환경에서는 대화형 모드가 지원되지 않습니다.
--package 옵션으로 지정된 모든 패키지는 로컬로 설치된 패키지 실행 파일과 함께 실행된 명령의 PATH에 제공됩니다. --package 옵션은 지정된 모든 패키지를 사용할 수 있는 환경에서 제공된 명령을 실행하기 위해 여러 번 지정할 수 있습니다.
이 명령어는 당신이 요청한 패키지가 로컬 프로젝트의 의존관계에 없으면 실행된 프로세스의 PATH 환경 변수에 추가되는 npm 캐시의 폴더에 설치됩니다. 프롬프트가 출력됩니다(--yes 또는 --no를 제공하여 표시하지 않을 수 있음).
지정자 없이 제공된 패키지 이름은 로컬 프로젝트에 존재하는 모든 버전과 일치합니다. 지정자가 있는 패키지 이름은 로컬 종속성과 정확히 동일한 이름 및 버전이 있는 경우에만 일치하는 것으로 간주됩니다.
-c 또는 --call 옵션이 제공되지 않으면 위치 인수가 명령 문자열을 생성하는 데 사용됩니다. --package 옵션이 제공되지 않으면 npm은 다음 발견적 방법에 따라 첫 번째 위치 인수로 제공된 패키지 지정자에서 실행 파일 이름을 결정하려고 시도합니다.
패키지에 package.json의 bin 필드에 단일 항목이 있거나 모든 항목이 동일한 명령어의 별칭인 경우 해당 명령어가 사용됩니다. 패키지에 여러 bin 항목이 있고 그 중 하나가 범위가 지정되지 않은 부분과 일치하는 경우 이름 필드의 경우 해당 명령어가 사용됩니다. 이렇게 해도 정확히 하나의 옵션이 생성되지 않으면(bin 항목이 없거나 패키지 이름과 일치하지 않기 때문에) npm exec가 오류와 함께 종료됩니다.
명명된 바이너리가 아닌 다른 바이너리를 실행하려면 npm이 첫 번째 명령 인수에서 패키지를 추론하지 못하도록 하는 하나 이상의 --package 옵션을 지정합니다.

728x90