【发布时间】:2019-03-24 22:23:19
【问题描述】:
我有一个 Node 应用程序(HTTP 服务器),它使用 express 和 EJS 来呈现模板。当我尝试使用 pkg (https://github.com/zeit/pkg) 构建可执行的独立文件时,它会引发一个错误,指出我尝试访问“localhost/”时未安装 ejs。但是原app运行正常,EJS安装为除了express之外的另一个模块。
文档指出在构建 .exe 时会考虑依赖关系。我可以看到它有效,因为使用 mysql 模块缓存数据库的请求正常发生。这似乎是 EJS 特有的问题,可能是因为它不是 'require()d' 而是在 express 选项中被选为模板引擎。
虽然很简单,但有人遇到过这样的事情吗?在使用 pkg 构建项目后,我用谷歌搜索但没有发现任何与 ejs 中的错误相关的内容。
堆栈跟踪:
Error: Cannot find module 'ejs'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:592:15)
at Function.Module._resolveFilename (pkg/prelude/bootstrap.js:1280:46)
at Function.Module._load (internal/modules/cjs/loader.js:518:25)
at Module.require (internal/modules/cjs/loader.js:648:17)
at Module.require (pkg/prelude/bootstrap.js:1159:31)
at require (internal/modules/cjs/helpers.js:20:18)
at new View (C:\snapshot\Eduardo\node_modules\express\lib\view.js:81:14)
at Function.render (C:\snapshot\Eduardo\node_modules\express\lib\application.js:570:12)
at ServerResponse.render (C:\snapshot\Eduardo\node_modules\express\lib\response.js:1008:7)
at C:\snapshot\Eduardo\Desktop\node eureka\app.js:0:0
【问题讨论】:
-
您找到解决方案了吗?我遇到了同样的问题。如果我能找到什么,会回来报告。
-
还没有。我忽略了 de pkg 创建并使用了完整的节点环境,但我必须从前端开发人员那里删除我想要保护的部分代码。
-
我已经解决了这个问题。在这一点上,老实说,我不记得最初是什么给了我这个错误。 (在我看来,这两天真的是一趟过去的旅行,大声笑,对不起)我最后所做的是将视图作为 express.static 文件包含在应用程序配置中。在 package.json 中,我指定我正在使用公共和我的视图文件夹中的任何内容。将在答案中发布,因为评论太长了。