NodeJS AWS Lambda Require not able to import subfolders - node.js

So I am attempting to deploy a zipped nodejs project to AWS Lambda to run the script for me. It has a directory structure similar to the following:
apps
-api1
-case1
-index.js
-case2
-api2
-case1
index.js
And you get the rest. The lambda can successfully find and run the index.js script, the problem is I import those api modules through a require('./api1/case1);. For some reason, the lambda is unable to find them and when I try to test it throws this error:
"errorMessage": "Error: Cannot find module './apps/api1/case1'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
I've opened the zip I've uploaded and seen that the apps folder and everything in it is there in the correct spot, including the index.js file. Does anyone have any ideas what could be wrong?

I had this issue where I was zipping the entire folder which contains app folder and index.js file.
So after uploading, it was unable to locate.
instead, I selected all folders manually and compressed it. (I'm not compressing the root folder, I'm inside root folder and selecting all the required files and zipping it. ) and then uploaded.
Boom!!! it worked.

Related

Accessing typeDefs in the dist folder | NestJS/GraphQL

Context: I had a project at which backend was written with GraphQL/lambdas. Now I want to convert whole project to NestJS/GraphQL.
I have joined.graphql file in src/GraphQL folder and there is an import (usage) of this file in another .ts file. When I try to start the project with nest start command, none of *.graphql files are copied to dist folder. Then, that import throws an error as there is no joined.graphql file in the dist folder.
I tried to add nest-cli.json file with assets field to include ./**/*.graphql files as assets. Now the issue is indeed fixed. But...
Problem:
Another error occured as
/<MY_PROJECT>/dist/src/GraphQL/joined.graphql
type Query {
^^^^^
SyntacError: Unexpected identifier
The code was trying to import typeDefs. I think there should be something a bit tricky since I'm not starting the project with nest but trying to convert to nest. Maybe missing some steps to work correctly with graphql. I'm not sure.

Heroku Error: ENOENT: no such file or directory, stat '/app/index.html'

I deployed my nodejs app to Heroku
My project folder structure is as below
folder structure
Server.js
Server.js code
After deployment I am getting above error as no such file or directory /app/index.html
I tried running run bash command to check folder structure on Heroku but it doesnt show me app folder at root path
Heroku run bash output
wondering why I can't see app folder above where it is coming from?
Appreciate your help.
Your index.html has a caps, you called it Index.html.
If you are using windows just notice you might have to delete Index.html, then push your project without it, then push it again with your file correctly named (index.html).
You are not seeing app/ because you are inside of it.

How to path to an image file in an electron app?

I am creating an electron app and packaging for distribution with electron-builder for windows and Mac. This app creates a folder and some pdfs inside varying based on user input. The pdfs also use an image, which as a node app I was keeping in the root folder of the app.
I managed to write to the desktop using an absolute path.
if (!fs.existsSync(`/Users/${user}/Desktop/2019 Certificates`)){
fs.mkdirSync(`/Users/${user}/Desktop/2019 Certificates`);
}
but when I use this relative path
stampandseal.png
I get the following error:
I expect it to find the png relative to the js file, however I get the following error:
fs.js:121 Uncaught Error: ENOENT: no such file or directory, open 'stampandseal.png'
If I understand your issue correctly, you are trying to copy an image from within the app bundle to the user's desktop. Use __dirname to reference the directory your code is executing in, and then build the path off of that.
The code below is used by my main.js file, which is in the directory containing my app directory. I use upath to build the path and jetpack instead of fs for copying
var fromPath = upath.join(__dirname, "app", "assets", "image.png");
jetpack.copy(fromPath, toPath, { overwrite: true });

NodeJS/CloudFoundry - failed: The app upload is invalid: Symlink(s) point outside of root folder

I'm testing CloudFoundry on IBM and are running NodeJS.
When trying to cf push my application I get the following error:
failed: The app upload is invalid: Symlink(s) point outside of root folder
In my appllcation I have the following code:
return res.sendFile(path.join(__dirname +'/tvshows/'+ guide +'.html'));
When not using path.join and simply use:
return res.sendFile(path.join('./tvshows/'+ guide +'.html'));
I get this error instead:
TypeError: path must be absolute or specify root to res.sendFile
What to do?
I've also tried stuff like path.join((process.env.BUILD_DIR || __dirname), and return res.sendFile('index.html', { root: path.join(__dirname, 'tvshows', guide) }); but no luck.
The fail came from my node_modules folder.
Adding .cfignore with node_modules/ fixed the issue.
You didn't mention the version of the cf cli that you're using, but I think that this is expected behavior starting with version 6.34.0.
push now preserves relative symlinks in app files. This makes it easier to work with Node.js apps using npm link, but note that it now errors when it detects a symlink pointing outside the app folder (e.g. a node_modules folder containing external symlinks).
https://github.com/cloudfoundry/cli/releases/tag/v6.34.0
I think you're running into the second part, "how errors when it detects a symlink pointing outside the app folder". It's nothing to do with the code in your app, but rather somewhere in your project folder there is a symlink which references another file that is not under the root of your project.
If you're on a unix-like system you can run find . -type l to find all the symlinks under the current directory. Then you just need to figure out which one is pointing outside of the project root.
Options are to remove that symlink, point it to something under your project root or use .cfignore to ignore that file (which is what you ended up doing).
Hope that helps!

Reading files from a directory inside a meteor app

How can i read the public directory in a meteor application inside my /server path.
I tried using the native 'fs' package but i keep getting a file/directory not found error.
var fs = Npm.require('fs');
var files = fs.readdirSync('/public/soundfiles/');
Has anyone used the filesystem package to read static files inside a meteor application?
I learned that it is best to upload files in your private folder if you are not displaying them outside.
In my case I need to store XML uploads and process them.
At first I wrote the XML into the public folder but that would trigger a reload.
Then I renamed the the upload folder to /public/.#uploads which would stop the reload of Meteor, but then again...it completely ignored that folder during build and the uploaded folder would not exist in the build (throw ENOENT error during read).
So I figured out it is best to put the files in /private/files and then reading goes as follows:
result = fs.readdirSync('assets/app/files')
Everything in the private folder will be moved to the Assets folder where during runtime there is an APP folder available (you do not see that in your build folder structure).
It helps to just simple dump result = fs.readdirSync('.') to see what folder you in and look through the structure.
***UPDATE*****
Locally putting files in private folder still triggered meteor rebuild/update (perhaps not in production..) so I found another solution using the UploadServer just to define the upload directory:
https://github.com/tomitrescak/meteor-uploads
This works for me in Meteor 1.0:
var fs = Npm.require('fs')
var xsd = fs.readFileSync(process.cwd().split('.meteor')[0] + 'server/company.xsd', 'utf8')
Access files without the "/public" part. In a running Meteor app, the public directory becomes your root, and everything that is located at /public/whatever can be accessed at /whatever.
Additionally, if you're playing around with files, you might find these useful:
FileSaver.js
CollectionFS
This is no longer true. For Meteor 0.8, the folder "../client/app" is public. Thus, use fs.readdirSync('../client/app') to get files and folders in public.
Source: personal experience and https://stackoverflow.com/a/18405793
For meteor 1.0.2 public is /web.browser/app/
Checked by entering .meteor dir
Total path in linux /home/user/your_app_name/.meteor/local/build/programs/web.browser/app/
And to get to root is `process.env.PWD or process.cwd().
Im not sure if its work deployed.
_meteor_bootstrap_.serverDir +'/assets/app'
This is path to private folder.
For Meteor 1.4, use server Assets.
See the official docs on Assets
http://docs.meteor.com/api/assets.html
On the server you can use fs to access any part of the meteor directory tree, not just /public, for example
import fs from 'fs';
const rd = process.env.PWD;
const obj = JSON.parse(fs.readFileSync(`${rd}/private/file.json`));
would read and parse a json file located at private/file.json under your meteor app directory root.

Resources