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

Randy_Johnson's avatar

InertiaJS app.js Resolve Package Issue!

I have created a package that works with InertiaJS, the package looks like so.

\---Backbone
    |   composer.json
    |
    \---src
        |   BackboneServiceProvider.php
        |   routes.php
        |   SettingsController.php
        |   TableController.php
        |
        +---database
        |   \---migrations
        |           2023_05_24_103552_create_backbone_settings_table.php
        |
        \---resources
            \---js
                +---Components
                +---Layouts
                \---Pages
                    +---Table
                    |   |   Index.jsx
                    |   |
                    |   +---Contexts
                    |   |       ColumnContext.jsx
                    |   |       DeleteContext.jsx
                    |   |       ExcludeCreateContext.jsx
                    |   |       ExcludeDeleteContext.jsx
                    |   |       ExcludeReadContext.jsx
                    |   |       ExcludeUpdateContext.jsx
                    |   |       ObjectContext.jsx
                    |   |       TableContext.jsx
                    |   |       TableNameContext.jsx
                    |   |       UpdateContext.jsx
                    |   |
                    |   \---Partials
                    |           Create.jsx
                    |           Delete.jsx
                    |           Read.jsx
                    |           Settings.jsx
                    |           Test.jsx
                    |           Update.jsx
                    |
                    \---Tables
                        |   Index.jsx
                        |
                        \---Partials

I had to change the app.js so it would take into account the package when looking for a route, and page.

Original app.js

resolve: (name) => resolvePageComponent(`./Pages/${name}.jsx`, import.meta.glob('./Pages/**/*.jsx')),

Edited app.js

    resolve: async (name) => {
        const firstPath = `./Pages/${name}.jsx`;
        const secondPath = `../../packages/JDH/Backbone/src/resources/js/Pages/${name}.jsx`;

        try {
            const module = await import(firstPath);
            return module.default || null;
        } catch (error) {
            console.error(`Failed to resolve page component '${firstPath}':`, error);
            try {
                // @vite-ignore
                const module = await import(secondPath);
                return module.default || null;
            } catch (error2) {
                console.error(`Failed to resolve page component '${secondPath}':`, error2);
            }
        }

        return null;
    },

Right now everything is okay, but there are some errors showing up:

https://pasteboard.co/D9wq58WqwJgg.png

Looking at what I have done above can anyone confirm that this is a legitimate way of doing it.

I have a feeling that when I upload to shared hosting it isn't going to allow it because I ran a server with php -S from the public folder and it would not display.

Please help. Sorry for my poor explanation.

0 likes
3 replies
LaryAI's avatar
Level 58

The errors in the screenshot suggest that some dependencies are missing. One possible solution is to run npm install to install all the required dependencies. Additionally, it's recommended to use a package manager like Yarn or npm to manage dependencies instead of manually copying files.

Regarding the app.js changes, it seems like a valid approach to resolve components from a package. However, it's important to make sure that the package is installed and available in the correct path.

If the app doesn't display when running php -S, it could be due to a misconfiguration or missing dependencies. It's recommended to use a web server like Apache or Nginx to serve the app instead of the built-in PHP server.

Here's an example of how to use Yarn to install the package and resolve components in app.js:

// Install the package
yarn add @jdh/backbone

// Update app.js
resolve: async (name) => {
  const firstPath = `./Pages/${name}.jsx`;
  const secondPath = `@jdh/backbone/Pages/${name}.jsx`;

  try {
    const module = await import(firstPath);
    return module.default || null;
  } catch (error) {
    console.error(`Failed to resolve page component '${firstPath}':`, error);
    try {
      const module = await import(secondPath);
      return module.default || null;
    } catch (error2) {
      console.error(`Failed to resolve page component '${secondPath}':`, error2);
    }
  }

  return null;
},
Randy_Johnson's avatar
resolve: (name) => resolvePageComponent(`./Pages/${name}.jsx`, import.meta.glob('./Pages/**/*.jsx'))
    || resolvePageComponent(`../../packages/JDH/Backbone/src/resources/js/Pages/${name}.jsx`, import.meta.glob('../../packages/JDH/Backbone/src/resources/js/Pages/**/*.jsx')),

I came up with this, but it doesn't work, if anyone has an idea why, since I am using the same path as previous.

Randy_Johnson's avatar
Randy_Johnson
OP
Best Answer
Level 8

There is no answer, this is totally the wrong way of doing it.

Please or to participate in this conversation.