The cause of

After publishing the Egg project to production this week, I found that the production environment could not execute NPM run start, as shown below

For reasons

How does the script command work?

Cross-env NODE_ENV=dev as a front-end engineer, I work with a variety of environments on a daily basis. I often run cross-env NODE_ENV=dev on the command line. One day, I find that cross-env NODE_ENV=dev is an error.

NPM I cross-env-g is installed globally. NPM I cross-env-g is installed globally

Well, it did not report an error, why? In fact, we can see from the installation feedback above that global installation creates a soft connection

This is not the case with project-dependent installation, so we cannot execute commands directly using abbreviations.

So the question is, do all the packages I need to execute on the command line need to be installed globally?

  1. The first way is to execute the file./node_modules/.bin/cross-env NODE=dev
  2. As a lazy person, of course, will not use method one, this timenpm scriptIt comes in handy

Add commands to script of package.json

"scripts": {
    "dev": "cross-env NODE=dev"
}
Copy the code

Run dev NPM run dev NPM run dev NPM run dev NPM run dev

However, I think there are still some people like me who don’t understand why NPM script can not be used to execute the above command error, will also create soft connection? NPM run will add all the files under node_modules/.bin to the environment variables of the system, so that we can use abbreviations during the execution, and then delete the environment variables automatically at the end of execution

I can’t find the module. I have a look at the code below.bin

Js file, but we did not find the file in the parent directory, so we reported an error. Of course this is impossible, after MY constant toss and toss, finally found!!

In fact, when installing NPM, we will first download the compressed package of the corresponding resource package and put it in the. NPM folder of the user directory, and then unpack it to the node_modules of the project, and extract the bin file specified in the dependency package. In Linux, we will create a soft connection. So what we really do on Linux is what the files in the.bin folder point to, as shown below.

The problem I encountered was that NPM install and NPM start were executed on two different machines. The files on the production machine were actually copied from the distribution machine, resulting in the loss of the soft connection, so the error was reported.

learn

Such a small problem can be exposed. Although we execute NPM script every day, we are very vague about its execution process, leading to problems but unable to find the cause. Finally, AFTER reading some information, I summarized the following points.

conclusion

  1. npm installIt will first look for packages that have been downloaded locally, no matter what version is found, it will not download, so if you want to upgrade dependencies, you can usenpm updateOr show installationnpm install cross-env --save
  2. npm installThe dependencies in the project will be downloaded first, and then the dependencies of the dependencies will be downloaded, which will result in the generated file is a tree structure, and there are many duplicate packages, so this timenpmThe dependency will be flattened, the dependency will be extracted to the first layer, encounter inconsistent version number will be retained, encounter completely consistent will be deleted.
  3. Finally, the dependencies are extractedbinFile,windowsOperating system generationcmdFile,linuxThe system generates a soft connection

Information Searched

  • In 2018, do you still only know NPM install?
  • Introduction to the NPM installation mechanism

Each small problem deep down will scalp pins and needles, learn not to move