Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running js file in terminal

I'm trying to run this simple file in an empty folder

// time.js
const moment = require('moment')

function getDay() {
  var today = moment().day()
  console.log(today);
}

getDay()

using node time.js

But I get

Error: Cannot find module 'moment'

However I have run npm install -g moment and npm install moment.

What noob error am I doing?

like image 248
softcode Avatar asked Dec 19 '25 22:12

softcode


1 Answers

Just do the following commands on your console inside your folder:

npm init // just hit enter some times or follow the process
npm install moment --save
node time.js

Note: You could skip the npm init part but I wouldn't recommend it due to dependency control.

like image 63
cyr_x Avatar answered Dec 22 '25 10:12

cyr_x



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!