using nodejs and forever i get the error Cannot find module - node.js

I am trying to get forever to start up all my microservices
All the services fail the same way
my forever.json looks like (abbreviated)
[
{
"uid": "account-service",
"append": true,
"watch": true,
"script": "app.js",
"sourceDir": "/home/trig/account-service/",
"workingDir": "/home/trig/account-service/",
"logFile": "/home/trig/logs/forever.log",
"outFile": "/home/trig/logs/account_service_out.log",
"errFile": "/home/trig/logs/account_service_err.log"
},
after starting i do
forever list and get (abbreviated)
/home/trig/.forever/lQOi.log STOPPED
if i look at the log
data: account-service:25858 - module.js:471
data: account-service:25858 - throw err;
data: account-service:25858 - ^
data: account-service:25858 - Error: Cannot find module '/home/trig/account-service'
data: account-service:25858 - at Function.Module._resolveFilename (module.js:469:15)
data: account-service:25858 - at Function.Module._load (module.js:417:25)
data: account-service:25858 - at Module.runMain (module.js:604:10)
data: account-service:25858 - at run (bootstrap_node.js:383:7)
data: account-service:25858 - at startup (bootstrap_node.js:149:9)
data: account-service:25858 - at bootstrap_node.js:496:3
data: account-service:25858 - error: Forever detected script exited with code: 1
but if i do
ls -ls /home/trig/account-service
4 -rw-rw-r-- 1308 Dec 28 20:43 app.js
4 drwxrwxr-x 4096 Dec 28 20:43 controllers
4 drwxrwxr-x 4096 Dec 22 05:36 env
4 drwxrwxr-x 4096 Dec 28 20:43 models
4 drwxrwxr-x 154 4096 Dec 28 20:43 node_modules
4 -rw-rw-r-- 1 359 Dec 28 20:43 package.json
4 drwxrwxr-x 2 4096 Dec 28 20:43 utils
I then tried and changed the paths from absolute to relative ( based on comment below )
[
{
"uid": "account-service",
"append": true,
"watch": true,
"script": "app.js",
"sourceDir": "./account-service/",
"workingDir": "./account-service/",
"logFile": "/home/trig/logs/forever.log",
"outFile": "/home/trig/logs/account_service_out.log",
"errFile": "/home/trig/logs/account_service_err.log"
},
then did
forever start account-service ./forever.json
and still see the error
data: account-service:1795 - module.js:471
data: account-service:1795 - throw err;
data: account-service:1795 - ^
data: account-service:1795 - Error: Cannot find module '/home/trig/account-service'
data: account-service:1795 - at Function.Module._resolveFilename (module.js:469:15)
data: account-service:1795 - at Function.Module._load (module.js:417:25)
data: account-service:1795 - at Module.runMain (module.js:604:10)
data: account-service:1795 - at run (bootstrap_node.js:383:7)
data: account-service:1795 - at startup (bootstrap_node.js:149:9)
data: account-service:1795 - at bootstrap_node.js:496:3
data: account-service:1795 - error: Forever detected script exited with code: 1
if move the forever.json file into the dir /home/trig/account-service ( and remove the other services) and leave the sourceDir and workingDir to absolute path it starts correctly

I gave up trying to get forever to work with multiple apps .
I tried http://pm2.keymetrics.io/ and got it working pretty quickly.
so far so good

Related

Error: Cannot find module '/app/wait-for-it.sh"'

I am trying to dockerization my backend server.
my stack is nodejs-nestjs with redis and postgres
here is my Dockerfile
FROM node:15
WORKDIR /usr/src/app
COPY package*.json ./
COPY tsconfig.json ./
COPY wait-for-it.sh ./
COPY . .
RUN npm install -g npm#7.22.0
RUN npm install
RUN npm run build
RUN chmod +x ./wait-for-it.sh .
EXPOSE 3333
CMD [ "sh", "-c", "npm run start:prod"]
and here is my docker-compose file:
version: '3.2'
services:
redis-service:
image: "redis:alpine"
container_name: redis-container
ports:
- 127.0.0.1:6379:6379
expose:
- 6379
postgres:
image: postgres:14.1-alpine
container_name: postgres-container
restart: always
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=1234
- DB_NAME = db
ports:
- 127.0.0.1:5432:5432
expose:
- 5432
volumes:
- db:/var/lib/postgresql/data
oms-be:
build: .
ports:
- 3333:3333
links:
- postgres
- redis-service
depends_on:
- postgres
- redis-service
environment:
- DB_HOST=postgres
- POSTGRES_PASSWORD = 1234
- POSTGRES_USER=root
- AUTH_REDIS_HOST=redis-service
- DB_NAME = db
command: ["./wait-for-it.sh", "postgres:5432", "--", "sh", "-c", "npm run start:prod"]
volumes:
db:
driver: local
However, when I run docker-compose up
I got this error :
taching to oms-be-oms-be-1, postgres-container, redis-container
redis-container | 1:C 05 Jun 2022 00:35:16.730 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-container | 1:C 05 Jun 2022 00:35:16.730 # Redis version=7.0.0, bits=64, commit=00000000, modified=0, pid=1, just started
redis-container | 1:C 05 Jun 2022 00:35:16.730 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-container | 1:M 05 Jun 2022 00:35:16.731 * monotonic clock: POSIX clock_gettime
redis-container | 1:M 05 Jun 2022 00:35:16.731 * Running mode=standalone, port=6379.
redis-container | 1:M 05 Jun 2022 00:35:16.731 # Server initialized
redis-container | 1:M 05 Jun 2022 00:35:16.731 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis-container | 1:M 05 Jun 2022 00:35:16.732 * The AOF directory appendonlydir doesn't exist
redis-container | 1:M 05 Jun 2022 00:35:16.732 * Ready to accept connections
postgres-container |
postgres-container | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres-container |
postgres-container | 2022-06-05 00:35:16.824 UTC [1] LOG: starting PostgreSQL 14.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
postgres-container | 2022-06-05 00:35:16.824 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres-container | 2022-06-05 00:35:16.824 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres-container | 2022-06-05 00:35:16.827 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres-container | 2022-06-05 00:35:16.833 UTC [21] LOG: database system was shut down at 2022-06-05 00:34:36 UTC
postgres-container | 2022-06-05 00:35:16.836 UTC [1] LOG: database system is ready to accept connections
oms-be-oms-be-1 | internal/modules/cjs/loader.js:905
oms-be-oms-be-1 | throw err;
oms-be-oms-be-1 | ^
oms-be-oms-be-1 |
oms-be-oms-be-1 | Error: Cannot find module '/app/wait-for-it.sh"'
oms-be-oms-be-1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
oms-be-oms-be-1 | at Function.Module._load (internal/modules/cjs/loader.js:746:27)
oms-be-oms-be-1 | at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
oms-be-oms-be-1 | at internal/main/run_main_module.js:17:47 {
oms-be-oms-be-1 | code: 'MODULE_NOT_FOUND',
oms-be-oms-be-1 | requireStack: []
oms-be-oms-be-1 | }
oms-be-oms-be-1 exited with code 1
I tried to build it without wait-for-it.sh and it was complaining that the server cannot connect to the Postgres DB and Redis, so I added wait-for-it.sh file to make it wait until the Redis and the Postgres DB are up, but I got the above error
Can anyone tell me what I am doing wrong?
I've simplified your Dockerfile and docker-compose.yaml in order to test things out on my system. I have this package.json:
{
"name": "example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "echo \"Example build command\"",
"start:prod": "sleep inf"
},
"author": "",
"license": "ISC"
}
And this Dockerfile:
FROM node:15
WORKDIR /usr/src/app
COPY package*.json ./
COPY wait-for-it.sh ./
RUN chmod +x ./wait-for-it.sh .
RUN npm install
RUN npm run build
EXPOSE 3333
CMD [ "sh", "-c", "npm run start:prod"]
And this docker-compose.yaml:
version: '3.2'
services:
postgres:
image: docker.io/postgres:14
environment:
POSTGRES_PASSWORD: secret
oms-be:
build: .
ports:
- 3333:3333
command: [./wait-for-it.sh", "postgres:5432", "--", "sh", "-c", "npm run start:prod"]
Note that the command: on the final line there has the missing quote. If I try to bring this up using docker-compose up, I see:
oms-be_1 | node:internal/modules/cjs/loader:927
oms-be_1 | throw err;
oms-be_1 | ^
oms-be_1 |
oms-be_1 | Error: Cannot find module '/usr/src/app/wait-for-it.sh"'
oms-be_1 | at Function.Module._resolveFilename (node:internal/modules/cjs/loader:924:15)
oms-be_1 | at Function.Module._load (node:internal/modules/cjs/loader:769:27)
oms-be_1 | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
oms-be_1 | at node:internal/main/run_main_module:17:47 {
oms-be_1 | code: 'MODULE_NOT_FOUND',
oms-be_1 | requireStack: []
oms-be_1 | }
If I correct the syntax so that we have:
version: '3.2'
services:
postgres:
image: docker.io/postgres:14
environment:
POSTGRES_PASSWORD: secret
oms-be:
build: .
ports:
- 3333:3333
command: ["./wait-for-it.sh", "postgres:5432", "--", "sh", "-c", "npm run start:prod"]
Then it runs successfully:
oms-be_1 | wait-for-it.sh: waiting 15 seconds for postgres:5432
oms-be_1 | wait-for-it.sh: postgres:5432 is available after 0 seconds
oms-be_1 |
oms-be_1 | > example#1.0.0 start:prod
oms-be_1 | > sleep inf
oms-be_1 |
The difference in behavior is due to the ENTRYPOINT script in the underlying node:15 image, which includes this logic:
if [ "${1#-}" != "${1}" ] || [ -z "$(command -v "${1}")" ]; then
set -- node "$#"
fi
That says, essentially:
IF the first parameter starts with -
OR There is no command matching $1
THEN try starting the command with node
With the missing ", you end up with an argument that doesn't match any valid commands, which is why you end up with an error in which node is trying to run the wait-for-it.sh script.

Imports from tsconfig paths not working with bazel: Cannot find module '#...'

Repo for reproduction: https://github.com/timothyaltemus/bazel-typescript-stackoverflow-example.
I am running into an issue where I am unable to use my tsconfig paths configuration for importing in my TypeScript files.
tsconfig.json:
{
...
"compilerOptions": {
"baseUrl": ".",
"paths" {
"#*": [
".",
"bazel-out/darwin-fastbuild/bin/*",
"bazel-out/k8-fastbuild/bin/*",
"bazel-out/x64_windows-fastbuild/bin/*",
"bazel-out/darwin-dbg/bin/*",
"bazel-out/k8-dbg/bin/*",
"bazel-out/x64_windows-dbg/bin/*"
]
},
"rootDirs": [
".",
"bazel-out/darwin-fastbuild/bin/",
"bazel-out/k8-fastbuild/bin/",
"bazel-out/x64_windows-fastbuild/bin/",
"bazel-out/darwin-dbg/bin/",
"bazel-out/k8-dbg/bin/",
"bazel-out/x64_windows-dbg/bin/"
]
...
}
...
}
Folder Structure:
apps
server
services
BUILD
hello_service.ts
BUILD
index.ts
libs
a_lib
BUILD
do_a.ts
b_lib
BUILD
do_b.ts
c_lib
BUILD
do_c.ts
Build
tsconfig.json
index.ts:
...
import { doToC } from '#libs/c_lib/do_c';
import {
HelloService,
HelloServiceImpl,
} from '#apps/server/services/hello_service';
...
Full Stacktrace:
INFO: Analyzed target //apps/server:server (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //apps/server:server up-to-date:
bazel-bin/apps/server/server.sh
bazel-bin/apps/server/server_loader.js
bazel-bin/apps/server/server_require_patch.js
INFO: Elapsed time: 1.994s, Critical Path: 0.03s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Error: Cannot find module '#libs/c_lib/do_c'
Require stack:
- /home/tim/.cache/bazel/_bazel_tim/4d04e993ef8ae996d277978eb4eab582/execroot/typescript/bazel-out/k8-fastbuild/bin/apps/server/server.sh.runfiles/typescript/apps/server/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (home/tim/.cache/bazel/_bazel_tim/4d04e993ef8ae996d277978eb4eab582/execroot/typescript/bazel-out/k8-fastbuild/bin/apps/apps/server/index.ts:4:1)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
Please see the example repo for an example of how to reproduce this error.

Semantic-release - Error: Cannot find module '../output'

No matter what I do I always get exactly the same error when I run command 'npx semantic-release --debug
' in BitBucket pipelines. Dry-run seems to run fine.
Thing I have already tried:
reinstall node modules
update packages
copy exactly the same configuration/packages from project with
working semantic-release
All the files are definitely there. Maybe it can't access?
here is a full debug report:
npx: installed 746 in 16.856s
[5:36:26 PM] [semantic-release] › ℹ Running semantic-release version 17.1.1
2020-06-25T17:36:26.883Z semantic-release:config load config from: /opt/atlassian/pipelines/agent/build/package.json
2020-06-25T17:36:26.887Z semantic-release:config options values: {
branches: [ 'master', 'develop' ],
repositoryUrl: 'git#$NEXUS_USERNAME.org:acurable/acupebble-g2-firmware.git',
tagFormat: 'v${version}',
plugins: [
[ '#semantic-release/commit-analyzer', [Object] ],
[ '#semantic-release/npm', [Object] ],
[ '#semantic-release/release-notes-generator', [Object] ],
'#semantic-release/changelog',
'#semantic-release/git'
],
_: [],
debug: true,
'$0': '/root/.npm/_npx/52/bin/semantic-release'
}
[5:36:27 PM] [semantic-release] › ✖ An error occurred while running semantic-release: Error: Cannot find module '../output'
Require stack:
- /opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/output-json.js
- /opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/index.js
- /opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/index.js
- /opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/lib/prepare.js
- /opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/index.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/utils.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/index.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/lib/get-config.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/index.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/cli.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/bin/semantic-release.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:952:15)
at Function.Module._load (internal/modules/cjs/loader.js:835:27)
at Module.require (internal/modules/cjs/loader.js:1012:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/output-json.js:4:24)
at Module._compile (internal/modules/cjs/loader.js:1123:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
at Module.load (internal/modules/cjs/loader.js:972:32)
at Function.Module._load (internal/modules/cjs/loader.js:872:14)
at Module.require (internal/modules/cjs/loader.js:1012:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/output-json.js',
'/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/index.js',
'/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/index.js',
'/opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/lib/prepare.js',
'/opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/index.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/utils.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/index.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/lib/get-config.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/index.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/cli.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/bin/semantic-release.js'
]
}
Error: Cannot find module '../output'
Require stack:
- /opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/output-json.js
- /opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/index.js
- /opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/index.js
- /opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/lib/prepare.js
- /opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/index.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/utils.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/index.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/lib/get-config.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/index.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/cli.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/bin/semantic-release.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:952:15)
at Function.Module._load (internal/modules/cjs/loader.js:835:27)
at Module.require (internal/modules/cjs/loader.js:1012:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/output-json.js:4:24)
at Module._compile (internal/modules/cjs/loader.js:1123:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
at Module.load (internal/modules/cjs/loader.js:972:32)
at Function.Module._load (internal/modules/cjs/loader.js:872:14)
at Module.require (internal/modules/cjs/loader.js:1012:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/output-json.js',
'/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/index.js',
'/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/index.js',
'/opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/lib/prepare.js',
'/opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/index.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/utils.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/index.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/lib/get-config.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/index.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/cli.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/bin/semantic-release.js'
]
2020-06-25T17:36:27.964997083Z stderr P }
This is crazy.
Just realised that I had this line in gitIgnore.txt: **/Output
In my local repository all the files were there but index.js was missing since all files in any **/Output path would not be included.
Will never use global gitIgnore again.

Found extra flags error in new version of protractor

I am trying to run a js scripts using protractor, but i am getting following error
C:\Users\Hoodi\AppData\Roaming\npm\node_modules\protractor\built\cli.js:172
throw new Error('Found extra flags: ' + unknownKeys.join(', '));
^
Error: Found extra flags: identityManagement
at Object.<anonymous> (C:\Users\Hoodi\AppData\Roaming\npm\node_modules\protractor\built\cli.js:172:15)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\Hoodi\AppData\Roaming\npm\node_modules\protractor\bin\protractor:5:1)
at Module._compile (module.js:570:32)
My config file
// conf.js
exports.config = {
framework: 'jasmine',
seleniumAddress: 'http://localhost:4444/wd/hub',
useAllAngular2AppRoots: true,
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
// Use colors in the command line report.
showColors: true,
// Default time to wait in ms before a test fails.
defaultTimeoutInterval: 60000,
},
onPrepare: function() {
require('../../lib/waitReady.js');
},
capabilities :{
browserName : 'chrome',
}
}
console.dir("argv: " + process.argv)
switch(process.argv[3]) {
case '--identityManagement':
exports.config.specs = ['./identityManagement.js'];
break;
default:
exports.config.specs = ['./identityManagement.js'];
}
I tried executing above script using protractor as well as "npm run" command, but in both cases i am getting same error. command that i used
protractor ./conf.js --identityManagement
and
npm run im
This actually works on my other system. npm and node version of system where this works are
node v7.2.1
npm v3.10.10
And where is doesnt work
node v7.4.0
npm v4.0.5
My package.json file looks like this
{
"name": "intelliflash",
"author": "Vishwanath Rawat <rawat#tegile.com>",
"description": "IntelliFlash tests",
"scripts": {
"im": "protractor ./conf.js --identityManagement"
}
}
Please help.
It has nothing to do with your npm or node versions, Latest release of Protractor 5.0 has put a check in cli for unidentified flags as you are putting with the help of process.argv.
You can solve this by disabling the flag checks:
protractor ./conf.js --identityManagement --disableChecks
For more details please refer the Protractor 5.0 changelog
Note: min node version now is v6.9.x which supports this version of protractor

How to deploy a phantomjs node app on AWS Lambda?

I threw together a small Lambda function together to crawl a website using the SpookyJS, CasperJS, and PhantomJS toolchain for headless browsing. The task is quite simple, and at some point a few months ago it was working in Lambda. I recently had to change a few things around and wanted to work on the project again, but started fresh and had trouble getting Lambda to run without erroring in any capacity. My question is how can I run phantomjs in Lambda?
The example code I am running is:
spooky.start('http://en.wikipedia.org/wiki/Spooky_the_Tuff_Little_Ghost');
spooky.then(function () {
this.emit('hello', 'Hello, from ' + this.evaluate(function () {
return document.title;
}));
});
spooky.run();
The error I am getting in Lambda is:
{ [Error: Child terminated with non-zero exit code 1] details: { code: 1, signal: null } }
I have followed a variety of procedures to ensure everything is able to run on Lambda. Below is a long list of things I've attempted to diagnose:
Run locally using node index.js and confirm it is working
Upload package.json and the js file to an Amazon Linux EC2 instance for compilation as recommended for npm installation calls and described here
Run npm install on the ec2 instance, and again run node index.js to ensure the correct output
zip everything up, and deploy to AWS using the cli
My package.json is:
{
"name": "lambda-spooky-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"casperjs": "^1.1.3",
"phantomjs-prebuilt": "^2.1.10",
"spooky": "^0.2.5"
}
}
I have also attempted the following (most also working locally, and on the AWS EC2 instance, but with the same error on Lambda:
Trying the non -prebuilt version of phantom
Ensuring casperjs and phantomjs are accessible from the path with process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'] + ':' + process.env['LAMBDA_TASK_ROOT'] + '/node_modules/.bin';
console.log( 'PATH: ' + process.env.PATH );
Inspecting spawn calls by wrapping child_process's .spawn() call, and got the following:
{ '0': 'casperjs',
'1':
[ '/var/task/node_modules/spooky/lib/bootstrap.js',
'--transport=http',
'--command=casperjs',
'--port=8081',
'--spooky_lib=/var/task/node_modules/spooky/lib/../',
'--spawnOptions=[object Object]' ],
'2': {} }
Calling .exec('casperjs') and .exec('phantomjs --version') directly, confirming it works locally and on EC2, but gets the following error in Lambda. The command:
`require('child_process').exec('casperjs', (error, stdout, stderr) => {
if (error) { console.error('error: ' + error); }
console.log('out: ' + stdout);
console.log('err: ' + stderr);
});
both with the following result:
err: Error: Command failed: /bin/sh -c casperjs
module.js:327
throw err;
^
Error: Cannot find module '/var/task/node_modules/lib/phantomjs'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/var/task/node_modules/.bin/phantomjs:16:15)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
2016-08-07T15:36:37.349Z b9a1b509-5cb4-11e6-ae82-256a0a2817b9 sout:
2016-08-07T15:36:37.349Z b9a1b509-5cb4-11e6-ae82-256a0a2817b9 serr: module.js:327
throw err;
^
Error: Cannot find module '/var/task/node_modules/lib/phantomjs'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/var/task/node_modules/.bin/phantomjs:16:15)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
I found the issue to be that including the node_modules/.bin in the path works on both local and ec2 machines because those files simply point to the action /bin folders in each respective library. This breaks if calls within those files use relative paths. The issue:
[ec2-user#ip-172-31-32-87 .bin]$ ls -lrt
total 0
lrwxrwxrwx 1 ec2-user ec2-user 35 Aug 7 00:52 phantomjs -> ../phantomjs-prebuilt/bin/phantomjs
lrwxrwxrwx 1 ec2-user ec2-user 24 Aug 7 00:52 casperjs -> ../casperjs/bin/casperjs
I worked around this by adding each library's respective bin to the lambda path in the Lambda handler function:
process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT']
+ ':' + process.env['LAMBDA_TASK_ROOT'] + '/node_modules/phantomjs-prebuilt/bin'
+ ':' + process.env['LAMBDA_TASK_ROOT'] + '/node_modules/casperjs/bin';
And this will now run phantom, casper, and spooky correctly in Lambda.

Resources