Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realmjs not working after deployment in serverless framework #2509

Closed
mithunph opened this issue Sep 5, 2019 · 7 comments
Closed

Realmjs not working after deployment in serverless framework #2509

mithunph opened this issue Sep 5, 2019 · 7 comments

Comments

@mithunph
Copy link

mithunph commented Sep 5, 2019

Hi, I have installed Realmjs which is working fine in local. But after deployment, it is showing error when i require(or import) that package.
Showing error in the below line
const Realm = require('realm');

Below is the full logs of error:
Unable to import module 'server': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object. (/var/task/node_modules/realm/lib/index.js:103:28)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object. (/var/task/controllers/realmController.js:3:15)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)

@bmunkholm
Copy link
Contributor

@mithunph Could you please fill out the issues template? Specifically how we should reproduce this?

@mithunph
Copy link
Author

mithunph commented Sep 5, 2019

Hi @bmunkholm ,
Below is my realmController.js file:
const Realm = require('realm');
const studentSchema = {
name: 'Students',
properties: {
questionId: {type: 'string'},
subconceptId: {type: 'string', default: '0'},
pointsAchievable: {type: 'int', default: 0},
pointsAchieved: {type: 'int', default: 0},
timeTaken: {type: 'int', default: 0},
}
};
exports.generateOfflineTables = async (req, res) => {
Realm.open({schema: [studentSchema], path: 'students.realm', schemaVersion: 1})
.then(realm => {
realm.write(() => {
console.log('writing file==');
});
realm.close();
})
.catch(error => {
console.log(error);
});
}

I get error in very first line while importing the package

@bmunkholm
Copy link
Contributor

@mithunph Please fill out the entire issue template that occurs when you click "New Issue".

@mithunph
Copy link
Author

mithunph commented Sep 5, 2019

Hi @bmunkholm ,
Below is the full code,
1.serverless.yml file
service: test-backend
provider:
name: aws
runtime: nodejs8.10
memorySize: 512 # set the maximum memory of the Lambdas in Megabytes
timeout: 10 # the timeout is 10 seconds (default is 6 seconds)
stage: ${opt:stage, 'local'} # setting the env stage to dev, this will be visible in the routes
region: ap-south-1
environment:
DB: ${file(./config.${self:provider.stage}.json):DB}
STAGE: ${file(./config.${self:provider.stage}.json):environment}

functions:
server:
handler: server.run
events:
- http: ANY /
- http: 'ANY {proxy+}'

  1. server.js
    const sls = require('serverless-http');
    const handler = require('./handler');
    module.exports.run = sls(handler);

3)handler.js
'use strict';
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const cors = require('cors');
var compression = require('compression');
require('dotenv').config({ path: './variables.env' });
require('./db').connectToDatabase();
app.use(compression()); //use compression
app.use(bodyParser.json({ limit: "50mb" ,strict: false }));
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true, parameterLimit:50000 }));
app.use(expressValidator()); //required for Express-Validator
app.use(cors());

const routes = require('./routes/routes.js')
app.use('/', routes);

module.exports = app;

4)routes.js
var app = express();
const router = express.Router();
const realmController = require('../controllers/realmController');
app.use('/generate-offline-tables', realmController.generateOfflineTables);
app.use('', router);
module.exports = app;

5)realmController.js
const Realm = require('realm');
const studentSchema = {
name: 'Students',
properties: {
questionId: {type: 'string'},
subconceptId: {type: 'string', default: '0'},
pointsAchievable: {type: 'int', default: 0},
pointsAchieved: {type: 'int', default: 0},
timeTaken: {type: 'int', default: 0},
}
};
exports.generateOfflineTables = async (req, res) => {
Realm.open({schema: [studentSchema], path: 'students.realm', schemaVersion: 1})
.then(realm => {
realm.write(() => {
console.log('writing file==');
});
realm.close();
})
.catch(error => {
console.log(error);
});
}

@bmunkholm
Copy link
Contributor

Hi @mithunph
Running on AWS lambda does unfortunately not work at the moment for other reasons than what you see here. Sorry about that.

@mithunph
Copy link
Author

mithunph commented Sep 5, 2019

I fixed it by adding the below line in package.json
"postinstall": "chmod +x ./bin/build-realm.sh"

Added below code to build-realm.sh file
#!/usr/bin/env bash
set -e
npm install -g node-gyp
npm install -g node-pre-gyp
cd ./node_modules/realm/
node-pre-gyp install --fallback-to-build

@bmunkholm
Copy link
Contributor

Great @mithunph !
I'm curious to know if everything works now, as Amazon has previously had a bug that prevented us from working.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants