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.
Related
I have the following package.json and I'd like to run the bins "build" and "run":
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"bin": {
"build": "./bin/build.js",
"dev": "./bin/dev.js"
}
}
I've tried:
yarn run build
and I get
error Command "build" not found.
I've also tried:
yarn build
but the same thing happens:
error Command "build" not found.
It's propably not the right way to run bins. But then again, what is the right way to run bins with yarn?
Your package isn't installed.
When Yarn (and NPM) installs your package, it adds the commands under node_modules/.bin/, e.g. node_modules/.bin/build. Running yarn build would (if it doesn't find a matching script in the current package) look for a build in this .bin, then traverse upwards through the filesystem, looking for other node_modules/.bin/build's.
If your build script is only meant to be run while developing that specific package, add it as a script (see example here). It would more or less look like this:
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"scripts": {
"build": "node ./bin/build.js",
"dev": "node ./bin/dev.js"
}
}
Do not need relative path added:
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"scripts": {
"build": "build.js",
"dev": "dev.js"
}
}
The hashbang comment specifies the path to a specific JavaScript interpreter that you want to use to execute the script.
For example, helloWorld.js in ./node_modules/.bin:
#!/usr/bin/env node
console.log("Hello world");
You have a typo in your package.json. Where it says bin: it should say scripts:
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"scripts": { // <-- here
"build": "./bin/build.js",
"dev": "./bin/dev.js"
}
}
My app runs locally with no issues by its failing on Heroku
package.json
{
"name": "app name",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "nodemon app.js"
},
"engines": {
"node": "14.17.0"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mongoose": "^5.12.9"
}
}
Procfile
web:node app.js
its returning the below error
sh: 1: nodemon: not found
Any help on how to make this work?
The nodemon is installed globally on your machine that's why you are able to run it on your machine. When you are pushing it on Heroku it is showing the error because there is nothing like nodemon.
Also, Heroku doesn't need nodemon so just replace your start script:
node app.js
and change your Procfile to this:
web: npm start
Even I had the same issue.
You just have to remove nodemon from start script
It has to be iin this way;
"scripts": { "start": "src/index.js", "dev": "env-cmd -f ./config/dev.env nodemon src/index.js" }
I have a bunch of npm errors in my heroku logs. How can I begin to debug this? thanks!
screenshot
package.json
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines": {
"node": "8.1.1",
"npm": "5.0.3"
},
"scripts": {
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.0"
}
}
NPM ERR: missing script: start
It seems like you're missing the "start" script in your package.json file. That's where I would start looking. Heroku might assume you've set this script up beforehand. Here's a sample package.json file with the start script:
{
"name": "your-app",
"version": "1.0.0",
"main": "index.js",
"scripts": { // your npm scripts go here
"start": "nodemon index.js", // or whatever server package you're using
"lint:js": "node_modules/eslint/bin/eslint.js ./ ./**/*.js --fix; exit 0",
"lint:css": "node_modules/csslint/cli.js public/css/; exit 0",
"test": "NODE_ENV=test node_modules/mocha/bin/mocha"
},
"more": "settings below"
}
Here's a blurb from the Heroku website:
Specifying a start script
To determine how to start your app, Heroku first looks for a Procfile. If no Procfile exists for a Node.js app, we will attempt to start a default web process via the start script in your package.json.
The command in a web process type must bind to the port number specified in the PORT environment variable. If it does not, the dyno will not start.
For more information, see Best Practices for Node.js Development and Heroku Node.js Support.
https://devcenter.heroku.com/articles/deploying-nodejs
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"
}
The Node JS application is working fine using node command. (node app.js) I haven't as yet used the start / test scripts in package.json file (As a bonus: If you define scripts.start in your package.json file, you don't need a Procfile) like:
{
"name": "application-name",
"version": "0.1.0",
"description": "A Node.js app using Express 3",
"main": "app",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.0",
"ejs": "*"
},
"keywords": [
"node",
"heroku",
"express"
]
}
And I'm not getting any error but a message after issuing the command npm start
> application-name#0.1.0 start E:\nodeFileUpload
> node app.js
And then the application exits while if I issue node app.js it works fine.
I’m pretty sure that this is configuration related error but I can’t fathom out why.