Issue with running node app using forever - node.js

So I have a node.js app in which I am using node-config-manager, to manage the various environments (development, staging, production).
I am trying to use forever to run this process in the background, however when I run forever start /path/to/main.js, it is telling me
Error: No file for this config - app.
The error is saying it's coming from line 9. When it first tries to add a configuration file, app.json.
1. import configManager from 'node-config-manager';
2. const options = {
3. configDir: './config',
4. env: 'develop',
5. camelCase: true
6. };
7. console.log(process.env.NODE_ENV);
8. configManager.init(options);
9. configManager.addConfig('app');
10. configManager.addConfig('logger');
11. configManager.addConfig('db');
export default configManager;
Inside my config folder I have three other folders, 'develop', 'staging', and 'production'.
Inside all of these folders I have three files, app.json, db.json, & logger.json.
So I am not sure what is causing the problem here. The config 'develop' does in fact exist, but there seems to be something wrong.
This app runs fine when I start it with nodemon, it's just forever thats causing problems.
Am I missing something with my understanding of how node-config-manager works? I thought all i had to do was change my NODE_ENV variable to the name of the folder inside my config directory and i'd be all set.
Thanks in advance for all the help.

the ./ directive for the config file looks at the relative path from the current working directory. forever is probably not being run from the working directory of the project, so it can't find the file.
if you project lives in /path/to then you need to provide that as the --workdingDir parameter to forever.
forever start /path/to/main.js --workingDir=/path/to/
that should take care of it

Related

Run nextjs application in the background

I have an application built with nextJs and this application should work on a local server (Windows).
my customer told me that he needed this application to work in the background after searching I found that I needed to use a package called pm2 and when I used it gives me an error and I found that I needed to make some configurations for it and I can't found any helping resources, please help 💔
I found that to run nextJs application in the background you will need a custom configuration
you need to download the pm2 globally in your system
create a file with the name ecosystem.config.js in the root folder next to the package.json file
you need to put your config data in this file which would be something like this
module.exports = {
apps: [
{
name: "inventory_test",
script: "node_modules/next/dist/bin/next",
args: "start -p 3333", //running on port 3000
watch: false,
},
],
};
you should set the name as the name you want to see when you check
the list of pm2
the problem will be solved when you set the script as I did in the code above to be more precise the default run of pm2 is to go to the node js folder in the system and try to make start for the application using npm directly but this is the problem we need to make it use the node runner from the nextjs itself or something like this so we change the script as above
after that, we set the arguments that we should run after the npm and in my example is the arg start and choose the port for our application too
and now we make our config
NOTES
you should make build before you start the application
to run the project you will open the folder of the project in the terminal || cmd || cmder and run the command pm2 start ecosystem.config.js

I can't access my .env variables!! nodeJS

Server is running on PORT: undefined in undefined mode.
MongoDB database connected with HOST: localhost
used to get Server is running on PORT: 4000 in development mode. and suddenly it's undefined for both and every variable in my .env, like cloudinary .. stripe ...
You can add this snippet before starting mongoDB to see what's getting to your node process env variables:
console.log(process.env)
Also note that you can pass inline environment variables to node process like this:
PORT=4000 node ./my-script.js
To automatically load .env files, you might be using this package: https://github.com/motdotla/dotenv so be sure that you have all packages installed (npm install or yarn install, depending on what you are using).
One of the first things you should pay attention to is the file path. Check where your .env file is located in the project. If it is in the root of the project, there is no need to do anything. Otherwise, you must specify the path to the .env file.

Set webpack environmental variable for apache2

Is it possible to fetch a webpack envvar outside the node / js scope?
I'm developing with vueJS and TYPO3 and want to load the JS files from node server, when it runs. Otherwise, I want to load the built JS files from the project folder.
TYPO3 has conditions for file loading, in dependence of an Apache envvar.
SetEnv TYPO3_CONTEXT Development
Questions:
Is it possible to set an apache2 envvar when launching a node devserver by webpack?
If not, is it possible to hook in that process and write a temporary htaccess file with the var in it, place it in a specific directory and delete it, when I stop the node server?
Background:
In TYPO3, js and css includes are configured by TypeScript.
In Prod Env:
page.includeJSFooter.app = path/to/build/name.js
This load can be changed by condition:
[applicationContext = Development]
page.includeJSFooter.app = http://192.168.100.38:8080/app.build.js
[end]
Now I want to set this context, as soon as I start my node dev server that builds the files on the fly:
webpack-dev-server --open --config webpack.dev.js
I doubt this is possible but as a solution I would propose to implement a custom condition (described here in the manual https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/TypoScriptSyntax/TypoScriptParserApi/CustomConditions.html) and check within that e.g with a request if the node server is responding.
Hi Georg and all others,
a custom condition would be a solution, but I don't want to influence the production environment (e.g. pinging for a response from node server).
I made it with two shell scripts, executed by webpack-shell-plugin.
The first script is triggered "onBuildStart", looking for an .htaccess file and adding
SetEnv TYPO3_CONTEXT Development/NodeServer
to the end of the file.
Unfortunately there is no event fired when the node server is shut down. As a workaround, I added a new npm run script, which just executes a shell script to delete that line from .htaccess again.
I guess there is a besser solution, but at this time, it solves my (very little) problem to avoid changing the JS includes every time I start to develop.

Unexpected token import - using react and node

I'm getting an error when running my react app: Uncaught SyntaxError: Unexpected token import
I know that there are a plethora of similar issues on here, but I think mine is a little different. First of all, here is the repository, since I'm not sure where exactly the error is: repo
I'm using create-react-app, and in a seperate backend directory I'm using babel (with a .babelrc file containing the preset es2015). The app worked fine until I added another file in a new directory in the backend folder (/backend/shared/validations/signup.js).
I was using es6 before that too, and it was working perfectly fine. First I thought it was some problem with windows, but I cloned the repo on my Ubuntu laptop and I'm getting the same error there.
Some things I tried already:
Move the entire folder from /backend to the root folder
Move just the file (signup.js) just about everywhere
So no matter where the file is, the error stays the same. If I remove the entire file and all references to it the app works again.
I think this error is pretty weird, considering I'm using es6 everywhere else in the app without trouble. It would be great if anyone could help me with this error.
edit: If you want to test this on your own machine just clone the repo and run npm start in the root folder (and also npm start in the backend folder, but that isn't required for the app to run, or for the error to show up).
That's what's happening:
Your webpack is set to run babel loader only in the src folder (include directive).
To change that, one approach is:
1) extract webpack configuration with npm run eject (don't know if there is another way to override settings in react-create-app). From the docs:
Running npm run eject copies all the configuration files and the
transitive dependencies (Webpack, Babel, ESLint, etc) right into your
project so you have full control over them.
2) in config/paths.js, add an appShared key, like that:
module.exports = {
/* ... */
appSrc: resolveApp('src'),
appShared: resolveApp('backend/shared'),
/* ... */
};
3) in config/webpack.config.dev.js, add the path to the babel loader, like that:
{
test: /\.(js|jsx)$/,
include: [ paths.appSrc, paths.appShared ],
loader: 'babel',
/* ... */
},
It works now!

nconf not loading from file on heroku

I have switched over from using dotcloud to heroku. I am using nconf for my configuration. I have it setup that it first grabs from the environment variables, and if not there, then it grabs from the config.json file. On localhost this is working fine. For my build number, I store it in the config file, not in the environment variable, so that I can set it on push and then not have to change the environment.
app.coffee
nconf.argv().env().file file: "./config.json"
config.json
{
"APP_BUILD_NUMBER": "1.0.0"
}
If i run this locally or on dotcloud, nconf correctly passes 1.0.0 if I do
nconf.get("APP_BUILD_NUMBER")
but, on heroku, it returns undefined. If I do set it in the environment variables, then it does work. I am wondering what I am doing wrong.
Try removing the './' portion of the path:
nconf.argv().env().file file: "config.json"
If that doesn't work, try
nconf.argv().env().file file: __dirname + "/config.json"
I would recommend running heroku run bash then entering the Node REPL and trying multiple paths until you figure out what is different. Making a change and then waiting for a push is too tedious a debug cycle. I suspect your issue is around the path, or perhaps not unsetting the environment variable.

Resources