Deploy Node application in Heroku with ES6 modules? - node.js

I am trying to deploy my Node.js ES6 modules-based application to Heroku, it works fine locally with npm start and also when I run heroku local web but unfortunately in the deployment it throws the error attached in the screenshot.
This is how my package.json looks like.
{
"name": "",
"private": true,
"type": "module",
"engines": {
"node": "12.6.0"
},
"scripts": {
"start": "node --experimental-modules index.js",
},
"dependencies": {
...
},
"devDependencies": {
...
}
}
I have also tried by upgrading Node to 13.2.0 as suggested in this thread https://stackoverflow.com/a/59399717/5763627, but nothing changes with Heroku.

Related

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.

Heroku: Bunch of npm errors on deployment

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

Configure local typescript compiler inside package.json

EDIT #1: seems like I have a working configuration, all suggestions to improve this are welcome though. See answer: https://stackoverflow.com/a/42269408/1155847
ORIGINAL QUESTION:
I'm currently trying to setup my environment so that my package.json's devDependencies typescript version will be used. What are some of the best practices for this, so that it is "editor unaware" and preferably can be used as an npm script, e.g.: npm run tscompile?
To be clear - I can get everything working when using npm install typescript -g - but then I'm relying on a global installed version, whic is not what I want - as we'll want to work in a team and set on a specific typescript version for each member before upgrading, so we're all on the same page.
I'm currently trying to set it up like this - yet npm then complains it doesn't recognize "node_modules" as an internal or external command... I presume I do have to pass the tsconfig.json to tsc as well, or at least give it the "working directory" - but I can't even get past launching tsc from my locally downloaded npm cache.
package.json
{
"name": "tswithnodejsgettingstarted",
"version": "1.0.0",
"description": "",
"main": "app/index.js",
"scripts": {
"start": "node app/index.js",
"tscompile": "node_modules/typescript/tsc"
},
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "2.1.6"
}
}
tsconfig.json
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"sourceMap": true,
"outDir": "app"
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
Ok... It seems like it was as simple as this (see below). Answering it here, for anyone else looking for the answer. Or please let me know if there are better solutions.
Configure the script like "tsc": "tsc" inside package.json. Then just run npm run tsc and it will use your tsc version you have installed locally, and discover your tsconfig.json of course. It doesn't use your global version - as I uninstalled that one - just entering tsc in the command line errors out.
E.g.:
Check the repo* where I was playing with this.
package.json
{
"name": "tscnodejsgettingstarted",
"version": "1.0.0",
"description": "",
"main": "app/index.js",
"scripts": {
"start": "npm run tsc && node app/index.js",
"tsc": "tsc"
},
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "2.1.6"
}
}
*repo: https://github.com/pluralsight-courses/typescript-fundamentals/tree/master/001-GettingStarted
You can also use the prestart script. By default it runs before the start command (see all the default scripts that you can set up here).
"scripts": {
"prestart": "npm run tsc",
"start": "node app/index.js",
"tsc": "tsc"
}

Having trouble with IBM BlueMix when integrating Twilio and Parse

I am having difficulties finding the correct module for Parse when deploying onto IBM BlueMix. I have tried the following:
var Parse = require('node-parse-api').Parse;
This works locally because I used npm-install for parse but I am unsure of how to include this module on IBM BlueMix.
Using cf push <appname> will deploy node-parse-api into Bluemix.
Add the node-parse-api in the package.json file for your node.js application
Example:
{
"name": "NodejsStarterApp",
"version": "0.0.1",
"description": "A sample nodejs app for Bluemix",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "4.12.x",
"cfenv": "1.0.x",
"node-parse-api": "0.3.8"
},
"repository": {},
"engines": {
"node": "0.12.x"
}
}

Specifying a start script - Node

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.

Resources