Custom start-up command for Azure Node.js Web app (ES2015/babel)? - node.js

I am migrating an ES2015 node.js application from Heroku to Azure.
The current start-up command running on Heroku is
"start": "./node_modules/babel-cli/bin/babel-node.js index.js"
However, on Azure I am getting
Invalid start-up command "./node_modules/babel-cli/bin/babel-node.js index.js" in package.json. Please use the format "node <script relative path>".
Which indicates that Azure only supports vanilla node for npm start.
I'm aware that running babel-node on production isn't ideal, but I was hoping for a straightforward migration.
Three options that would require a bit of re-architecting are:
Using a gulp workflow to precompile ES2015: https://github.com/christopheranderson/azure-node-es2015-example
Using a bash workflow to precompile ES2015: http://www.wintellect.com/devcenter/dbaskin/deploying-an-es6-jspm-nodejs-application-to-azure
Using babel-register ala [link in comments].
I suspect option 3 will be easiest but checking if anyone has come across a similar issue and managed to run babel-node directly in npm start on Azure.

According your issue, please modify your start npm script to node ./node_modules/babel-cli/bin/babel-node.js index.js on Azure Web Apps.
Here is the content in test package.json:
{
"name": "website",
"description": "A basic website",
"version": "1.0.0",
"engines": {
"node": "5.9.1",
"npm": "3.7.3"
},
"scripts": {
"start": "node ./node_modules/babel-cli/bin/babel-node.js index.js"
},
"dependencies": {
"babel-preset-es2015": "^6.6.0",
"babel-cli": "^6.0.0"
}
}
Meanwhile if you need a higher Node.js version, you can specify in package.json, refer to https://azure.microsoft.com/en-us/documentation/articles/nodejs-specify-node-version-azure-apps/ for more.

Just as what Gary said, you need to update your package.json using the command below.
"scripts": {
"start": "node ./node_modules/babel-cli/bin/babel-node.js index.js"
}

Related

Node.js app doesn't work when I try to host it via GCP deploy command. Error: Cannot find module 'express'

I have my NodeJS app wrote using TypeScript and based on the Express framework. I want to host it in GCP cloud with gcloud app deploy command.
So, first of all, I build my TS sources to JavaScript -is that the correct way of doing it?.
Then from the build (with JS source code) folder I'm trying to run npm start command and it works successfully and I'm also able to check it with Preview:
.
It works well. So far, so good.
Then I run gcloud app deploy from the build folder (with built to JS sources) and I didn't see any errors during deploy.
But afterward, I receive a 500 error on each request whenever I'm trying to reach the deployed app. I've taken a look into a log and I see next error:
Error: Cannot find module 'express'
What seems to be the problem?
I tried the next commands in the build folder:
npm install
npm install express --save
npm install -g express
sudo apt-get install node-express
Nothing works for me.
Here is my package.json file:
{
"name": "full-node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc",
"dev": "node -r ts-node/register ./src/server.ts",
"debug": "ts-node --inspect ./src/server.ts",
"start": "node build/server.js",
"prod": "npm run build && npm run start"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"ts-node": "^7.0.1",
"typescript": "^3.0.1"
},
"dependencies": {
"#types/lodash": "^4.14.116",
"body-parser": "^1.18.3",
"connect": "^3.6.6",
"cors": "^2.8.4",
"crypto": "^1.0.1",
"express": "^4.16.3",
"firebase-admin": "^6.0.0",
"lodash": "^4.17.10"
}
}
Any idea what I'm missed? Is this the correct way to deploy an app wrote with TypeScript to GCP cloud?
app.yaml:
# [START app_yaml]
runtime: nodejs8
# [END app_yaml]
since you are running gcloud app deploy from within the build folder,probably the package.json is not deployed as npm install is run first by app engine there is no way express could be missing.you can go to gcp console and under app engine view the version and then under diagnose you can view the source(the files that were actually deployed to app engine).keep in mind that this is only possible for the standard version and not the flex.I can see from your app.yaml you are using the standard.If some files are missing then go to your app root directory and in your .gcloudignore file you can ignore the files/folders you do not want to deploy.then run gcloud app deploy from within the root directory of your project
The problem was pretty simple. Seems like gcloud app deploy use npm run build & npm run start commands to start application somewhere inside. To host Node.Js wrote on TS first we need to build it to simple JS using tsc command. Then in the build folder rewrite package.json file to use correct commands. Look at my start command: "start": "node build/server.js". I was using it inside build folder as well so that's mean gcloud command was searching in /build/build/ folder. I've changed start command to "start": "node server.js" and then all works well.

Why is Heroku not detecting my start script?

I have a Node.js application on Heroku. To start it, I need index.jsto be executed. To do that, I added a package.json file with a start script.
As I read in Heroku Node.js Support:
First, Heroku looks for a Procfile specifying your process types.
If no Procfile is present in the root directory of your app during the build process, your web process will be started by running npm start, a script you can specify in package.json...
When I define the start script to be node index.js and I deploy my app to Heroku, I don't see any Dynos in the resources tab.
My code:
package.json
{
"name": "node.js app",
"version": "1.4.0",
"description": "A node.js app.",
"main": "index.js",
"repository": {
"type": "git",
"url": "Node.js repository"
},
"author": "Realex78",
"license": "MPL-2.0",
"dependencies": {
"npm package": "^1.0.0"
},
"devDependencies": {
"npm package": "^1.0.0"
},
"scripts": {
"start": "node index.js",
"poststart": "node scripts/poststart.js",
"restart": "node scripts/restart.js"
}
}
Make sure that you aren't using a Procfile, as Heroku gives it priority over package.json!
I.e. even though I had updated my startup script in package.json:
"scripts": {
"start": "node --harmony server.js"
},
I had forgotten that my Procfile was setup like this:
web: node server.js
Therefore my startup script in package.json was being ignored! The fix was to update my Procfile (to in this case include the --harmony param).
Note that I did not have to wait 24 hours for a successful deploy; it worked immediately.

How can I publish a nodJS app on aws?

I'm using a free tier version of aws cloud, and have some trouble with loading up a nodeJs Application. I 've tried to follow the documentation of aws but it didn't work unfortunately. I choose the Elastic BeanStalk, from the services, and create a new Application. I choose nodeJs for the preconfigured platform of course. I make a zip with my app, and I really pay attention, to not have any parent folder above package.json, and app.js . As you can see on the picture above, I have a folder "public" which contains additional folders (css,img, javascript code). When I try to upload the project, I get an error message.
"Failed to find package.json. Node.js may have issues starting. Verify package.json is valid or place code in a file named server.js or app.js." In the project view I've got a health status "degraded", and an other error code: "i-045ac359227e3a9ae" Severe
I 've tried many php projects on this server, and everything worked fine,but I can't make it work with Node. Could you help me out please?
package.json:
{
"name": "kutyapplikacio",
"version": "1.0.0",
"description": "Egy alkalmazás kutyáknak és gazdáiknak",
"main": "server.js",
"scripts": {
"test": "test",
"start": "node server.js"
},
"author": "sethdevelop",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.3",
"express-mailer": "^0.3.1",
"express-mysql": "0.0.1",
"express-validator": "^3.2.0",
"mysql": "^2.13.0",
"nodemailer": "^4.0.1",
"nodemailer-smtp-transport": "^2.7.4"
}
}
The error message you wrote is : "Failed to find package.json. Node.js may have issues starting. Verify package.json is valid or place code in a file named server.js or app.js."
From the folder I only see you have app.js file.
And in your package.json, there are server.js:
"main": "server.js",
"scripts": {
"test": "test",
"start": "node server.js"
}
Perhaps you should change server.js to app.js ?
Update
Since you mentioned they have the same name:
could it be you zipped the folder instead of zipped the files ?
Same problem has been discussed here:
https://forums.aws.amazon.com/message.jspa?messageID=477087
https://forums.aws.amazon.com/thread.jspa?threadID=130140

How to run npm start for reactjs even after closing terminal?

I've been following facebook tutorial with reactjs
I installed npm install -g create-react-app https://www.npmjs.com/package/react-scripts and created the app with it
But now I'm confuse how do I deploy my app to, for example, digital ocean? I know I can use pm2 to run a node server but I don't see how does npm start works as it just runs this in the package.json
{
"name": "hello-world",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-scripts": "0.7.0"
},
"dependencies": {
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"express": "^4.14.0",
"mongoose": "^4.6.8",
"react": "^15.3.2",
"react-dom": "^15.3.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
"start": "react-scripts start" what is it actually starting there?
also, it says that to build your app for deployment to use npm run build that just gives me a folder named build but I can't find anything to run node on.
I recommend reading the User Guide as it answers many common questions.
npm start starts the development server. It's only useful for development so you should never use it in production. This is printed in its output.
npm run build produces a static build folder with HTML, CSS, and JS files. You don't need Node to run it. It is static. To serve it, you would need to use any static file server. You can do this with Node, Python, Nginx, or any file server that can serve static files. This should also be printed in the command's output.
The deployment instructions vary depending on your hosting provider but basically you need to serve the build output from the server root, and you're set. If you use client-side routing you'll also want to configure your web server to serve index.html as a fallback for any path.
The user guide includes specific deployment instructions for different providers so check it out.

Watcher automation in Mocha

I would like to achieve automation of motcha --watcher feature using package.json file without globally installing mocha.
One of npm features is to allow add custom scripts into npm command. Previously I configured test runner successfully and I can type in bash now:
npm test
Everything works fine, so I would like also do something similar because
./node_modules/mocha/bin/mocha --watch app.js test.js"
is not too efective.
My goal is to run mocha watcher by typing in bash:
npm watch
Unfortunately watcher doesn't run - instead I see standard output of npm command without parameters. It looks like my custom script wasn't registered by npm.
Here is my actual package.json file
{
"name": "screencast",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha test.js", // works properly after typing 'npm test'
"watch": "mocha --watch app.js test.js" // Syntax looks ok, but command 'npm watch' d
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.10.6"
},
"devDependencies": {
"mocha": "^2.0.1",
"supertest": "^0.15.0"
}
}
Anyone had this issue before?
For a 'custom' script like watch, you have to do npm run watch instead of npm watch

Resources