简体   繁体   中英

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`

 > parse-server-example@1.4.0 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": "*"
  },

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.'

Add moment to your package.json file and let Heroku install it, then just do var moment = require('moment.js');

Just add this line "moment": "^2.14.1" to the dependencies section of your package.json file.

In general you want to add your dependencies using npm install --save <libraryname> so it would be added to the package.json file.

You can do that; npm install --save moment as an alternative solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM