Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Chron's avatar
Level 6

include url source inside app.js

is there a way to add <script src="www.api.com/XX-XX-XX"> inside app.js instead of having multiple script tags?

0 likes
32 replies
Chron's avatar
Level 6

I'm currently importing the script before the <script src="{{ asset('js/app.js') }}"></script>.

// client-id from .env file
 <script src="https://www.api.com/{{ config('client-id') }}"></script>

How can I do this inside app.js?

Chron's avatar
Level 6

I tried this.

mix.scripts([
    "https://www.api.com/{{ config('client-id') }}",
]);

But I got an error after I compiled it.

Chron's avatar
Level 6

So it should be like this?

mix.scripts([
    "https://www.api.com/{{ process.env.client-id }}",
]);

I have client-id in my .env file.

Sinnbeck's avatar

Add CLIENTID to your env

mix.scripts([
    "https://www.api.com/{{ process.env.CLIENTID }}",
]);
Chron's avatar
Level 6

I already have it in .env

CLIENTID=XXXX-XXXX-XXXX

Then I did this:

mix.scripts([
    "https://www.api.com/api?client-id={{ process.env.CLIENTID }}",
]);

But I still got an error.

Chron's avatar
Level 6
AssertionError [ERR_ASSERTION]: mix.combine() requires a full output file path as the second argument.

...npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ development: `cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @ development script.
npm ERR! This is probably not a problem with npm. There is likely additional loggining output above.`
Chron's avatar
Level 6

Oh. I did it in webpack.config.js, that's why I'm getting an error.

Edit: I put it in app.js and I get mix is not defined. mix is already defined in webpack.config.js though.

Chron's avatar
Level 6

I put this inside webpack.config.js

mix.scripts([
    "https://www.api.com/api?client-id={{ process.env.CLIENTID }}",
]);

But it gives me an error when I compile it.

Sinnbeck's avatar

Try it without anything dynamic. Just the hard-coded url

If that does not work you can try showing your whole webpack file

Chron's avatar
Level 6

Here's the content of my webpack.mix.js:

const mix = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css')
   .scripts([
    'https://www.paypal.com/sdk/js?client-id=XX-XX-XX-XX-XX-XX&currency=USD&disable-card=visa,mastercard,amex'
]);

mix.webpackConfig({
    node: {
        fs: "empty"
    }
});

Sinnbeck's avatar

So it didn't work with a hard-coded url?

Sinnbeck's avatar

Oh silly me. Just remembered that the env needs to be prefixed with MIX_ (change the env file as well)

Also. Write your url like this in webpack

mix.scripts([
    "https://www.api.com/" + process.env.MIX_CLIENTID,
], 'public/js/all.js');
Chron's avatar
Level 6

It didn't work even if it is hard-coded.

Sinnbeck's avatar

Check my latest answer (now with missing argument)

Chron's avatar
Level 6

When I looked at public/js/all.js, it's empty. What does it do and is it intended to be empty?

Sinnbeck's avatar

That would be the output file. Did you remember to download the file like @nakov memtioned?

keep in mind that you will need to download the 3d party scripts locally:

Chron's avatar
Level 6

Wait. If I downloaded the script, what would be the purpose of putting the url source?

Nakov's avatar

@chron just download them locally and mix them into one file. That's it. No need for URL source.

Chron's avatar
Level 6

Is it okay to just enter this url and download it using "save as"?

https://www.paypal.com/sdk/js?client-id=t0R5jKrX2jd9TAyKhFNOfiAKYGuw9rnkh8Ywsc7bACH0eaDA5YWCjIHZ1vzFJDtolTBsEB6aT6wxbdpb&currency=USD&disable-card=visa,mastercard,amex
Nakov's avatar

if it shows the script on the browser then yes, it is :)

Chron's avatar
Level 6

Okay. I'll try it. Thanks!

Chron's avatar
Level 6

I did this:

 mix.scripts(['resources/js/paypal.js'], 'public/js/all.js')
 .js('resources/js/app.js', 'public/js')
 .sass('resources/sass/app.scss', 'public/css');

I'm getting an undefined error because app.js is being loaded first and to render paypal it needs the paypal.js

Edit: I did this:

mix
 .js('resources/js/app.js', 'public/js')
 .sass('resources/sass/app.scss', 'public/css')
 .scripts([
    'resources/js/paypal.js',
    'public/js/app.js',
    ], 'public/js/all.js');

Now I'm getting this error:

 Error: PayPal Payments SDK script not present on page! Excected to find <script src="https://www.paypal.com/sdk/js">
Nakov's avatar

So in your view do you include the all.js script? :)

You should have this two, and in this order as well.

<script src="/js/all.js"></script>
<script src="/js/app.js"></script>
jlrdw's avatar

You need to follow the PayPal API exactly like it says, not something else.

Chron's avatar
Level 6

I just did that. It's still the same error.

Chron's avatar
Level 6

Maybe there are still other options. I want my master blade to not be cluttered with too many script sources and linked stylesheet.

Next

Please or to participate in this conversation.