Add Javascript src path to Symfony Webpack Encore

Viewed 92

I have the following javascript links and other custom js files:

<script src="/assets/js/popper.min.js"></script>
<script src="/assets/js/moment.min.js"></script>
<script src="/assets/js/custom.js"></script>

How do I add them to my app.js webpack file?

I tried:

import './js/popper.min.js';
import './js/moment.min.js';
import './js/custom/js';

Then when i run npm run dev , i get the following error:

Module build failed: Module not found:
"./assets/js/moment.min.js" contains a reference to the file "./locale".
This file can not be found, please check it for typos or update it if the file got moved.

How do i solve this?

Thanks

1 Answers

When you use webpack Encore you have to :

  1. Install the dependecy thans to a package manager like npm or yarn with for examaple npm install popper --save or yarn add popper
  2. Add the dependency in your app.js like this : import 'popper.js'; and that's it ! It the same way for Moment or any other JS library.

NB : if you want to include a custom JS file you have to put it in the asset folder (where you want) and include it as you did : import './js/custom/js';.

Related