I have a problem when deploying my site to netlify. I have a project with multiple svelte vite projects and one svelte project as a shared library. Its not a monorepo (which i am aware you have documentation for), because i couldnt get it to work locally (i needed separate node_modules folders for specific dependencies with different versions in each project, and a global node_modules for the shared dependencies, but it kept installing everything into global node_modules). Anyway, So now I have a setup like this:
- shared/
- src/
- package.json
- …
- packages
- webxr/
- src/
- package.json
- …
- aframe/
- src/
- package.json
- …
- webxr/
I created an npm link so i can import from the shared project in the projects under packages. I tried using a “deploy.js” script where i run “npm install” in the shared folder and then “npm run build” in the corresponding packages project folder i want to deploy with the base directory being set to root level of the entire project. However, i get the following error in the “Building” section of my deploy log:
6:38:52 PM: Netlify Build
6:38:52 PM: ────────────────────────────────────────────────────────────────
6:38:52 PM:
6:38:52 PM: ❯ Version
6:38:52 PM: @netlify/build 29.31.1
6:38:52 PM:
6:38:52 PM: ❯ Flags
6:38:52 PM: baseRelDir: true
6:38:52 PM: buildId: 6592f89eed4f110008c050c9
6:38:52 PM: deployId: 6592f89eed4f110008c050cb
6:38:52 PM: packagePath: packages/webxr
6:38:52 PM:
6:38:52 PM: ❯ Current directory
6:38:52 PM: /opt/build/repo
6:38:52 PM:
6:38:52 PM: ❯ Config file
6:38:52 PM: No config file was defined: using default values.
6:38:52 PM:
6:38:52 PM: ❯ Context
6:38:52 PM: production
6:38:52 PM:
6:38:52 PM: Build command from Netlify app
6:38:52 PM: ────────────────────────────────────────────────────────────────
6:38:52 PM:
6:38:52 PM: $ node deploy.js
6:38:52 PM: Building projects… /opt/build/repo/shared /opt/build/repo/packages/webxr
6:38:54 PM: stdout:
6:38:54 PM: added 58 packages, and audited 59 packages in 1s
6:38:54 PM: 3 packages are looking for funding
6:38:54 PM: runnpm fund
for details
6:38:54 PM: found 0 vulnerabilities
6:38:54 PM: stderr:
6:38:54 PM: Error: Error: Command failed: npm run build
6:38:54 PM: sh: 1: vite: not found
6:38:54 PM: Failed to build: Error: Command failed: npm run build
6:38:54 PM: sh: 1: vite: not found
6:38:54 PM: at ChildProcess.exithandler (node:child_process:422:12)
6:38:54 PM: at ChildProcess.emit (node:events:517:28)
6:38:54 PM: at maybeClose (node:internal/child_process:1098:16)
6:38:54 PM: at ChildProcess._handle.onexit (node:internal/child_process:303:5) {
6:38:54 PM: code: 127,
6:38:54 PM: killed: false,
6:38:54 PM: signal: null,
6:38:54 PM: cmd: “npm run build”
6:38:54 PM: }
6:38:54 PM:
6:38:54 PM: (build.command completed in 1.5s)
6:38:54 PM:
6:38:55 PM: Finished processing build request in 15.22s
The content of the deploy.js script:
const { exec } = require('child_process');
const path = require('path');
// Function to run a command in a specified directory
function runCommand(command, directory) {
return new Promise((resolve, reject) => {
exec(command, { cwd: directory }, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error}`);
return reject(error);
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
resolve();
});
});
}
async function buildProjects() {
try {
// Directory two levels up
const shared = path.join(__dirname, 'shared');
const package = path.join(__dirname, 'packages/webxr');
console.log("Building projects...", shared, package);
// Run 'npm run build' in the parent directory
await runCommand('npm install', shared);
// Run 'npm run build' in the current directory
await runCommand('npm run build', package);
} catch (error) {
console.error('Failed to build:', error);
}
}
buildProjects();
The content of a package.json file in one of the packages project to show that “npm run build” maps to “vite build”:
{
"name": "webxr",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-check --tsconfig ./tsconfig.json"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^3.0.1",
"@tsconfig/svelte": "^5.0.2",
"svelte": "^4.2.8",
"svelte-check": "^3.6.2",
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"vite": "^5.0.8"
},
"dependencies": {
"three": "^0.160.0"
}
}
How do i solve this? Or better yet, how can i make this deploy work? Im very tight on time right now and I simply couldnt get a monorepo setup to work locally, although that may be better for deployment.