I have an JS file in a npm enviornment and it and its associated package.json is given below. I receive the following ERROR, while running the file 'app.js'.
TypeError: Cannot read property 'setDefault' of undefined
I am not able to access moment.tz() as it is getting the value of undefined.
app.js
import moment from "moment";
moment.tz.setDefault(moment.tz.guess());
console.log(moment().tz());
package.json
"name": "folder_name_here",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"moment": "^2.29.1",
"moment-timezone": "^0.5.33"
}
}
You should import from moment-timezone
rather than moment
.
import moment from "moment-timezone";
Also, there's very little reason to do moment.tz.setDefault(moment.tz.guess());
because the default time zone is already understood by Moment. If that's your only reason for using Moment-Timezone, then don't - just use Moment by itself.