Laravel Nwidart生成模块报错:缺失app.stub文件
问题描述
执行Nwidart模块生成命令时,生成resources/js/app.js文件失败,报错信息如下:
Generating file Modules/Page/module.json .............................................. 0ms DONE
Generating file Modules/Page/routes/web.php ........................................... 0ms DONE
Generating file Modules/Page/routes/api.php ........................................... 0ms DONE
Generating file Modules/Page/resources/views/index.blade.php .......................... 0ms DONE
Generating file Modules/Page/resources/views/layouts/master.blade.php ................ 0ms DONE
Generating file Modules/Page/config/config.php ........................................ 0ms DONE
Generating file Modules/Page/composer.json ............................................ 0ms DONE
Generating file Modules/Page/resources/js/app.js ...................................... 0ms FAILErrorException
file_get_contents(/vendor/nwidart/laravel-modules/src/Support/../Commands/stubs/js/app.stub): Failed to open stream: No such file or directory
at vendor/nwidart/laravel-modules/src/Support/Stub.php:106
对应的Stub.php代码片段:
* @return mixed|string */ public function getContents() { $contents = file_get_contents($this->getPath()); foreach ($this->replaces as $search => $replace) { $contents = str_replace('$' . strtoupper($search) . '$', $replace, $contents); }
已尝试更新包,但问题仍存在,模块生成后缺少服务提供者文件。
解决方法
1. 手动创建缺失的stub文件
- 进入项目的
vendor/nwidart/laravel-modules/src/Commands/stubs目录,新建js文件夹 - 在
js文件夹内创建app.stub文件,添加基础内容:
require('./bootstrap'); // Modules import './modules/Example';
2. 发布模块stub文件自定义修改
- 执行命令发布包的stub文件到项目目录:
php artisan vendor:publish --provider="Nwidart\Modules\Providers\ModulesServiceProvider" --tag="stubs"
- 发布后,stub文件会存放在
resources/modules/stubs目录,检查并确保js/app.stub存在,若缺失则手动创建 - 后续生成模块时,系统会优先使用项目目录下的stub文件
3. 检查包版本兼容性
- 确认当前Laravel版本与
nwidart/laravel-modules版本是否兼容(比如Laravel 10对应模块包^10.0版本) - 若版本不匹配,调整模块包版本:
composer require nwidart/laravel-modules:^10.0 # 根据实际Laravel版本修改版本号
4. 手动补全缺失的服务提供者文件
- 在
Modules/Page/Providers目录创建PageServiceProvider.php,内容示例:
<?php namespace Modules\Page\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Database\Eloquent\Factory; class PageServiceProvider extends ServiceProvider { /** * Boot the application events. * * @return void */ public function boot() { $this->registerTranslations(); $this->registerConfig(); $this->registerViews(); $this->registerFactories(); $this->loadMigrationsFrom(module_path('Page', 'Database/Migrations')); } /** * Register the service provider. * * @return void */ public function register() { $this->app->register(RouteServiceProvider::class); } /** * Register config. * * @return void */ protected function registerConfig() { $this->publishes([ module_path('Page', 'Config/config.php') => config_path('page.php'), ], 'config'); $this->mergeConfigFrom( module_path('Page', 'Config/config.php'), 'page' ); } /** * Register views. * * @return void */ public function registerViews() { $viewPath = resource_path('views/modules/page'); $sourcePath = module_path('Page', 'Resources/views'); $this->publishes([ $sourcePath => $viewPath ],'views'); $this->loadViewsFrom(array_merge(array_map(function ($path) { return $path . '/modules/page'; }, \Config::get('view.paths')), [$sourcePath]), 'page'); } /** * Register translations. * * @return void */ public function registerTranslations() { $langPath = resource_path('lang/modules/page'); if (is_dir($langPath)) { $this->loadTranslationsFrom($langPath, 'page'); } else { $this->loadTranslationsFrom(module_path('Page', 'Resources/lang'), 'page'); } } /** * Register an additional directory of factories. * * @return void */ public function registerFactories() { if (! app()->environment('production') && $this->app->runningInConsole()) { app(Factory::class)->load(module_path('Page', 'Database/factories')); } } /** * Get the services provided by the provider. * * @return array */ public function provides() { return []; } }
- 同时确保
Modules/Page/Providers目录下存在RouteServiceProvider.php,若缺失可参考其他模块的对应文件创建
内容的提问来源于stack exchange,提问作者Gagandeep Singh

