1

我有一个节点项目,当尝试使用 pkg 模块打包它时,它使用模块 native-node-printer 和 edge-js 模块(适用于 Windows 平台)它给出了这个错误:

警告无法解析 ' ../../${ process.env.NNP_PACKAGE }' C:\VisualSutdioCode\ticketing-2019\pkg\print-scan-server\node_modules\node-native-printer\src\windows_printer.js 动态要求可能在运行时失败,因为请求的文件在编译时间并且不包含在可执行文件中。使用字符串文字作为 'require' 的参数,或保持原样并在 'scripts' 选项中指定解析的文件名。

edge-js 在本机节点打印机中被引用。

任何人都可以帮助我吗?

此致,

4

1 回答 1

0

编辑“node_modules\node-native-printer\src\windows_printer.js”如下

// const edge = require(`../../${process.env.NNP_PACKAGE}`); 
const edge = require(`edge-js`);

// const dllPath = fs.realpathSync(__dirname + '/../lib/windows/windows_printer.dll').replace('.asar', '.asar.unpacked');
const dllPath = fs.realpathSync(process.cwd() + '/lib/windows/windows_printer.dll').replace('.asar', '.asar.unpacked');

使用 pkg 打包应用程序后,将 node_modules/node-native-printer/lib 文件夹复制到您的应用程序根目录。您的目录结构应如下所示

  • 应用程序
  • 库/

如果您不更新 dllPath,您可能会收到类似于下面的错误

Error: Could not load file or assembly 'file:///E:\snapshot\print-server\node_modules\node-native-printer\lib\windows\windows_printer.dll' or one of its dependencies. The system cannot find the file specified.
    at Object.func (E:\snapshot\print-server\node_modules\edge-js\lib\edge.js:182:17)
    at WinPrinter.listPrinters (E:\snapshot\print-server\node_modules\node-native-printer\src\windows_printer.js:24:19)
    at E:\snapshot\print-server\app.js:9:13
    at Layer.handle [as handle_request] (E:\snapshot\print-server\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\snapshot\print-server\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (E:\snapshot\print-server\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (E:\snapshot\print-server\node_modules\express\lib\router\layer.js:95:5)
    at E:\snapshot\print-server\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (E:\snapshot\print-server\node_modules\express\lib\router\index.js:335:12)
    at next (E:\snapshot\print-server\node_modules\express\lib\router\index.js:275:10)
^Cnpm ERR! code ELIFECYCLE
npm ERR! errno 3221225786
npm ERR! print-server@1.0.0 start: `node app.js`
npm ERR! Exit status 3221225786
npm ERR!
npm ERR! Failed at the print-server@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

希望这可以帮助!

于 2020-01-28T10:18:38.907 回答