npm / npm/cli

[BUG] npx script does not appear to load dependencies

Open
#5,650 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug cmd:exec Needs Triage Release 8.x
Dominant language
JavaScript
Stars
10.1k
Forks
4.7k
Avg merge
2d 2h
Merged PRs (30d)
19

Description

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

I am attempting to write my first package to use for automated scripting, writing npx commands. I am attempting to use Google ZX for that scripting. After some initial testing I was finally able to run commands, when npm linking the package locally. But, after publishing my package, the commands no longer worked when running from other directories/terminal prompts. I have a very simple setup to begin with:

{
  "name": "pure-scripts",
  "version": "0.0.16",
  "description": "A repository of scripts that can be run using the npx command",
  "files": [
    "bin"
  ],
  "bin": {
    "pure-scripts": "chmod +x bin/index.mjs && bin/index.mjs"
  },
  "dependencies": {
    "zx": "7.0.8"
  }
}

My CI process publishes my package to a local private repository, with a bin/index.mjs file that looks a little like this:

#!/usr/bin/env zx
async function foo() {
  echo`This is foo!`;
}

async function hello() {
  $.verbose = false;
  let branch = await $`git branch --show-current`;
  $.verbose = true;

  echo`Hello`;
  echo`Current branch is ${branch}.`;
}

const commands = {
  foo,
  hello
};

const { _: [command] } = argv;

console.log('[pure-scripts] dirname: ', process.cwd());

if (commands[command]) {
  commands[command]();
}

As I said, this works fine locally. I build, run npm link, then run npx pure-scripts foo and get the expected output.

However, when I publish my package I do the following from another directory and new terminal

npm cache clean -f
// typical output
npx pure-scripts foo
// output: "env: zx: No such file or directory"

This appears to indicate that my defined dependencies, from my package.json, have not been installed for use by my scripts.
I should note that I never get asked to install the package either.

Expected Behavior

That, after publishing my package, that running the npx <packagename> <command-args> command would install all of the dependencies required for my package scripts to run properly.

Steps To Reproduce
  1. On any OS
  2. With the package.json defined similar to above, and a bin/index.mjs as above, published
  3. Run npx <packagename> <command-args> (in this case foo or hello)
  4. Gets error env: zx: No such file or directory
Environment
  • npm: 8.11.0
  • Node.js: 16.16.0
  • OS Name: MacOS Monterey
  • System Model Name: MacBook Pro
  • npm config:
; "user" config from /Users/<redacted>/.npmrc

//npm.pkg.github.com/:_authToken = (protected) 
//registry.npmjs.org/:_authToken = (protected) 
; email = "<redacted>" ; overridden by project
; registry = "https://registry.npmjs.org/" ; overridden by project

; "project" config from /Users/<redacted>/Documents/<redacted>/packages/<redacted>/.npmrc

email = "<redacted>" 
registry = "<redacted>" 

; node bin location = /Users/<redacted>/.nvm/versions/node/v16.16.0/bin/node
; node version = v16.16.0
; npm local prefix = /Users/<redacted>/Documents/<redacted>/packages/<redacted>
; npm version = 8.11.0
; cwd = /Users/<redacted>/Documents/<redacted>/packages/<redacted>
; HOME = /Users/<redacted>
; Run `npm config ls -l` to show all defaults.

; "publishConfig" from /Users/<redacted>/Documents/<redacted>/packages/<redacted>/package.json
; This set of config values will be used at publish-time.

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 by reproducing the published-package case with npx pure-scripts foo, using the shown package.json and bin/index.mjs setup. Trace the npx execution path for package dependencies and add coverage for the reported behavior; done means the command can access its declared dependencies after publication.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.