Package the nodejs file into exe and set it to start automatically at boot (no black window)

nodejs packaged into exe

Use node’s pkg package

# 安装pkg
npm install -g pkg
# 使用pkg打包, 该命令会同时编译 linux, win, mac 版的exe
pkg server.js
# 只打包win版
pkg -t win server.js

If it prompts that pkg is not an internal command after installing pkg, reopen the cmd window and try again. If it still prompts, you need to configure environment variables
to view the configuration information of the .npmrc file (the default is under C:\Users{user}, if not, go to node (Search under npm in the installation directory)
Check the prefix configuration path in the configuration information, add it to the environment variable, and then restart the cmd window

Set boot autostart (1)

Create a bat file in the exe program directory

# nodejs exe路径
call E:\sdl\server.exe

Then create the vbs file

set ws=WScript.CreateObject("WScript.Shell")
# bat文件路径
ws.Run "E:\sdl\nodeStart.bat",0

Insert image description here
If you run the server.exe program directly, a black window will appear. In this way, no black window will appear.
Then move the vbs file to the C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp directory. The
StarUp directory is the startup directory and
you can restart the computer. Start automatically at boot

Set boot autostart (2)

The above method requires users to configure it themselves. For those who do not understand, it cannot solve the problem with one click.
Next, use the method of packaging the exe program into a windows service to design a one-click auto-start on boot.

First, download the tool package winsw
and download the winsw program link: https://github.com/winsw/winsw/releases .
Select the stable version WinSW v2.11.0. Direct link: https://github.com/winsw/winsw/releases /tag/v2.11.0
Insert image description here
software requires the .Net Framework software environment to run. Download it as needed. Generally, Windows has this environment. To view it, enter C:\Windows\Microsoft.NET\Framework on the address bar and press the Enter key. You can view it, missing download and installation.

Then download the sample-minimal.xml configuration file

Place these two files in the same directory as server.exe (the exe program created by node)
and modify the sample-minimal.xml configuration file.

<service>
  <!-- ID of the service. It should be unique across the Windows system-->
  # 安装windows服务后的服务ID,必须是唯一的。
  <id>nodeStart</id>
  <!-- Display name of the service -->
  # 服务名称,必须是唯一的。一般和id一致即可。
  <name>nodeStart</name>
  <!-- Service description -->
  # 服务描述,可做备注使用。
  <description>nodeStart</description>
  
  <!-- Path to the executable, which should be started -->
  # 执行的命令
  <executable>%BASE%\server.exe</executable>
  # 日志输出
  <logpath>%BASE%\serviceLogs</logpath>
</service>

Then create two bat files
install.bat

@echo off
start cmd /k "nodeStart.exe install"
exit

uninstall.bat

@echo off
start cmd /k "nodeStart.exe uninstall"
exit

The above exe program is the downloaded winsw package, I changed the package name

Then run install to set up autostart

Guess you like

Origin blog.csdn.net/weixin_44931584/article/details/128815589