问题
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?
回答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\""
},
回答2:
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.
回答3:
This is what worked for me for Windows:
"scripts": {
"db": "path_to_mongod.exe --dbpath path_to_mongodb-data_folder"
}
来源:https://stackoverflow.com/questions/49686131/starting-mongodb-server-using-npm-scripts