当前位置:网站首页>1-4 configuration executable script of nodejs installation

1-4 configuration executable script of nodejs installation

2022-04-23 16:58:00 Endless cake

NodeJS Configure executable script

First , Get to know , The difference between local module installation and global module installation .

overall situation :
1. All projects can use this module
Inferiority :(1) It is easy to cause version conflicts
(2) Cannot be based on commonJS The module specification , Call use
Just can't js Pass through require Call use

Local :
1. Only the current project can use this module
Inferiority :(1) You can't directly use commands to operate . For example, the command line uses
It is recommended to install local .

Speaking of this , You must be surprised , Why is it possible to execute commands directly when installed globally 》
First remember a command

npm root   ||  npm root -g   
 View the local or global environment ,npm Installation directory 

 Insert picture description here
This is my installation path . I installed it globally . And then according to npm The path to open the folder . You can see the following figure :
 Insert picture description here
You can see npm, There is a vue.cmd The file of , This is what I installed to test . In fact, there is this file , You can use... From the command line vue command . If you want to create a command , Then trigger... From the command line , Just in this directory , Create a cmd The file of , Just edit the content .
in other words , Modules installed in the global directory , Most of them will generate a .cmd The file of , As long as you have this file , Then the file name is an executable command . for example :vue.cmd that vue It's an order
As for how to create your own commands , Copy the following code :

@IF EXIST "%~dp0\node.exe" (
“%~dp0\node.exe” "%~dp0\node_modules\lol\index.js" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS=;%
  node  "%~dp0\node_modules\lol\index.js" %*
)

Create your own directory , Just play .

So I see . Only when our module is installed globally can we use the command line , Carry out orders , How to install locally , See the following example :

The following is the of the previous article package.json file , That is, our configuration list .
You can see it in here , There is one scripts An object parameter of , Executable script

{
  "name": "c_web",		// Module name 
  "version": "1.0.0",		// Version number 
  "description": "",		// Module description 
  "main": "index.js",		// Module main entry file 
  "scripts": {					// Executable script . Here is an example :
    "dev": "webpack --mode development",			// Packaging in the development environment 
    "build": "webpack --mode production"				// Packaging in a production environment 
  },
  
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {				// Production depends on 
    "jquery": "^3.4.1"
  },
    "devDependencies":{		// Development depends on 
	  
  }
}

Its implementation principle is , Install the module locally , If it supports command operation ( Will be in node_modules Of bin In the middle of xxx.cmd file ) But this file cannot be executed globally . Note here , there node_modules In the current directory .

“dev”: “webpack --mode development”
The attribute name of the command , Just define yourself , Property value is the command script to be executed .
You can see that I have configured two commands .

“scripts”: { // Executable script . Here is an example :
“dev”: “webpack --mode development”, // Packaging in the development environment
“build”: “webpack --mode production” // Packaging in a production environment
},
Through here scripts Execute the commands we define , for example :

npm run dev
npm run build

Such a command actually performs the following steps :
(1) First find scripts Search for
(2) Find and put the following corresponding value ( Execute the script ) perform
(3) When executing a script , Back home node_modules Of bin Search under file , If not , In the npm Search under the global directory of the installation .

版权声明
本文为[Endless cake]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230554520256.html