Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: laravel is not a function #187

Closed
ferberus opened this issue Dec 16, 2022 · 15 comments
Closed

TypeError: laravel is not a function #187

ferberus opened this issue Dec 16, 2022 · 15 comments
Assignees

Comments

@ferberus
Copy link

  • Laravel Vite Plugin Version: 0.7.2
  • Laravel Version: 9.4.0
  • Node Version: 16.18.1
  • NPM Version: 8.19.2
  • Host operating system: Windows
  • Web Browser & Version: Chrome 108.0.5359.124
  • Running in Sail / Docker: Docker

Description:

I'm using Laravel 9.x along with Inertia (Svelte). There is an issue when running npm run dev: error when starting dev server:
TypeError: laravel is not a function.

Steps To Reproduce:

Bellow is vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import { svelte } from '@sveltejs/vite-plugin-svelte';

export default defineConfig({
    plugins: [
        laravel({
            input: ['resources/js/app.js'],
            refresh: true,
        }),
        svelte({
            prebundleSvelteLibraries: true,
        }),
    ],
    
    optimizeDeps: {
        include: [
            '@inertiajs/inertia',
            '@inertiajs/inertia-svelte',
            "@inertiajs/progress",
        ]
    }
});
@fpolli
Copy link

fpolli commented Dec 16, 2022

I have the same issue. lrfahmi and I are following the same path. We both first had an error that the svelte module could not be resolved because our package.json file was missing "type": "module". We added that, and then this error was the result.

My vite.config.js:

import { defineConfig } from "vite";
import laravel from "laravel-vite-plugin";
import { svelte } from '@sveltejs/vite-plugin-svelte';

export default defineConfig({
plugins: [
laravel({
input: ["resources/js/app.js"],
refresh: true,
valetTls: 'seeds.test'
}),
svelte({
/* plugin options */
})
],

resolve: {
alias: {
'ziggy': '/vendor/tightenco/ziggy/src/js'
},
},

css: {
preprocessorOptions: {
scss: {
additionalData: '@use "src/variables.scss" as *;',
},
},
},
});

My package.json:

{
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^2.0.2",
"axios": "^1.1.2",
"bulma": "^0.9.4",
"laravel-vite-plugin": "^0.7.2",
"lodash": "^4.17.19",
"postcss": "^8.1.14",
"sass": "^1.53.0",
"svelte-preprocess": "^4.10.7",
"vite": "^4.0.0"
},
"dependencies": {
"@inertiajs/inertia": "^0.11.1",
"@inertiajs/inertia-svelte": "^0.8.0",
"@inertiajs/progress": "^0.2.7"
}
}

@driesvints
Copy link
Member

@fpolli I see you're using

"type": "module",

That's not a valid setting for a Laravel app. Please revert that and try again. @lrfahmi are you also using that?

@fpolli
Copy link

fpolli commented Dec 16, 2022

If I do that, then I go right back to my first error, where NPM cannot resolve the svelte-vite-plugin.

It sounds like you are saying that Svelte cannot be used in a Laravel app. At least with Vite.

This is all very confusing.

Does this mean that Laravel is a CommonJS environment and not ES6?

@driesvints
Copy link
Member

I have no idea about all that sorry. I just can confirm that you cannot use "type": "module", in a Laravel app.

@driesvints
Copy link
Member

driesvints commented Dec 16, 2022

Basically because a Laravel app is... well an app, not a module.

@fpolli
Copy link

fpolli commented Dec 16, 2022

Thank you. That is helpful information. As you can tell, I am just learning all of this.

Direct support for Svelte as you have for React and Vue would be cool.

@driesvints
Copy link
Member

Gonna close this for now but feel free to still reply @lrfahmi

@ferberus
Copy link
Author

I'll try to start with the configuration details:

# package.json

...
    "private": true,
    "type": "module",
    "scripts": {
        "dev": "vite",
        "build": "vite build"
    },
    "devDependencies": {
        "@inertiajs/inertia": "^0.11.1",
        "@inertiajs/inertia-svelte": "^0.8.0",
        "@inertiajs/progress": "^0.2.7",
        "@sveltejs/vite-plugin-svelte": "^2.0.2",
        "@tailwindcss/forms": "^0.5.3",
        "autoprefixer": "^10.4.13",
        "axios": "^1.1.2",
        "laravel-vite-plugin": "^0.7.2",
        "lodash": "^4.17.19",
        "postcss": "^8.4.20",
        "svelte": "^3.55.0",
        "tailwindcss": "^3.2.4",
        "vite": "^4.0.0"
    },
...
# vite.config.js

import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: ['resources/js/app.js'],
            refresh: true,
        }),
        svelte({
            prebundleSvelteLibraries: true,
        }),
    ],
    
    optimizeDeps: {
        include: [
            '@inertiajs/inertia',
            '@inertiajs/inertia-svelte',
            '@inertiajs/inertia-progress',
        ]
    }
});

Is not working. When package.json is added "type": "module", i get an error message:

failed to load config from /home/fahmi/repos/..../laravel/vite.config.js
error when starting dev server:
TypeError: laravel is not a function

If without "type": "module", i get an error message:

✘ [ERROR] [plugin externalize-deps] Failed to resolve entry for package "@sveltejs/vite-plugin-svelte". The package may have incorrect main/module/exports specified in its package.json: No known conditions for "." entry in "@sveltejs/vite-plugin-svelte" package
    node_modules/esbuild/lib/main.js:1355:27:
      1355 │         let result = await callback({

Temporary Solution

It can take hours or even days to solve the problem. Conflicts between one package and another. The solution I reverted to the previous running version of the packages with vite 3.0.0.

Change package.json to rollback packages version:

  1. "@sveltejs/vite-plugin-svelte": "^1.4.0"
  2. "laravel-vite-plugin": "^0.7.1"
  3. "vite": "^3.0.0"
...
    "private": true,
    "scripts": {
        "dev": "vite",
        "build": "vite build"
    },
    "devDependencies": {
        "@inertiajs/inertia": "^0.11.1",
        "@inertiajs/inertia-svelte": "^0.8.0",
        "@inertiajs/progress": "^0.2.7",
        "@sveltejs/vite-plugin-svelte": "^1.4.0",
        "@tailwindcss/forms": "^0.5.3",
        "autoprefixer": "^10.4.13",
        "axios": "^1.1.2",
        "laravel-vite-plugin": "^0.7.1",
        "lodash": "^4.17.19",
        "postcss": "^8.4.20",
        "svelte": "^3.55.0",
        "tailwindcss": "^3.2.4",
        "vite": "^3.0.0"
    },
...

The laravel application is up and back to work. Hope this solution helps, until there is an official solution from the publishers. Cheers.

@fpolli
Copy link

fpolli commented Dec 16, 2022

Thank you for this! I hope the developers do solve it, because each side seems to think there is no problem.

@fpolli
Copy link

fpolli commented Dec 17, 2022 via email

@CodeKJ
Copy link

CodeKJ commented Dec 19, 2022

I have exactly the same issue here since updated to Vite 4.

sveltejs/vite-plugin-svelte requires "type": "module" in package.json since latest release (2.0).

More here, and also mentioned in this issue

And from what I understand laravel-vite-plugin currently doesn't support that, and should be updated.

We are currently forced to stay with Vite 3.

@jessarcher
Copy link
Member

I've made a note to look into this when I'm back at work next week.

@timacdonald timacdonald reopened this Dec 20, 2022
@timacdonald
Copy link
Member

timacdonald commented Dec 20, 2022

As a temporary fix, you may specify "type": "module" in the package.json and then when you reference the laravel function, instead reference laravel.default.

We will investigate a fix for this.

e.g.

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import { svelte } from '@sveltejs/vite-plugin-svelte';

export default defineConfig({
    plugins: [
        laravel.default({
            input: ['resources/js/app.js'],
            refresh: true,
        }),
        svelte({
            //
        }),
    ],
});

@timacdonald
Copy link
Member

WIP PR to address this issue: #189

@timacdonald
Copy link
Member

Gonna close this and we can continue the conversation in the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants