Home > Net >  Where are npm scripts actually run from?
Where are npm scripts actually run from?

Time:11-28

If I run

npm run client

in my_directory, it will translate to:

webpack -w --mode production --config ./webpack.config.js

However, if I am in my_directory and I manually type in the above command, I get:

zsh: command not found: webpack

Also, I can I change things so that I can run from my_directory. This is a two part question if possible.

I was expecting to be able to run webpack manually without an npm script.

CodePudding user response:

webpack is installed in node_modules/.bin/, so you can do

node_modules/.bin/webpack -w --mode production --config ./webpack.config.js

CodePudding user response:

if you visit node_modules/.bin directory, you will see all the executables in your project are stored here. Those binaries are symlinked to the package's directory inside node_modules. When you run npm run, local node_modules/.bin directory is added to the PATH. When you execute the script, it actually executes from local node_modules/.bin.

If you want to manually write webpack to terminal you should install webpack globally, its executable directory will be added to PATH so that you can run it from terminal. Since you did not install webpack globally, it has no executable in your machine.

 sudo npm i -g webpack

this will install webpack in your actual machine, not in your project.

npm root -g 

this will show where your global modules installed.

But installing packages globally is not a good practice. let' 's say your app works with webpack5 now but in the future webpack6 is released and you upgraded webpack to 6 (or someone cloned your project and its system has webpack6) but your old project might break because of breaking changes in webpack6 version.

  • Related