0

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

2
  • Why are you using mongo for execute nodejs file? Commented Jul 28, 2020 at 15:47
  • 1
    it might sound unreal but mongo does take a JavaScript file and execute it. docs.mongodb.com/manual/tutorial/… Commented Jul 28, 2020 at 16:28

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.