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

t9dev's avatar
Level 2

Instead change the require of to a dynamic import() using Shiki for Laravel in Shard Hosting

I am using spatie/laravel-markdown in my personal blog website, and I am hosting it in a Shared Hosting service ( I know it's a bad idea), I have manged to link the Node.js to my project directly by using the concept in this cast

'https://laracasts.com/discuss/channels/laravel/spatie-laravel-markdown-not-working-locally' To let you know, I am unable to call Shiki directly, so I called it in my blog page Livewire page like this

public $blog;

    public function mount($slug)
    {
        $this->blog = Blog::where('slug', '=', $slug)->first();
        if ($this->blog) {
             $this->callShiki($this->blog->content);  
        }
    }
    protected function callShiki(...$arguments): string
    {
        $cwd = './../vendor/spatie/shiki-php/bin';
    
        if (!$cwd) {
            throw new \Exception("The specified cwd does not exist: " . __DIR__ . '/../bin');
        }
        $command = [
            (new ExecutableFinder())->find('node', 'node', [
                '/usr/local/bin',
                '/opt/homebrew/bin',
            ]),
            'shiki.js',
            json_encode(array_values($arguments)),
        ];

        $process = new Process(
            command: $command,
            cwd: $cwd,
            timeout: null,
        );
        $process->run();
		// !!! the error below occures here
        if (! $process->isSuccessful()) {
            throw new ProcessFailedException($process);
        }

        return $process->getOutput();
    }

However, now I am facing this error when I am calling it, it happend in my local side but, I did some npm update and cache clearing and it worked fine even without calling Shiki from the Livewire component, but not happing in the production side.

failed. Exit Code: 1(General error) Working directory: ./../vendor/spatie/shiki-php/bin Output: ================ Error Output: ================ node:internal/modules/cjs/loader:1089 throw new ERR_REQUIRE_ESM(filename, true); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module /home/some-user/domains/domain.com/public_html/vendor/spatie/shiki-php/node_modules/shiki/dist/index.mjs not supported. Instead change the require of /home/some-user/domains/domain.com/public_html/vendor/spatie/shiki-php/node_modules/shiki/dist/index.mjs to a dynamic import() which is available in all CommonJS modules. at Object.<anonymous> (/home/some-user/domains/domain.com/public_html/vendor/spatie/shiki-php/bin/shiki.js:1:15) { code: 'ERR_REQUIRE_ESM' } Node.js v20.6.0

Any idea how to solve this? Do I need to downgrade a package as some says, or is there a better solution?

0 likes
1 reply
t9dev's avatar
t9dev
OP
Best Answer
Level 2

As a solution, I moved my Website to a new VPS server to have a better control over it and mange the packages easily. Regrading the JS error ERR_REQUIRE_ESM, the best solution it to check the most compatible version of that package you have a problem with and install it, for me Shiki 1.1.1 was the problem and I downgrade it to 0.14.1 and it worked perfectly on the server side.

Please or to participate in this conversation.