0

Currently when developing locally I run my MongoDB and start Nodemon. I do this in vscode terminals but I never remember the commands so I have a txt file that I copy/paste from every time. I was wondering if it's possible to set a script in my package.json that I could call to run these for me.

I've tried to use the answer from how to execute powershell ps1 scripts from package.json "scripts"? as an example but since I'm not able to figure it out. The two things I'm currently doing are:

To start MongoDB

& 'C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe'

To start Nodemon. I also set the node_env to development here

$env:NODE_ENV="development"
nodemon server/server.js

I've been doing it the current way for months, so it isn't that big of a deal. I'm just wondering if there is an easier way to start these up without copy/pasting the commands.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
Tyler C
  • 3
  • 6
  • 1
    I would strongly suggest that you ***don't*** actually launch the `mongod` process with each application start ( and especially restart ). Instead it's *far more sane* to simply spawn this process ( preferably in a background process ) **once**, and possibly preferably on your computer startup as a service. Unless you really have some need for "multiple instances" ( and I don't see any `--dpbath` or `--port` options, so you likely do not ) then that's what you should be doing. Consult the documentation on installing as a Windows Service instead. – Neil Lunn Apr 14 '18 at 08:42
  • For reference: [Configure a Windows Service for MongoDB Community Edition](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/#configure-a-windows-service-for-mongodb-community-edition) – Neil Lunn Apr 14 '18 at 08:44
  • I will look into that, thank you! – Tyler C Apr 14 '18 at 08:51

1 Answers1

0

I've just found this question and using the question you linked this was my solution:

Create two scripts:

"scripts": {
  "powershell": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command ./run_dev.ps1",
  "start": "nodemon server/server.js"
}

Create run_dev.ps1 in the same directory as my package.json with the following line:

$env:NODE_ENV="development"; npm start

So when you run npm run powershell it'll run the powershell file and nodemon all within the same terminal with the env correctly set - running fine within my VS Code terminal. I know the question's old but hopefully this helps others that found their way here.

RyanHx
  • 411
  • 3
  • 10