nodejs forever: start application - node.js

I am running my web app with npm start but now I need to deploy it to Digital Ocean which means that I'll have to use forever to start my app and I can't figure this out...
I tried to run it as forever npm start but it returns:
error: Cannot start forever
error: script /root/saleseng/OP Apps Platform/server/npm does not exist.
This is my scrips object from package.json:
"scripts": {
"start": "nodemon --watch src --exec babel-node -- src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
}

You will need to pass the name of .js file to forever:
forever start src/index.js
This will start the script with forever. It could be monitored with
forever logs scriptId -f
forever list will fetch you the scriptId
UPDATE:
Transpiling to ES5:
To use babel have the following dependencies installed:
"dependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
}
Then create a .babelrc in the root of your project having the following:
{
"presets": ["es2015","stage-0"]
}
Now to finally do a transpile, run:
babel myInputDirectory --out-dir myOutputDirectory

Related

Deploy to Node App Azure App Service tsc not recognized as command

I am trying to deploy a nodejs app onto Azure App Service. I did the basics of deploying it, but it's failing to run. It seems it is designed to run "node run.js" commands rather than "npm run start".
I'm playing in the console, and if I try to run npm run start manually, I get a series of errors tied to build. Basically:
'tsc' is not recognized as an internal or external command
I'm wondering if there's something really obvious here about how tsc (and others) can be added to path. I have to admit, I'm not particularly well versed in using Azure or Node for that matter. Any help would be very much appreciated! Thanks!
This is the package.json file:
{
"name": "test-scraper",
"version": "0.1.1",
"description": "",
"main": "dist/main.js",
"scripts": {
"build": "tsc",
"build:dev": "tsc --watch",
"prestart": "npm run build",
"start:dev": "nodemon",
"start": "pm2 start dist/src/main.js --node-args=\"-r ./tsconfig-paths-bootstrap.js\" && pm2 monit",
"stop": "pm2 delete main"
},
"author": "",
"license": "MIT",
"devDependencies": {
"#types/lodash": "^4.14.161",
"#types/node": "^14.11.8",
"#types/puppeteer": "^3.0.2",
"nodemon": "^2.0.4",
"prettier": "^2.1.2",
"typescript": "^4.0.3"
},
"dependencies": {
"axios": "^0.20.0",
"discord-webhook-node": "^1.1.8",
"lodash": "^4.17.20",
"messaging-api-telegram": "^1.0.1",
"playwright-firefox": "^1.4.2",
"pm2": "^4.5.0",
"tsconfig-paths": "^3.9.0",
"winston": "^3.3.3"
}
}
Run this command locally for installing typescript, because your code is compiled with the tsc command.
npm install -g typescript
Mostly I was following this tutorial.
Add the tsc command to package.json and add the dependencies:
"build": "tsc --project ./"
...
"devDependencies": {
"#types/express": "^4.17.9",
"#types/node": "^14.14.20",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
},
Here is my file structure:
I add a empty file.ts and add this scripts to the tsconfig.jason file:
"exclude": [ "src", "wwwroot" ],
"include": [ "file.ts" ]
Deploy through Azure Web App deployment center:
And the app build (tsc) run successfully:
if you are deploying to Production (NODE_ENV: production), devDependencies will no longer being installed, and you need to edit your package.json and move typescript to dependencies.
Source

How to run Nodemon with TypeScript in debug mode in WebStorm

I have a very basic Express server setup with NodeJS/Express + Nodemon. I'm using WebStorm as my IDE.
When running the debug/inspect script, my application is throwing a compile-time error. Following the instructions here, and cognizant of the post here, my (simplified) package.json looks like this:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/app.ts",
"debug": "nodemon --inspect=127.0.0.1:9229 src/app.ts"
},
"devDependencies": {
"#types/express": "^4.17.9",
"#types/node": "^14.14.19",
"nodemon": "^2.0.6",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
When I run npm run debug I receive the following error:
I've also tried the following, all with the same result:
"debug": "nodemon --inspect src/app.ts"
"debug": "nodemon --inspect=0.0.0.0:9229 src/app.ts"
Any ideas why this error is occurring?
The command npm run dev does not generate an error. Upon further investigation, nodemon automatically substitutes ts-node when targeting a .ts file. Ts-node, in turn, recommends debugging by registering ts-node and running the server with the node command.
If you need to use advanced node.js CLI arguments (e.g. --inspect),
use them with node -r ts-node/register instead of the ts-node CLI.
You can modify your script as follows:
"debug": "nodemon --exec \"node --inspect-brk=0.0.0.0:9229 --require ts-node/register src/app.ts\""
to make sure that the --inspect-brk is passed to Node.js and not to ts-node

A2Hosting: npm works in terminal, but not for cron worker

I am using A2hosting and trying to configure cron worker, but no luck yet.
I followed this article to install node and npm, which is basically list of commands below
cd ~
wget https://nodejs.org/dist/v12.9.1/node-v12.9.1-linux-x64.tar.xz
tar xvf node-v12.9.1-linux-x64.tar.xz
mv node-v12.9.1-linux-x64 nodejs
mkdir ~/bin
cp nodejs/bin/node ~/bin
cd ~/bin
ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm
I have tried multiple commands
npm run --prefix ~/cloudflare-upload-tool start which produces /bin/bash: npm: command not found
and
~/nodejs/bin/npm run --prefix ~/cloudflare-upload-tool start which produces /usr/bin/env: node: No such file or directory
Both commands run fine in terminal, could you tell what are my options to fix this?
package.json
{
"name": "cloudflare-upload-tool",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "env-cmd node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.20.0",
"cloudflare": "^2.7.0",
"env-cmd": "^10.1.0",
"form-data": "^3.0.0",
"fs": "0.0.1-security",
"jsonwebtoken": "^8.5.1",
"path": "^0.12.7",
"tus-js-client": "^2.2.0"
}
}
Support told that this approach is not supported. Ended up creating node js app using UI, stopping it as we don't need to expose it and using command for cron worker below as suggested in other article
source /home/<USERNAME>/nodevenv/cloudflare_upload_tool/10/bin/activate && cd /home/<USERNAME>/cloudflare_upload_tool && npm run start

how to use forever with npm

i am trying to run this command in my debian 10 vps forever start npm start but i got this error in the terminal,
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: npm
(node:23858) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:23858) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
error: Cannot start forever
error: script /root/node_projects/tiktok/backend/npm does not exist.
Here is my package.json file
{
"name": "auth-graphql",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"engines": {
"node": "10.11.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "node -r esm ./src/index.js",
"start": "nodemon -r esm ./src/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
...
"esm": "^3.2.0",
...
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-2": "^6.24.1",
"nodemon": "^1.18.4"
}
}
i tried
forever start esm ./src/index.js
forever start -c "npm start" ./
forever start -c "npm start" /path/dir/
forever --sourceDir /path/dir/ -c "npm start" /
and i also got almost the same error, Please I need help, How can i solve this problem
try this command:
forever start -c "npm start" ./
running your application's specific directory:
forever start -c "npm start" /path/dir/
or use this command:
forever --sourceDir /path/dir/ -c "npm start" /
I've had similar problems and I think it may be some sort of incompatibility with NPM/Node versions (see discussion here.
I switched to https://github.com/Unitech/pm2 instead and things started working immediately.
I don't know the solution but. The problem is related to a circular dependency. Node version 14+ is no longer supports circular dependency. Example
In fileA
var fileB = require(fileB);
In fileB
var fileA = require(fileA);

build before restarting the node.js (express) server using Nodemon

I am using nodemon to watch the changes to the server files and restart the server. It works fine when the script is ec5
nodemon ./server.js
but I want to write the script in ec6 and compile using babel doing the same thing but building before the server is restarted.
package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel src --out-dir dist",
"serve": "npm run build && node dist/index.js",
"serve-dev": "nodemon dist/index.js"
},
"dependencies": {
"babel-preset-env": "^1.7.0",
"express": "^4.16.4"
},
"devDependencies": {
"#babel/cli": "^7.0.0-rc.1",
"#babel/core": "^7.0.0-rc.1",
"#babel/node": "^7.0.0-rc.1",
"#babel/preset-env": "^7.0.0-rc.1",
"nodemon": "^1.18.3"
}
Run with babel-node. (I can see it's already installed as dev dependency)
"serve-dev": "nodemon --exec babel-node src/index.js"
(assumed that src/index.js is your entry point of your app)
I think nodemon has built-in support for this now.
Create a config file: https://github.com/remy/nodemon#config-files
Watch for the restart event and put your build script stuff there: https://github.com/remy/nodemon#triggering-events-when-nodemon-state-changes

Resources