cli icon indicating copy to clipboard operation
cli copied to clipboard

`nuxt preview` runs commands generated by nitro + not declared as `peerDependencies`

Open wilsonpinto opened this issue 2 years ago • 7 comments

Environment

  • Operating System: Darwin
  • Node Version: v18.12.0
  • Nuxt Version: 3.3.1
  • Nitro Version: 2.3.1
  • Package Manager: [email protected]
  • Builder: vite
  • User Config: app, target, modules
  • Runtime Modules: modules/my-module/module.ts
  • Build Modules: -

Reproduction

Running following commands with target: 'static' in the nuxt.config.ts:


npm run generate

npm run preview

Describe the bug

We have Nuxt.js version 3.3.1 in our internal private registry, but when we attempt to run the npm run preview command, we encounter an error indicating that the serve dependency is missing from our private registry.

Should perhaps nuxt or nuxi have the peer dependency of serve?

Additional context

No response

Logs

npm ERR! code E404
npm ERR! 404 Not Found - GET https://private-registry/npm-internal/serve - Package 'serve' not found
npm ERR! 404 
npm ERR! 404  'serve@latest' is not in this registry.
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/wilson/.npm/_logs/2023-04-12T15_10_06_261Z-debug-0.log

 ERROR  Command failed with exit code 1: npx serve ./public

wilsonpinto avatar Apr 12 '23 15:04 wilsonpinto

@wilsonpinto When I do the above, I am asked if I wish to install serve image

Selecting yes, it installs and loads ok.

By your error, it suggests you have some custom .npmrc file or similar which is causing the npm lookup of serve to look in the wrong place.

DanielRivers avatar Apr 17 '23 17:04 DanielRivers

hi @DanielRivers thank you for checking this.

Due to security reasons in our company, we cannot access the public npm directly, and thus, we have custom properties for appointing always to the internal private registry. As a result, not all dependencies are accessible for installation unless they have passed the internal approval process.

My point here is that it would be helpful if serve were defined as a peer dependency in Nuxt. This would make it easier to understand which dependencies are required and avoid having to figure it out when running the development command npm run preview.

wilsonpinto avatar Apr 18 '23 10:04 wilsonpinto

@wilsonpinto as alternative / temporary fix you could serve the content with any other webserver, using OCI image (docker as example). or as you are on macOS you should already have python3 installed (python3 -m http.server --directory .output/client)

flapili avatar Apr 20 '23 07:04 flapili

@wilsonpinto as alternative / temporary fix you could serve the content with any other webserver, using OCI image (docker as example). or as you are on macOS you should already have python3 installed (python3 -m http.server --directory .output/client)

Thanks :) We end up to approve this dependency, but the process could be much smother...

wilsonpinto avatar Apr 20 '23 12:04 wilsonpinto

serve is only meant as a development dependency. You can use any package that allows you to serve static files.

It's used by nitropack, along with other provider-specific packages for other deployment targets.

Would it be sufficient if it were an optional peerDependency of nitropack? (We've also considered creating an unjs lib to serve static files which we might depend on directly.)

cc: @pi0

danielroe avatar May 24 '24 19:05 danielroe

h3 already exposes utils for serving static dir someone has to implement it in nuxi and bind it with fs :)

pi0 avatar May 27 '24 08:05 pi0

But I think nuxi rightly respects the command produced by nitro, right?

https://github.com/unjs/nitro/blob/944dcde2ab1591b1851b6a877d0566df5b9f91c2/src/presets/_static/preset.ts#L15-L17

Otherwise we'd need to write custom logic to override that command only for a 'static' build.

Could we have a nitropack preview command instead that uses those utils directly for static build?

danielroe avatar May 27 '24 09:05 danielroe