Pkg打包nodejs程序过程中遇到的问题与处理

准备打包

在package.json文件中添加关于pkg打包文件的一些文件夹包含(实际按照项目而定):

1
2
3
4
5
6
7
  "pkg":{

"assets":["api/**/*","runtime/**/*"]

  }

"bin":"main.js"

打包命令为:

1
pkg . -t node6-win-x86 -o myapp

问题处理:

1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
D:\FUXA\FUXA-master\server>pkg . -t node6-win-x86 -o myapp

> [email protected]

> Fetching base Node.js binaries to PKG_CACHE_PATH

  fetched-v6.17.1-win-x86      [                    ] 0%

> Error! 404 Not Found

  https://github.com/zeit/pkg-fetch/releases/download/v2.6/uploaded-v2.6-node-v6.17.1-win-x86

> Asset not found by direct link:

  {"tag":"v2.6","name":"uploaded-v2.6-node-v6.17.1-win-x86"}

> Not found in GitHub releases:

  {"tag":"v2.6","name":"uploaded-v2.6-node-v6.17.1-win-x86"}

> Building base binary from source:

  built-v6.17.1-win-x86

> Cloning Node.js repository from GitHub...

  git                          [                    ] 0%

> Error! Error: spawn git ENOENT

    at Process.ChildProcess._handle.onexit (internal/child_process.js:230:19)

    at onErrorNT (internal/child_process.js:407:16)

at process._tickCallback (internal/process/next_tick.js:63:19)

处理方案:

狸猫换太子之法处理。

找到下载失败的缓存路径,默认为C:\Users\Administrator\.pkg-cache\v2.X

需要手动下载一个二进制文件:

网络路径为https://github.com/vercel/pkg-fetch/releases

这里找一个和node版本相近的如个人使用的为nodeV10.8就下载了node-10.17-win-X86的二进制包,然后将源路径的失败的缓存文件删除,替换为报错的文件名称,本例子为fetched-v6.17.1-win-x86。

2.

> [email protected]

> Warning Cannot resolve 'path.join(__dirname, 'package.json')'

D:\FUXA\FUXA-master\server\fuxa.js

Dynamic require may fail at run time, because the requested file

is unknown at compilation time and not included into executable.

Use a string literal as an argument for 'require', or leave it

as is and specify the resolved file name in 'scripts' option.

处理方案:

将固定的一些js文件写入到package.json中:

"pkg":{

"assets":["./_appdata/*.js","./*.js","./api/*.js","./api/**/*.js","./runtime/**/*.js","./runtime/**/**/*.js","./runtime/*.js","./node_modules/**/*.js"]

}

在当地目录已经成功打包出一个myapp.exe的文件。

运行出错:

3.需要到D:/snapshot中获取配置文件。__dirname为该路径。

处理方案:

这里可能需要将__dirname改为本地路径”./”???

4.反馈 Get / 404

处理方案:

这里也是一个原因,去找静态的html文件的时候没有在本地寻找,打印了一下又去D:/snapshot中寻找,改为了”./”之后可以正常使用。