Home > Web Front-end > JS Tutorial > body text

Detailed explanation on how to specify the version to run the project under multiple node versions?

青灯夜游
Release: 2021-04-06 10:59:42
forward
3061 people have browsed it

This article will introduce to you how to run projects under multiple nodejs by specifying nodejs versions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed explanation on how to specify the version to run the project under multiple node versions?

As the title says, there are multiple nodejs projects on our service and they need to be deployed. Each nodejs requires a different version (one is 12.8.0, One 6.3.0, one 8.11.1)

If you look at traditional methods, there are nvs, nvm, pm2, etc. Although the tools are good, I always find them troublesome. Learning tools is tiring and takes time.

Also, the framework I currently choose is eggjs. Egg official has done a good job in optimizing multi-process and other performance for you (this is the solution adopted by Alibaba during Double Eleven). I don’t I need to use other tools to help me optimize performance. I don't need it for such a small site.

Related recommendations: "nodejs Tutorial"

Solution process

I keep searching Baidu, but I can only use Baidu See Ali's solution. No more (frustrated)

Then I thought, first of all, nodejs must run js files under the node environment, and the node command needs to be executed. If the computer has installed nodejs, then you can actually use the npm command directly. In the installed nodejs environment (assumed to be 6.0.0), your project needs to be 10.0.0 or above to run.

Then first, I know a nodejs official website package, and the official website version is updated simultaneously. It should also be maintained by the official website.

Package name: node https://www.npmjs.com/package /node

After installation, you can run js through npx node@4 your js file

, but I tried for a long time and could not pass this command. To execute the package file under node_modules (the principle of the command is actually the node command. The difference is that you install it as the current running environment and will not change due to changes in the outer layer)

Later it was Baidu. and try. Still can't find a solution.

But I thought of one thing, webstrom can help you start the project.

Did you see that when webstrom starts nodejs, it itself specifies the nodejs version. That is to say, the startup method of webstrom is the startup method we need.

Then I thought, what is the command to start webstrom. (I was stupid for a long time) In fact, after starting it, I saw the

screenshot:

Looking at the screenshot, the above part is how webstrom starts.

But it is useless if you directly use this command to start it under cmd, and it is useless to switch the drive letter.

Final solution

First: You need to switch to the project directory under cmd

As shown (the same directory as package.json Below):

Second: Find the node package you installed

Third: Analyze the execution command just now

F:\ItemSet\egg-project\dream-backend\node_modules\node\bin\node.exe F:\nodejs\node_modules\npm\bin\npm-cli.js run start --scripts- prepend-node-path=auto

Obtained through analysis

The first part is your package name path all the way to node.exe

The second part is installed by your npm npm-cli.js file location

The third part is run, run the command

The fourth part: start The startup script command defined above package.json

As shown in the figure :

Part 5: --scripts-prepend-node-path=auto (I don’t know what it does. Please enlighten me)

Then directly copy

and run:

Look at the screenshot (it has been successfully run)

So this is the final solution.

Use the most original node running method to get the environment where a project specifies nodejs running. In this way, as long as you have any nodejs environment on your computer, you can run it.

Note that npm must be installed with nodejs, so it can be any version of nodejs

There is a problem here, because the node file installed by the package is node.exe. To be honest, I don’t know whether it works on Linux, but the node package should be an official package. Hope it is also compatible with linux. Then it is cross-platform. If not, you can use Alibaba’s nodeinstall module to achieve the same effect.

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of Detailed explanation on how to specify the version to run the project under multiple node versions?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template