简体   繁体   English

NPM 在 laravel 9 中运行开发

[英]NPM RUN DEV in laravel 9

enter image description here I have a problem when I create a new project in laravel and I want to build authintication...this is my cmd command:在此处输入图像描述我在 laravel 中创建新项目时遇到问题,我想构建身份验证...这是我的 cmd 命令:

laravel new laravel2030
cd laravel2030
composer require laravel/ui 
php artisan ui bootstrap --auth
npm install 
npm run dev 

在此处输入图像描述

In Laravel 9 this command works too.在 Laravel 9 中,此命令也有效。

vite build --watch

https://vitejs.dev/guide/build.html#rebuild-on-files-changeshttps://vitejs.dev/guide/build.html#rebuild-on-files-changes

I think the problem in package.json file我认为 package.json 文件中的问题

You have to change "dev": "vite" in package.json您必须更改 package.json 中的 "dev": "vite"

    "scripts": {
    "dev": "vite",
    "build": "mix build"
    }

to be "dev":"mix" like this像这样“开发”:“混合”

    "scripts": {
    "dev": "mix",
    "build": "mix build"
    }

and run npm run dev again.并再次运行 npm run dev。

now laravel started to implement Vite instead of mixin现在 laravel 开始实现 Vite 而不是 mixin

so you need to revert back to mix which I don't recommend所以你需要恢复到我不推荐的混音

by intsalling laravel mix通过安装 laravel mix

npm i laravel-mix

then update the scripts section in package.json to be like this然后将 package.json 中的脚本部分更新为这样

"scripts": {
    "dev": "npm run development",
    "development": "mix",
    "watch": "mix watch",
    "watch-poll": "mix watch -- --watch-options-poll=1000",
    "hot": "mix watch --hot",
    "prod": "npm run production",
    "production": "mix --production"
},

OR second option update your laravel ui view files to be compatible with vite which I think is a matter of time till laravel ui package make an update to implement this by default You can follow this tutorial starting from step 5(if you already have a project) or follow from first step to create new project and see how you can fix this或者第二个选项更新你的 laravel ui 视图文件以与 vite 兼容,我认为 laravel ui 包进行更新以默认实现这一点是时间问题你可以从第 5 步开始遵循本教程(如果你已经有一个项目) 或从第一步开始创建新项目,看看如何解决这个问题

https://techvblogs.com/blog/how-to-install-bootstrap-5-in-laravel-9-with-vite https://techvblogs.com/blog/how-to-install-bootstrap-5-in-laravel-9-with-vite

you'll just need to edit in 4 files I think to fix this你只需要编辑 4 个文件我认为可以解决这个问题

in laravel 9 update we use Vite instead of mix then you need to do some configration:在 laravel 9 更新中,我们使用 Vite 而不是 mix 那么你需要做一些配置:

Import vite.config.js Bootstrap 5 Path导入 vite.config.js Bootstrap 5 路径

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import path from 'path'

export default defineConfig({
    plugins: [
        laravel([
            'resources/js/app.js',
        ]),
    ],
    resolve: {
        alias: {
            '~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap'),
        }
    },
});

Update bootstrap.js We need to use import instead of require.更新 bootstrap.js我们需要使用 import 而不是 require。

import loadash from 'lodash'
window._ = loadash

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

import axios from 'axios'
window.axios = axios

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

/**
 * Echo exposes an expressive API for subscribing to channels and listening
 * for events that are broadcast by Laravel. Echo and event broadcasting
 * allows your team to easily build robust real-time web applications.
 */

// import Echo from 'laravel-echo';

// window.Pusher = require('pusher-js');

// window.Echo = new Echo({
//     broadcaster: 'pusher',
//     key: process.env.MIX_PUSHER_APP_KEY,
//     cluster: process.env.MIX_PUSHER_APP_CLUSTER,
//     forceTLS: true
// });

Import Bootstrap 5 SCSS in JS Folder Now you need to import bootstrap 5 SCSS path in you resources/js/app.js or resources/js/bootstrap.js在 JS 文件夹中导入 Bootstrap 5 SCSS现在您需要在 resources/js/app.js 或 resources/js/bootstrap.js 中导入 bootstrap 5 SCSS 路径

resources/js/app.js资源/js/app.js

import './bootstrap';

import '../sass/app.scss'

import * as bootstrap from 'bootstrap'

Replace mix() with @vite Blade directive将 mix() 替换为 @vite Blade 指令

<link rel="stylesheet" href="{{ mix('css/app.css') }}">
<script src="{{ mix('js/app.js') }}" defer></script>

to this:对此:

@vite(['resources/js/app.js'])

views/layouts/app.blade视图/布局/app.blade

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'Laravel') }}</title>

    <!-- Fonts -->
    <link rel="dns-prefetch" href="//fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">

    @vite(['resources/js/app.js'])

</head>
<body>
    <div id="app">
        <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
            <div class="container">
                <a class="navbar-brand" href="{{ url('/') }}">
                    {{ config('app.name', 'Laravel') }}
                </a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <!-- Left Side Of Navbar -->
                    <ul class="navbar-nav me-auto">

                    </ul>

                    <!-- Right Side Of Navbar -->
                    <ul class="navbar-nav ms-auto">
                        <!-- Authentication Links -->
                        @guest
                            @if (Route::has('login'))
                                <li class="nav-item">
                                    <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
                                </li>
                            @endif

                            @if (Route::has('register'))
                                <li class="nav-item">
                                    <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
                                </li>
                            @endif
                        @else
                            <li class="nav-item dropdown">
                                <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                                    {{ Auth::user()->name }}
                                </a>

                                <div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                                    <a class="dropdown-item" href="{{ route('logout') }}"
                                       onclick="event.preventDefault();
                                                     document.getElementById('logout-form').submit();">
                                        {{ __('Logout') }}
                                    </a>

                                    <form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
                                        @csrf
                                    </form>
                                </div>
                            </li>
                        @endguest
                    </ul>
                </div>
            </div>
        </nav>

        <main class="py-4">
            @yield('content')
        </main>
    </div>
</body>
</html>

Running Vite Command to build Asset File运行 Vite 命令构建资产文件

npm run build
php artisan serve

source: https://techvblogs.com/blog/how-to-install-bootstrap-5-in-laravel-9-with-vite来源: https://techvblogs.com/blog/how-to-install-bootstrap-5-in-laravel-9-with-vite

there are several files with the extension package.json and I cannot distinguish which file I want to modify I want the answer soon please有几个扩展名为 package.json 的文件,我无法区分我想修改哪个文件,请尽快给我答复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM