Gopal
Gopal

Reputation: 111

How to execute nodejs file in shell script file

My first file is run.sh another file is script.js

run.sh

mongo --username --password --authenticationDatabase --host --port script.js

script.js

require('../config/config');
console.log("came here");

But this is not working. i got error

uncaught exception: ReferenceError: require is not defined : @import.js:1:1

Upvotes: 0

Views: 208

Answers (2)

lastr2d2
lastr2d2

Reputation: 3968

There is no module loader in the mongo shell environment.

Config.js sounds like a simple file that contains a mapping of configurations, if that's the case, you can try load()

// script.js
load("../../config.js")

If your config.js requires other modules, it's a different story and I wish you good luck...

Upvotes: 1

JSchirrmacher
JSchirrmacher

Reputation: 3364

The mongo command starts a MongoDB database. To execute your script, you need to run the command

node script.js

instead.

If you want to run both, the database and your script, you could start the database in the background first before running node script.js by adding a & after the database call.

Upvotes: 0

Related Questions