'Deploying Svelte App to Heroku / Dokku fails because modules cannot be found

I am having problems deploying my svelte web3 App to my VPS running Dokku. The structure of the folder is as follows:

root
|-->svelte 
|--> ...

I found some solutions to work with the two folders needing npm install around here. So I put: "heroku-postbuild": "cd ./svelte && npm install && npm run build" and "cacheDirectories": ["./node_modules", "./svelte/node_modules"] in the root package.json. But when the process comes to this point it fails with the following error:

remote: [!] Error: Cannot find module '@rollup/plugin-node-resolve'
remote: Require stack:
remote: - /tmp/build/svelte/rollup.config.js
remote: - /tmp/build/svelte/node_modules/rollup/dist/shared/loadConfigFile.js
remote: - /tmp/build/svelte/node_modules/rollup/dist/bin/rollup

I've tried changing the import to include the full paths etc. but nothing has worked so far. I have also changed the order of the imports, but the process fails with the first import in the rollup.config. Any ideas?

package.json root:

 "scripts": {
    "heroku-postbuild": "cd ./svelte && npm install && npm run build"
    
  },
  "dependencies": {
    "@openzeppelin/contracts": "^4.6.0",
    "@truffle/hdwallet-provider": "^2.0.8",
    "babel-polyfill": "^6.26.0",
    "babel-register": "^6.26.0",
    "dotenv": "^16.0.0",
    "web3": "^1.7.3"
  },
  "cacheDirectories": ["./node_modules", "./svelte/node_modules"],
  "engines": {
    "node": "16.14",
    "npm": "8.7.x"
  }
}

package.json svelte (added all the dev dependencies again just in case)

{
  "name": "app",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w",
    "start": "sirv public --no-clear"
  },
  "devDependencies": {
    "@rollup/plugin-commonjs": "^17.0.0",
    "@rollup/plugin-node-resolve": "^11.0.0",
    "rollup": "^2.3.4",
    "rollup-plugin-css-only": "^3.1.0",
    "rollup-plugin-livereload": "^2.0.0",
    "rollup-plugin-svelte": "^7.0.0",
    "rollup-plugin-terser": "^7.0.0",
    "svelte": "^3.0.0"
  },
  "dependencies": {
    "rollup-plugin-terser": "^7.0.0",
    "svelte": "^3.0.0",
    "@rollup/plugin-commonjs": "^17.0.0",
    "@rollup/plugin-node-resolve": "^11.0.0",
    "rollup-plugin-css-only": "^3.1.0",
    "rollup": "^2.3.4",
    "rollup-plugin-svelte": "^7.0.0",
    "@rollup/plugin-json": "^4.1.0",
    "apexcharts": "^3.35.0",
    "bootstrap": "^5.1.3",
    "moment": "^2.29.2",
    "rollup-plugin-copy": "^3.4.0",
    "sirv-cli": "^2.0.0",
    "svelte-web3": "^3.4.0"
  }
}

rollup.config.js

import resolve from '@rollup/plugin-node-resolve';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import svelte from 'node_modules/rollup-plugin-svelte';
import commonjs from 'node_modules/@rollup/plugin-commonjs';
import livereload from 'node_modules/rollup-plugin-livereload';
import { terser } from 'node_modules/rollup-plugin-terser';
import css from 'node_modules/rollup-plugin-css-only';
import json from 'node_modules/@rollup/plugin-json';
import copy from 'node_modules/rollup-plugin-copy'

const production = !process.env.ROLLUP_WATCH;

function serve() {
    let server;

    function toExit() {
        if (server) server.kill(0);
    }

    return {
        writeBundle() {
            if (server) return;
            server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                stdio: ['ignore', 'inherit', 'inherit'],
                shell: true
            });

            process.on('SIGTERM', toExit);
            process.on('exit', toExit);
        }
    };
}

export default {
    input: 'src/main.js',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/build/bundle.js'
    },
    plugins: [
        svelte({
            compilerOptions: {
                // enable run-time checks when not in production
                dev: !production
            }
        }),
        // we'll extract any component CSS out into
        // a separate file - better for performance
        css({ output: 'bundle.css' }),

        // If you have external dependencies installed from
        // npm, you'll most likely need these plugins. In
        // some cases you'll need additional configuration -
        // consult the documentation for details:
        // https://github.com/rollup/plugins/tree/master/packages/commonjs
        resolve({
            browser: true,
            dedupe: ['svelte']
        }),
        commonjs(),
        json(),
        nodeResolve(),
        copy({
            targets: [{ 
                src: './node_modules/bootstrap/dist/**/*', 
                dest: 'public/vendor/bootstrap' 
            }]
        }),

        // In dev mode, call `npm run start` once
        // the bundle has been generated
        !production && serve(),

        // Watch the `public` directory and refresh the
        // browser on changes when not in production
        !production && livereload('public'),

        // If we're building for production (npm run build
        // instead of npm run dev), minify
        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

```


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source