This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Regular study notes, including ES6, Promise, Node.js, Webpack, HTTP Principles, Vue buckets, and possibly more Typescript, Vue3, and common interview questions.
Third-party modules
References to third-party modules also have no absolute path.
let r = require('co');
console.log(r);
Copy the code
There are two types of reference for third-party modules:
-
Global module, directly installed to the computer NPM directory.
-
By default, built-in third-party modules will look up the same folder in node_modules along the current directory, starting with the file set by main in package.json. If no, go to index.js to find it. If node_modules is not found in its own directory, it looks up. (You can find paths by printing Module.paths)
Let’s say we create a new node_modules folder in our current directory and create a folder named co that contains an index.js file. Module. Exports = “hello”.
When we print it again, we see that the result is hello.
The reason is because of third-party module lookup rules.
npm
NPM Docs/NPM
As a front-end engineer, you should be familiar with NPM. CNPM is not recommended at this time, the version cannot be locked, and many strange problems can occur. NRM or YARN is recommended.)
Here we introduce the 3N module.
npm
The full namenode package manager
Nodejs package manager, used for node plug-in management, including installation, uninstallation, dependency management, etc.nrm
The full namenode registry manager
, is the NPM mirror source management tool, sometimes foreign resources are too slow, use this can quickly switch between NPM sources.(example:nrm use taobao
、nrm use cnpm
, etc…).nvm
The full namenode version manager
Nodejs is a nodeJS version management tool that allows you to install and switch between different versions of Node and NPM.
Modules installed using NPM are global modules that cannot be referenced by require and can only be used on the command line.
We can access NPM or Node directly from the CMD console because of the configuration on the Path property of the environment variable.
The global module we installed through NPM will generate a shortcut in the NPM directory, so we can use the global module from the command line.
Configure command line commands
Suppose we now have a file that needs to be printed on the command line.
// ./bin/www
console.log('a')
Copy the code
How do we configure this?
Start by generating the default configuration file from the command line NPM init -y.
Add the attribute bin to the file and add the corresponding file path to bin. (We can also customize a command for easy operation)
After we have configured the configuration file, we need to place it in the global environment. There are two ways to do this:
- Publish to the global module environment, and publish.
- use
npm link
, link our current file tonode_moduleUnder the folder.(Currently recommended for easy debugging)
At this point we can see that the current custom command “gm” has been mapped to our current file directory.
// Console prints C:\Users\XXX\AppData\Roaming\ NPM \ GM -> C:\Users\XXX\AppData\Roaming\ NPM \node_modules\global-modules\bin\ WWW C:\Users\XXX\AppData\Roaming\npm\node_modules\global-modules -> D:\xxxx\xxxx\xxxx\xxx\xxx\global-modulesCopy the code
But we can’t use the “gm” command directly yet, we still need to configure a run mode.
At the beginning of the file we created earlier, type #! /usr/bin/env node
#! /usr/bin/env node
console.log('a')
Copy the code
This line of code tells the command line that the current file is being run using node in the current environment variable.
The runtime has changed and we need to re-run NPM link –force. (–force overwrites the previous configuration file)
This allows us to print the contents of our configuration file on the command line.
We can implement some custom global packages in this way, which can be configured in this way.
To sum up:
- Sir Into the default configuration file.
- configuration
bin
Command. - Adds the execution mode under the specified file
#! /usr/bin/env node
. - Put this package under NPM and you can publish it to a global environment or use it
npm link
Perform temporary debugging.
This article was created by Mo Xiaoshang. If there are any problems or omissions in the article, welcome your correction and communication. You can also follow my personal site, blog park and nuggets, AND I will upload the article to these platforms after it is produced. Thank you for your support!