CasperJS 和 AWS Lambda
CasperJS and AWS Lambda
我正在尝试让 casperjs 与我的 AWS Lambda 函数一起工作。
{
"errorMessage": "Cannot find module 'casper'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:276:25)",
"Module.require (module.js:353:17)",
"require (internal/module.js:12:17)",
"Object.<anonymous> (/var/task/index.js:3:14)",
"Module._compile (module.js:409:26)",
"Object.Module._extensions..js (module.js:416:10)",
"Module.load (module.js:343:32)",
"Function.Module._load (module.js:300:12)",
"Module.require (module.js:353:17)"
]
}
我不断收到 Lambda 无法检测到 casperjs 的错误。我将我的 zip 文件上传到 Lambda,并在我压缩文件之前将 casperjs 模块安装到我的目录中。
我的 package.json 文件说我安装了 casperjs。
{
"name": "lambda",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"casperjs": "^1.1.3",
}
}
有人知道我做错了什么吗?谢谢。
根据 Casper.js 文档,它实际上并不是一个节点模块。所以你不能在 Package.json 中要求它并用节点模块压缩它。您将需要找到如何将它安装在 lambda 实例上,或者找到一个可以执行您想要的操作的实际节点模块。我怀疑在 lambda 上安装 casper 可能是不可能的,但这只是我的直觉。
Warning
While CasperJS is installable via npm, it is not a NodeJS module and will not work with NodeJS out of the box. You cannot load casper by using require(‘casperjs’) in node. Note that CasperJS is not capable of using a vast majority of NodeJS modules out there. Experiment and use your best judgement.
由于 CasperJs 依赖于 PhantomJs,您可以将其设置为与此 repo 非常相似:https://github.com/TylerPachal/lambda-node-phantom.
主要区别在于您需要添加和定位 CasperJs,并且您需要确保 CasperJs 可以找到并加载 PhantomJs。
在您的包目录中创建一个 node_modules 目录。
将 CasperJs 的依赖项添加到 packages.json 文件:
"dependencies": {
"casperjs": "latest"
}
在终端中,导航到您的包目录,然后 运行 'npm update' 将 CasperJs 包添加到 node_modules 目录。
假设你想 运行 CasperJs 带有 'test' 参数,index.js 文件将需要更改为如下所示:
var childProcess = require('child_process');
var path = require('path');
exports.handler = function(event, context) {
// Set the path as described here: https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/
process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'];
// Set the path to casperjs
var casperPath = path.join(__dirname, 'node_modules/casperjs/bin/casperjs');
// Arguments for the casper script
var processArgs = [
'test',
path.join(__dirname, 'casper_test_file.js')
];
// Launch the child process
childProcess.execFile(casperPath, processArgs, function(error, stdout, stderr) {
if (error) {
context.fail(error);
return;
}
if (stderr) {
context.fail(error);
return;
}
context.succeed(stdout);
});
}
如果您不想 运行 CasperJs 带有 'test' 参数,只需将其从参数列表中删除即可。
你的包根目录下的PhantomJs二进制文件需要重命名为phantomjs,这样CasperJs才能找到它。如果你想获得新版本的 PhantomJs,你可以在这里获得一个:https://bitbucket.org/ariya/phantomjs/downloads。确保下载 linux-x86_64.tar.bz2 类型,以便它可以在 Lambda 中 运行。下载后,只需从 bin 目录中拉出一个新的二进制文件并将其放在根包目录中。
为了让Lambda拥有访问所有文件的权限,最简单的方法是在类Unix操作系统中压缩包。确保包中的所有文件都具有读取和执行权限。从包目录中:chmod -R o+rx *
。然后压缩它:zip -r my_package.zip *
.
将压缩包上传到您的 Lambda 函数。
我正在尝试让 casperjs 与我的 AWS Lambda 函数一起工作。
{
"errorMessage": "Cannot find module 'casper'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:276:25)",
"Module.require (module.js:353:17)",
"require (internal/module.js:12:17)",
"Object.<anonymous> (/var/task/index.js:3:14)",
"Module._compile (module.js:409:26)",
"Object.Module._extensions..js (module.js:416:10)",
"Module.load (module.js:343:32)",
"Function.Module._load (module.js:300:12)",
"Module.require (module.js:353:17)"
]
}
我不断收到 Lambda 无法检测到 casperjs 的错误。我将我的 zip 文件上传到 Lambda,并在我压缩文件之前将 casperjs 模块安装到我的目录中。
我的 package.json 文件说我安装了 casperjs。
{
"name": "lambda",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"casperjs": "^1.1.3",
}
}
有人知道我做错了什么吗?谢谢。
根据 Casper.js 文档,它实际上并不是一个节点模块。所以你不能在 Package.json 中要求它并用节点模块压缩它。您将需要找到如何将它安装在 lambda 实例上,或者找到一个可以执行您想要的操作的实际节点模块。我怀疑在 lambda 上安装 casper 可能是不可能的,但这只是我的直觉。
Warning While CasperJS is installable via npm, it is not a NodeJS module and will not work with NodeJS out of the box. You cannot load casper by using require(‘casperjs’) in node. Note that CasperJS is not capable of using a vast majority of NodeJS modules out there. Experiment and use your best judgement.
由于 CasperJs 依赖于 PhantomJs,您可以将其设置为与此 repo 非常相似:https://github.com/TylerPachal/lambda-node-phantom.
主要区别在于您需要添加和定位 CasperJs,并且您需要确保 CasperJs 可以找到并加载 PhantomJs。
在您的包目录中创建一个 node_modules 目录。
将 CasperJs 的依赖项添加到 packages.json 文件:
"dependencies": { "casperjs": "latest" }
在终端中,导航到您的包目录,然后 运行 'npm update' 将 CasperJs 包添加到 node_modules 目录。
假设你想 运行 CasperJs 带有 'test' 参数,index.js 文件将需要更改为如下所示:
var childProcess = require('child_process'); var path = require('path'); exports.handler = function(event, context) { // Set the path as described here: https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/ process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT']; // Set the path to casperjs var casperPath = path.join(__dirname, 'node_modules/casperjs/bin/casperjs'); // Arguments for the casper script var processArgs = [ 'test', path.join(__dirname, 'casper_test_file.js') ]; // Launch the child process childProcess.execFile(casperPath, processArgs, function(error, stdout, stderr) { if (error) { context.fail(error); return; } if (stderr) { context.fail(error); return; } context.succeed(stdout); }); }
如果您不想 运行 CasperJs 带有 'test' 参数,只需将其从参数列表中删除即可。
你的包根目录下的PhantomJs二进制文件需要重命名为phantomjs,这样CasperJs才能找到它。如果你想获得新版本的 PhantomJs,你可以在这里获得一个:https://bitbucket.org/ariya/phantomjs/downloads。确保下载 linux-x86_64.tar.bz2 类型,以便它可以在 Lambda 中 运行。下载后,只需从 bin 目录中拉出一个新的二进制文件并将其放在根包目录中。
为了让Lambda拥有访问所有文件的权限,最简单的方法是在类Unix操作系统中压缩包。确保包中的所有文件都具有读取和执行权限。从包目录中:
chmod -R o+rx *
。然后压缩它:zip -r my_package.zip *
.将压缩包上传到您的 Lambda 函数。