Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku cannot find `babel-core/register`

I am building a small url-shortener app using NodeJS. I am writing the code in ES6, using an index file that imports Babel and then my server:

require('babel-core/register');
require('./server.js');

This works absolutely fine on localhost and all my ES6 code works correctly.

However, when I successfully deploy to Heroku, I receive an application error with the following showing up in heroku logs --tail -a shortenthisurl:

2016-02-06T12:31:14.074071+00:00 heroku[slug-compiler]: Slug compilation started
2016-02-06T12:31:14.074080+00:00 heroku[slug-compiler]: Slug compilation finished
2016-02-06T12:31:13.936954+00:00 heroku[api]: Deploy f576f69 by [email protected]
2016-02-06T12:31:13.936991+00:00 heroku[api]: Release v17 created by [email protected]
2016-02-06T12:31:14.230327+00:00 heroku[web.1]: State changed from crashed to starting
2016-02-06T12:31:15.215417+00:00 heroku[web.1]: Starting process with command `node index.js`
2016-02-06T12:31:16.816525+00:00 app[web.1]: module.js:341
2016-02-06T12:31:16.816534+00:00 app[web.1]:     throw err;
2016-02-06T12:31:16.816535+00:00 app[web.1]:     ^
2016-02-06T12:31:16.816536+00:00 app[web.1]:
2016-02-06T12:31:16.816541+00:00 app[web.1]: Error: Cannot find module 'babel-core/register'
2016-02-06T12:31:16.816541+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:339:15)
2016-02-06T12:31:16.816542+00:00 app[web.1]:     at Function.Module._load (module.js:290:25)
2016-02-06T12:31:16.816543+00:00 app[web.1]:     at Module.require (module.js:367:17)
2016-02-06T12:31:16.816544+00:00 app[web.1]:     at require (internal/module.js:16:19)
2016-02-06T12:31:16.816544+00:00 app[web.1]:     at Object.<anonymous> (/app/index.js:1:63)
2016-02-06T12:31:16.816545+00:00 app[web.1]:     at Module._compile (module.js:413:34)
2016-02-06T12:31:16.816546+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:422:10)
2016-02-06T12:31:16.816546+00:00 app[web.1]:     at Module.load (module.js:357:32)
2016-02-06T12:31:16.816547+00:00 app[web.1]:     at Function.Module._load (module.js:314:12)
2016-02-06T12:31:16.816547+00:00 app[web.1]:     at Function.Module.runMain (module.js:447:10)
2016-02-06T12:31:17.669897+00:00 heroku[web.1]: State changed from starting to crashed
2016-02-06T12:31:17.655186+00:00 heroku[web.1]: Process exited with status 1
2016-02-06T12:31:40.693975+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=shortenthisurl.herokuapp.com request_id=c2bffaba-4de6-4310-b535-4ff37ec637ad fwd="86.1.35.144" dyno= connect= service= status=503 bytes=

Here is my package.json:

{
  "name": "shortenthisurl",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "mocha --compilers js:babel-register -w",
    "start": "node server.js",
    "babel-node": "babel-node --stage 0 --ignore='foo|bar|baz'",
    "build": "babel -d lib"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel": "^6.3.26",
    "chai": "^3.5.0",
    "express": "^4.13.4",
    "lodash": "^4.2.1",
    "mocha": "^2.4.5",
    "mongoose": "^4.4.1",
    "request": "^2.69.0"
  },
  "devDependencies": {
    "babel-core": "^6.4.5",
    "babel-preset-es2015": "^6.3.13",
    "babel-register": "^6.4.3"
  }
}

Perhaps I am not correctly importing Babel. Do you have any advice for how I might going about getting this to work?

Here is a link to the full repo on Github: https://github.com/alanbuchanan/shortenthisurl

like image 442
alanbuchanan Avatar asked Feb 06 '16 13:02

alanbuchanan


1 Answers

The module was unavailable because it wasn't installed. Heroku installs only the packages in dependencies as these are required for the application to work.

From the Heroku documentation:

Npm reads configuration from any environment variables beginning with NPM_CONFIG. We set production=true by default to install dependencies only.

You can solve this problem by moving the babel-core module from the devDependencies to the dependencies list since you require the module to compile ES6 scripts to ES5.

like image 57
gnerkus Avatar answered Oct 24 '22 20:10

gnerkus