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

rsarvarov's avatar

Inertia SSR: Cannot use import statement outside a module

I have configured inertiajs SSR (using official documentation). When user is navigating to my website SSR is not working and I see this in my console. Any ideas to fix this?

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Module._compile (/var/www/html/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.newLoader [as .js] (/var/www/html/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
0 likes
3 replies
Sinnbeck's avatar

What did you add the second before this error occurred? It's also really hard to debug without aby code

razek's avatar

Did you find a solution for this?

1 like
rsarvarov's avatar

@razek as I remeber, yes, I did.

// webpack.ssr.mix.js
const mix = require('laravel-mix');
const webpackNodeExternals = require('webpack-node-externals');

mix
  .options({ manifest: false })
  .js('resources/js/ssr.js', 'public/js')
  .vue({ version: 3, options: { optimizeSSR: true } })
  .webpackConfig(require('./webpack.ssr.config'))
  .webpackConfig({
    target: 'node',
    externals: [webpackNodeExternals()],
  });

Please or to participate in this conversation.