The Node_path in Nodejs

Source: Internet
Author: User

In the use of NODEJS development we have to install some third-party modules.

So you've come across some of the following problems

Before we go on reading, let's get a sense of the concept.

npm install --global xxxBelong to全局安装
npm install xxxBelong to本地安装

Too many modules installed, the module is not reusable

You write a project a need to install an express module, and develop a project B also need to install an express module

There are too many dependencies in the project, too much files, no reusable modules, and a variety of problems emerge

Because 安装的依赖包过多(而且依赖包嵌套依赖包) , if a project relies on more than one item (such as a dependency on a gulp series or a grunt series of project construction modules) then a project can be said to be light 轻松松上百兆 . If you want to change a directory for the project, you find it is wishful thinking. (基本都是1-10KB的小文本文件组成了一个100多MB的项目,那得有多少个文件啊!想想如果像java那样,模块都是以jar包存在的压缩归档文件可能也好一点). Not to mention deploying it.

In fact, this code is the code 几十KB到1兆 we write ourselves. These files want to achieve replication, mobile, deployment is very important.

The problem of deployment

If you deploy a node project to a remote server, uploading the Node_modules directory will be a scary thing.

Node_path emergence, module reuse, best practice scenarios

NODE_PATHWhat does it do?
There will be an environment variable in the operating system, and PATH presumably everyone knows that when the system calls a command, it will look in the path that is registered in the path variable, and if there is a call in the registered path, the prompt command is not found.

export PATH=$PATH: # 将 /usr/bin 追加到 PATH 变量中-> export NODE_PATH="/usr/lib/node_modules;/usr/local/lib/node_modules" #指定 NODE_PATH 变量

That NODE_PATH is NODE used in 寻找模块所提供的路径注册环境变量 . We can use the method above to specify the NODE_PATH environment variable. and ; split multiple different directories.

Loading time

About node's package loading mechanism I'm not here to talk nonsense. The path in the Node_path is traversed to occur in the
The directory is recursively searched from the root of the project node_modules until the root of the file system is found, and node_modules will go if the specified module has not been located NODE_PATH中注册的路径中查找 .

Solve the problem

The Nodejs-based package load path search algorithm allows us to install our packages to the global installation in a global manner.
In this way, our project can share the dependency packages in the global.

Understanding the Global

npm root -gView the global path in your system.
We can also pass
npm config ls -l | grep prefix(*nix) system
Or
npm config get prefix
To view the global path.
Yes, the prefix field is the globalbase path

How to set the global path?

# in *nixnpm config set prefix /path/to/global# in windowsnpm config set prefix C:\\Users\\pc\\global
Solutions to differences in module version problems

What if Project a uses the 3.x version of Express and Project B uses the 4.x version of Express?

You can store the 4.x version of Express in the location specified by Node_path, and then node_modules place the 3.x version in the directory of Project B.

This solves the problem of module version differences.

所以说,两种安装方式我们并不是只是用其中的一种,他们可以结合使用,根据 nodejs 的包加载机制,我们可以灵活使用。

Deployment is no longer a problem

Before we deploy, we can install all of our project's globally installed modules to the server in a global way. Next we can easily upload our project to the server. The upload will also become very fast.

Then configure our NODE_PATH environment variables. How to configure the above also talked about, there is no need to say more. (Because the project is deployed in a variety of ways, it can be decided in its own case.) )

I am using PM2 deployment to manage the Nodejs project, so I wrote it in the config file.

Problems brought about by

Yes, there are drawbacks to this approach. Because the parameter --global --save is used or the --save-dev argument is invalid.
This poses a problem. package.jsonin this dependencies case, you will not be devDependencies able to enjoy the convenience of NPM Automatic Updates, not to use dependencies , the devDependencies field is not acceptable to our project management.

How to Solve

I have a not very elegant solution, but also to solve the problem, I hope there is a better solution to the students give me a message.
I wrote a gadget ( npmafter ), it's very simple to use, it's cross-platform. Compatible (mac,windows,linux). (I didn't post it on GitHub because it feels like there's a better way)

$ npm install -g http://yinchangsheng-blog.qiniudn.com/blog/nodejs/npmafter.tgz # install# 然后我们安装任何模块就可以这样$ npm install express -g | npmafter$ npm install request q -g | npmafter --save$ npm install mocha chai -g | npmafter --save-dev

Yes, the package.json file will be updated.
Well, if you don't struggle with the problem then the problem is solved.

Use a NODE_PATH good solution to the problem of project development deployment.

The Node_path in Nodejs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.