Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between running a binary and running with npm a script from package.json?

The situation is as follows:

E.g. there is a package.json like:

{
  ...
  "scripts": {
    "build": "next build"
  },
  "dependencies": {
    "@types/node": "^14.0.26",
    "@types/react": "^16.9.43",
    "next": "9.4.4",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "typescript": "^3.9.7"
  }
}

Then running npm run build in the directory of the package.json executes the script assigned to the "build" key next build.

But directly running next build results in bash: next: command not found.

I did npm install so that all the dependencies defined in package.json get installed, though in the local node_modules-directory, which lies next to the package.json, where I can find the next-binary under node_modules/next/dist/bin/.

Using that like node_modules/next/dist/bin/next build works fine. So my assumption is that the command to install the dependencies does not encompass adding the next-binary to the path in the current environment to use it separately without the defined scripts in the package.json.

like image 305
quizmaster987 Avatar asked Dec 31 '25 11:12

quizmaster987


1 Answers

NPM utility extends PATH non-trivially - to include ./node_modules/.bin and additionally honors NPM workspaces support.

Plain Node.js knows nothing about ./node_modules/.bin nor about NPM workspaces.

You could use NPM runtime context with npm exec $CMD syntax or npx $CMD if you omit versions - it will use local versions:

npm exec next build                 # use project version
npx next build
./node_modules/.bin/next build

npm exec next@latest build          # use latest version
npm exec [email protected] -- next build  # use specific version
npm exec [email protected] -- next build
npx [email protected] next build
npm exec --ws -- eslint ./*.js      # execute in each workspace!!
like image 104
gavenkoa Avatar answered Jan 03 '26 10:01

gavenkoa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!