cretueusebiu / laravel-nuxt

A Laravel-Nuxt starter kit.
https://laravel-nuxt.cretueusebiu.com
MIT License
1.15k stars 262 forks source link

no such file or directory dist/_nuxt #76

Closed Jimmylet closed 4 years ago

Jimmylet commented 5 years ago

Hello,

Whenever I try to launch a build of my application, I have this error at the end.

After invistigation, I think the problem comes from nuxt.config.js :

hooks: {
    build: {
      done (builder) {
        // Copy dist files to public/_nuxt
        if (builder.nuxt.options.dev === false && builder.nuxt.options.mode === 'spa') {
          const publicDir = join(builder.nuxt.options.rootDir, 'public', '_nuxt')
          removeSync(publicDir)
          copySync(join(builder.nuxt.options.generate.dir, '_nuxt'), publicDir)
          copySync(join(builder.nuxt.options.generate.dir, '200.html'), join(publicDir, 'index.html'))
        }
      }
    }
  }

As soon as I remove this code, the npm run build works. But of course, it does not generate the expected files so the application does not work.

Any idea ?

EDIT: If I copy/paste the files in /public, it works. But it's pretty boring.

I tested on two different Macs computers, a homestead and a Linux server. I have the same problem.

jnickzlim commented 5 years ago

I encounter the same issue when running npm run build as well, tried create dist/_nuxt folder does not work as well, it generate another error:

Error: ENOENT: no such file or directory, stat '/Applications/MAMP/htdocs/vue-app/dist/200.html'

Any idea on how to resolve this before i start using to develop application?

Mesolo commented 5 years ago

I am having the exact same issue. Running on windows 10 with npm

hiddehs commented 5 years ago

The root cause of this error is that there are no pages generated yet by the time of finishing building (build done hook). In fact the generator:done hook is also triggered when running yarn build for example.

Simply change the following build hook code from your nuxt.config.js

    build: {
      done (builder) {
        // Copy dist files to public/_nuxt
        if (builder.nuxt.options.dev === false && builder.nuxt.options.mode === 'spa') {
          const publicDir = join(builder.nuxt.options.rootDir, 'public', '_nuxt')
          removeSync(publicDir)
          copySync(join(builder.nuxt.options.generate.dir, '_nuxt'), publicDir)
          copySync(join(builder.nuxt.options.generate.dir, '200.html'), join(publicDir, 'index.html'))
        }
      }
    }

into

    generate:{
      done(generator){
        // Copy dist files to public/_nuxt
        if (generator.nuxt.options.dev === false && generator.nuxt.options.mode === 'spa') {
          const publicDir = join(generator.nuxt.options.rootDir, 'public', '_nuxt')
            removeSync(publicDir)
            copySync(join(generator.nuxt.options.generate.dir, '_nuxt'), publicDir)
            copySync(join(generator.nuxt.options.generate.dir, '200.html'), join(publicDir, 'index.html'))
        }
      }
    }

This change ensures that copying the dist files (for Laravel routing) will take place after the build files exists in dist/_nuxt

sergiooak commented 5 years ago

I will add some prints to @hiddehs response before submitting a PR...

    build: {
      done (builder) {
        // Copy dist files to public/_nuxt
        if (builder.nuxt.options.dev === false && builder.nuxt.options.mode === 'spa') {
          const publicDir = join(builder.nuxt.options.rootDir, 'public', '_nuxt')
          removeSync(publicDir)
          copySync(join(builder.nuxt.options.generate.dir, '_nuxt'), publicDir)
          copySync(join(builder.nuxt.options.generate.dir, '200.html'), join(publicDir, 'index.html'))
        }
        console.log("Before generate dist folder"); //
      }
    }

This will try to copy the content of the "dist" folder generate in Laravel root and move to "public" folder, but it fails case dist folder doesn't exist yet and throw this error: Error: ENOENT: no such file or directory, stat 'C:\laragon\www\mhb\dist\_nuxt'

For test purpose, I've commented out this entire block, runs npm build so it creates the "dist" on Laravel root, then I uncommented the block and run npm build again: image It worked, but you can see it runs before generate the dist folder.


So changing to this code block:

generate:{
      done(generator){
        // Copy dist files to public/_nuxt
        if (generator.nuxt.options.dev === false && generator.nuxt.options.mode === 'spa') {
          const publicDir = join(generator.nuxt.options.rootDir, 'public', '_nuxt')
            removeSync(publicDir) //Clear content from previous builds
            copySync(join(generator.nuxt.options.generate.dir, '_nuxt'), publicDir)
            copySync(join(generator.nuxt.options.generate.dir, '200.html'), join(publicDir, 'index.html'))
            removeSync(generator.nuxt.options.generate.dir) //Delete 'Dist' folder from Laravel root
        }
        console.log("After generate dist folder");        
      }
    }

image It just works! I've added removeSync(generator.nuxt.options.generate.dir) to delete original 'dist'


I'm making a PR right now...