Home > Back-end >  Why does package.json script behave differently than identical terminal command
Why does package.json script behave differently than identical terminal command

Time:07-06

In my npm project, in my package.json file, I have the following lines of code:

  "scripts": {
    "build": "webpack"
  },

While in my terminal, if I run npm webpack, I get the error message:

Unknown command: "webpack"

But if I run npm run build, I get a prompt from webpack saying I need webpack-cli... so the command is obviously recognized.

I'm confused about the different behavior of these two commands. In this case, isn't running npm run build identical to running npm webpack in my terminal? Why does one command fail and one succeed? What is actually happening when I run npm run build?

CodePudding user response:

If we look at the documentation,

Environment

Package scripts run in an environment where many pieces of information are made available regarding the setup of npm and the current state of the process.

path

If you depend on modules that define executable scripts, like test suites, then those executables will be added to the PATH for executing the scripts.

Maybe this is the reason webpack is not recognized by the command line.

  • Related