Javascript 使用heroku rails应用程序中node.js包的最简单方法
你好railites和jsheads 我是一个喜欢ruby的人,对节点这件事很陌生,但我正在学习,到目前为止我真的很喜欢它。我已经在heroku上开发了一个rails应用程序一周左右了,当然,需求也在不断变化 我需要能够从我的rails应用程序中使用一个用于创建PDF的节点库 最终,我追求的是实现这一目标的最简单方法。这就是我迄今为止所尝试的: 从heroku文档中,我知道heroku rails应用程序附带了一个node js运行时,但似乎没有包括npm,我也找不到一个明显的方法来要求pdfkit 因此,我遵循这方面的指导原则,使用DDOLAL的多构建包:Javascript 使用heroku rails应用程序中node.js包的最简单方法,javascript,ruby-on-rails,ruby,node.js,heroku,Javascript,Ruby On Rails,Ruby,Node.js,Heroku,你好railites和jsheads 我是一个喜欢ruby的人,对节点这件事很陌生,但我正在学习,到目前为止我真的很喜欢它。我已经在heroku上开发了一个rails应用程序一周左右了,当然,需求也在不断变化 我需要能够从我的rails应用程序中使用一个用于创建PDF的节点库 最终,我追求的是实现这一目标的最简单方法。这就是我迄今为止所尝试的: 从heroku文档中,我知道heroku rails应用程序附带了一个node js运行时,但似乎没有包括npm,我也找不到一个明显的方法来要求pdfk
heroku config:add BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git
以下是相关文件的片段(如果我遗漏了什么,请告诉我;)
.buildpack
https://github.com/heroku/heroku-buildpack-nodejs
https://github.com/heroku/heroku-buildpack-ruby
package.json
{
"name": "MoneyMaker",
"version": "0.0.1",
"dependencies": {
"pdfkit": "0.5.2"
},
"repository": {
"type" : "git",
"url" : "https://github.com/mattwalters/example.git"
}
}
档案:
...
gem 'execjs'
...
test.js
// Generated by CoffeeScript 1.7.1
(function() {
var PDFDocument, doc, fs;
fs = require("fs");
PDFDocument = require('pdfkit');
doc = new PDFDocument;
doc.pipe(fs.createWriteStream('output.pdf'));
doc.addPage().fontSize(25).text('Here is some vector graphics...', 100, 100);
doc.save().moveTo(100, 150).lineTo(100, 250).lineTo(200, 250).fill("#FF3300");
doc.scale(0.6).translate(470, -380).path('M 250,75 L 323,301 131,161 369,161 177,301 z').fill('red', 'even-odd').restore();
doc.addPage().fillColor("blue").text('Here is a link!', 100, 100).underline(100, 100, 160, 27, {
color: "#0000FF"
}).link(100, 100, 160, 27, 'http://google.com/');
doc.end();
}).call(this)
注意,我确实将节点_模块检查到了源代码控制中。
我将所有这些推送到heroku,并尝试使用execjs手动调用test.js:
matt$ heroku run rails c
heroku-irb> ExecJS.eval(File.open('test.js').read)
我得到以下错误:
ExecJS::ProgramError: TypeError: undefined is not a function
from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/external_runtime.rb:68:in `extract_result'
from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/external_runtime.rb:28:in `block in exec'
from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/external_runtime.rb:41:in `compile_to_tempfile'
from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/external_runtime.rb:27:in `exec'
from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/external_runtime.rb:19:in `eval'
from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/runtime.rb:40:in `eval'
from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/module.rb:23:in `eval'
from (irb):3
from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from /app/bin/rails:4:in `require'
from /app/bin/rails:4:in `<main>'
但我也犯了同样的错误。我想我在execjs上错过了一些非常重要的东西。谁能启发我
然后我想看看是否可以直接从节点调用test.js。注意,这在我的本地开发环境中起作用
matt$ heroku run bash
heroku$ node test.js
但我得到:
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'zlib'
at Function._resolveFilename (module.js:320:11)
at Function._load (module.js:266:25)
at require (module.js:348:19)
at Object.<anonymous> (/app/node_modules/pdfkit/js/reference.js:12:10)
at Object.<anonymous> (/app/node_modules/pdfkit/js/reference.js:101:4)
at Module._compile (module.js:404:26)
at Object..js (module.js:410:10)
at Module.load (module.js:336:31)
at Function._load (module.js:297:12)
at require (module.js:348:19)
node.js:134
抛出e;//process.nextTick错误,或第一次勾选时的“error”事件
^
错误:找不到模块“zlib”
at函数。\u解析文件名(module.js:320:11)
at功能。加载(模块js:266:25)
根据需要(module.js:348:19)
反对。(/app/node_modules/pdfkit/js/reference.js:12:10)
反对。(/app/node_modules/pdfkit/js/reference.js:101:4)
在模块处编译(Module.js:404:26)
at Object..js(module.js:410:10)
在Module.load(Module.js:336:31)
at函数。_加载(module.js:297:12)
根据需要(module.js:348:19)
所以现在我举手,希望社区的智慧能帮助我。如果您想了解更多信息,请告诉我
非常感谢,
Matt所以问题在于,如果应用程序依赖于execjs,默认的buildpack会自动安装node版本。不幸的是,这是一个非常旧的节点版本——0.4.7——与我需要使用的节点库不兼容 我的解决方案是使用heroku的buildpack,并修补add_node_js_binary方法,以避免安装节点二进制文件。只要您的多构建包配置包含一个节点发行版,此功能就可以正常工作。在这里查看:
太棒了,我遇到了这个问题。正如您所说,/app/bin首先在环境路径变量中,它有节点0.4.7,而路径后面的另一个目录有package.json中指定的节点版本。超级烦人——准备放弃你的回购协议,开心一点。谢谢顺便说一句,我还是很难让ExecJS表现得好一些。。。。看到我的问题了吗:有什么想法吗?我不知道如何解决这个问题——我实际上是想让gem工作(browserify rails),它在资产编译期间使用Heroku上的节点模块。
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'zlib'
at Function._resolveFilename (module.js:320:11)
at Function._load (module.js:266:25)
at require (module.js:348:19)
at Object.<anonymous> (/app/node_modules/pdfkit/js/reference.js:12:10)
at Object.<anonymous> (/app/node_modules/pdfkit/js/reference.js:101:4)
at Module._compile (module.js:404:26)
at Object..js (module.js:410:10)
at Module.load (module.js:336:31)
at Function._load (module.js:297:12)
at require (module.js:348:19)