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.
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With