node_modules的脚本路径(Laravel)

What is the path to node_modules in laravel project?

<script src="???????" >

My files structure is pretty standard, i have node_modules directory in my root directory.

Laravel provide several helper to get path of public directory, app directory, storage directory and base directory. It will help to store files, read file from path and also for other from controller, view files, model etc.

You can see as bellow how to get path of that directory using helper one by one.

public_path(); // Path of public/

base_path(); // Path of application root

storage_path(); // Path of storage/

app_path(); // Path of app/

That is not how you go about including javascript for your laravel project.

Here is a very short introduction to building javascript with laravel mix: First - Create the file resources/assets/js/main.js (or if you are on laravel 5.7 it should be resources/js/main.js

In your webpack.mix.js file in your root you add the following:

mix.js('resources/assets/js/main.js','public/js/main.js')

It will then automaticly build your file when you run npm run dev

Before we are ready we need our page to load the correct file. There is a helper function for that called mix(). Use it and include this in your HTML file.

<script src="{{mix('js/main.js')}}"></script>

Now we are ready to start building using our new echosystem

Lets for example include momentjs in our project:

npm install moment

And as you have seen it will not put it under node_modules. But we want to use it on our page - lets open up our main.js file and add the following

import moment from 'moment'
window.moment = moment

and now moment will be available from the moment variable. Its a very simple tutorial on how to build js with laravel mix.

You can read more about it here: https://laravel.com/docs/5.7/mix

Happy hacking!