Search code examples
javascriptnode.jsherokumomentjsparse-server

Having problems using moment.js on Heroku


I used to have some cloud code on Parse using moment.js.

The first relevant line of code inside my main.js file was:

var moment = require('cloud/moment.js');

Now on Parse-Server I get the following error message in the logs:

Starting process with command `npm start`

 > [email protected] start /app
 > node index.js

 module.js:471
     throw err;
     ^
Error: Cannot find module 'moment.js'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/app/cloud/main.js:1:76)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

Can someone tell me what is happening?

After reading quite a bit on the net I have tried a few things, like:

var moment = require('./cloud/moment.js');
var moment = require('moment.js');

All failed.

I am sure that the file moment.js is present under my cloud folder. I have also tried to install a new version of moment(from: https://momentjs.com/), but to no avail.

Checking the package.json file, it contains the following:

  "dependencies": {
    "express": "~4.11.x",
    "kerberos": "~0.0.x",
    "moment": "^2.17.1",
    "parse": "~1.8.0",
    "parse-server": "*"
  },

Solution

  • The error is likely caused by this line:

    var moment = require('moment.js');

    It should be:

    var moment = require('moment');

    The first line looks for a specific file in your file tree, rather than a package. The second line looks for the package 'moment.js.'