0

I am starting mongodb server using cmd with the following command:

"C:/Program Files/MongoDB/Server/3.6/bin/mongod.exe"

It starts the mongodb server perfectly.

Now I need to do it via npm script. Below is my package.json file:

package.json:

"scripts": {
"ng": "ng",
"prestart": "start /b \"C:/Program Files/MongoDB/Server/3.6/bin/mongod.exe\"",
"start": "ng serve | node server.js",
"build": "ng build --prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
}

Now when I hit npm start, it doesn't starts the mongodb server.

I also included the path "C:/Program Files/MongoDB/Server/3.6/bin/mongod.exe" in environment variables (both user&system).

Can anyone help me to know where I was wrong?

4 Answers 4

1

I read help for the start cmd command and was able to run it in a separate named window:

"scripts": {
    "db": "start \"ToDo MongoDB instance\" /D \"C:/Program Files/MongoDB/Server/4.0/bin/\" mongod.exe --dbpath=\"C:/GIT/NodeJS/todo/db\""
},
Sign up to request clarification or add additional context in comments.

1 Comment

This is an elegant solution compared to prestart.
0

Try to escape the space in Program Files with a backslash, i.e. Program\ Files. I'm not a Windows guy but I hope this works there as well.

EDIT

As you figured out yourself, you also can enclose the path in (escaped) quotes, then resolving a directory name containing a space should work.

10 Comments

Thanks dr_barto. Please see my edited post. Now a new command prompt opens, but not starting the mongodb server.
Do you need the start command in there? I think that's what causes the additional prompt; just try to directly call the .exe: "prestart": "\"C:/Program Files/MongoDB/Server/3.6/bin/mongod.exe --dbpath E:/nodejs/mongo/data/db\""
I tried. But it gives the following error: Invalid command: Files/MongoDB/Server/3.6/bin/mongod.exe. Also I have escaped the space between program and files with carrot(^) . still no luck.
Update: using start /b stops npm to create a new cmd window. But server didn't start.
Of course there is a way, as the name of the page I posted suggest ("running-local-mongodb-with-npm-scripts"). I'm doing it myself, but on a mac.. no problems at all. Sorry we didn't figure out a way for you. I'm sure it's just about a tiny detail. One final (and now I mean it!) thought, as a workaround you could write a good old batch file for starting the server and call that batch file from the npm script.
|
0

This is what worked for me for Windows:

"scripts": {
    "db": "path_to_mongod.exe --dbpath path_to_mongodb-data_folder"
}

Comments

0

For me worked on WINDOWS 10 with escape characters for backslash character and for quote character . In your case You used only for quote character. I think that may wary depending on OS and terminal you start with. I go with VSC terminal.

"scripts": {

"db": "C:\\Users\\<HERE_GOES_YOUR_USER>\\<PATH_TO_MONGO>\\mongodb\\bin\\mongod.exe --dbpath=\"c:\\Users\\<HERE_GOES_YOUR_USER>\\<PATCH_TO_DIR_WITH_DATA>\\data\" "

},

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.