npm / npm/cli

[BUG] Package variable replacement in package scripts fails with names containing hyphens

Open
#2,541 9 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Priority 2 Release 7.x
Dominant language
JavaScript
Stars
10.1k
Forks
4.7k
Avg merge
2d 2h
Merged PRs (30d)
19

Description

Current Behavior:

project.json vars do not work with hyphens within package.json scripts interpolation.

Expected Behavior:

I would expect variable with hyphens which are used from with package script to be properly accessible.

Steps To Reproduce:
  1. With this config:
    package.json
{
	"name": "my-package",
	"bin": {
		"my-package": "./bin/my-package.js"
	},
	"scripts": {
		"prepack": "$npm_package_bin_my-package"
	}
}
  1. Run npm run prepack.
  2. Results in the following error:
% npm run prepack

> my-package@1.0.0 prepack
> $npm_package_bin_my-package

sh: -package: command not found
npm ERR! code 127
npm ERR! path /.../my-package
npm ERR! command failed
npm ERR! command sh -c $npm_package_bin_my-package

npm ERR! A complete log of this run can be found in:
npm ERR!     /.../.npm/_logs/2021-01-25T19_43_07_416Z-debug.log
  1. The process.env actually passed to the script is as follows:
{
  TERM_PROGRAM: 'Apple_Terminal',
  NODE: '/usr/local/bin/node',
  INIT_CWD: '/.../my-package',
  SHELL: '/bin/zsh',
  TERM: 'xterm-256color',
  npm_config_metrics_registry: 'https://registry.npmjs.org/',
  TMPDIR: '/var/folders/pf/rmb77nvd2t99vjmq9z1xc6100000gn/T/',
  TERM_PROGRAM_VERSION: '440',
  COLOR: '1',
  TERM_SESSION_ID: '...',
  USER: '...',
  npm_config_globalconfig: '/usr/local/etc/npmrc',
  SSH_AUTH_SOCK: '/private/tmp/com.apple.launchd.JpMQQSNA9S/Listeners',
  __CF_USER_TEXT_ENCODING: '0x1F5:0x0:0x0',
  npm_execpath: '/usr/local/lib/node_modules/npm/bin/npm-cli.js',
  'npm_config_init.module': '/.../.npm-init.js',
  PATH: '...',
  npm_package_json: '/.../my-package/package.json',
  _: './bin/my-package.js',
  npm_config_userconfig: '/.../.npmrc',
  npm_config_init_module: '/.../.npm-init.js',
  __CFBundleIdentifier: 'com.apple.Terminal',
  npm_command: 'run-script',
  PWD: '/.../my-package',
  npm_lifecycle_event: 'prepack',
  EDITOR: 'vi',
  npm_package_name: 'my-package',
  LANG: 'en_US.UTF-8',
  XPC_FLAGS: '0x0',
  npm_config_node_gyp: '/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js',
  npm_package_version: '1.0.1',
  XPC_SERVICE_NAME: '0',
  SHLVL: '2',
  HOME: '/...',
  'npm_package_bin_my-package': 'bin/my-package.js',
  npm_config_cache: '/.../.npm',
  LOGNAME: '...',
  npm_lifecycle_script: './bin/my-package.js',
  PREFIX: '/usr/local',
  npm_config_user_agent: 'npm/7.4.0 node/v15.6.0 darwin x64',
  npm_node_execpath: '/usr/local/bin/node',
  npm_config_prefix: '/usr/local'
}
Environment:
  • OS: MacOS 11.1 (20C69)
  • Node: 15.6.0
  • npm: 7.4.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the package.json script and the npm run prepack entry point, then trace how the npm_package_bin_my-package environment variable is interpolated before the shell runs it. Add a regression test covering a bin name with a hyphen and verify that npm run prepack invokes the expected path instead of treating the suffix as a separate shell argument.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
cli, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.