Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watch and reload api folder in Vue Nuxt

How to make nuxt watch for "non standard" directories and recompile / reload itself, and more specifically for dirs with additional server apis?

I have my express api in ~/api/. Since I reference the directory in serverMiddleware with '~/api', I would expect Nuxt to reload when I make some changes to the files in that dir, but it doesn't.

I'm simply using npm run dev that fires nuxt, I don't have any direct control on either nodemon (if it's used internally) or on webpack (that I'm pretty sure is). I tried adding watch: [ '~/api/*.js'], watch: [ '~/api/index.js'], watch: [ '~/api/**/*.js'] to build in nuxt.conf.js but with no luck.

like image 475
pistacchio Avatar asked Oct 09 '17 19:10

pistacchio


2 Answers

Simply use watch: ['api/**/*.js'] or watch: ['api'].

Contrary to what the docs show custom paths in build.watch are not normalized via Nuxt.resolveAlias(), which means that Nuxt prefixes like ~ and @ are not replaced by the actual paths.
Further down the line, when the watchers get created via chokidar, the checked path will still be "~/api" (which doesn't exist).

Bug report: https://github.com/nuxt/nuxt.js/issues/2983

like image 194
Fabian Iwand Avatar answered Sep 19 '22 22:09

Fabian Iwand


You can use nodemon to watch for changes inside your api folder. First install:

npm install --save-dev nodemon

or:

yarn add nodemon --dev

Then add this code inside of your package.json

{
  "scripts": {
    "dev": "nodemon --watch api --exec \"nuxt\"",
  },
}
like image 29
Hexodus Avatar answered Sep 20 '22 22:09

Hexodus