I have a problem with my monorepo, I try to deploy a standard express.js application but the shared dependencies don't load after the deployment, it works in local development.
Has anyone ever encountered this problem with similar configuration while using Vercel CLI for deployments ? Thanks.
Ideas:
I think the trouble probably come from the
<rootDir>/services/my-api/vercel.jsonhere (it probably has to wait for more instructions):
"builds": [{ "src": "src/*.ts", "use": "@vercel/node" }]
Other solution:
I think to solve the problem I will use a bundler (
webpackor@vercel/ncc) to wrap dependencies into a complete build. I haven't started yet but do you think it could bring other problems? I didn't find any repo on github or gitlab that did this and I think there is probably a reason for that. I'm not sure about the result because I think a build is already done remotely with@vercel/nccon vercel services.
Encountered error:
On build from vercel service, yarn try to import the package from the yarn packages registry.
18:55:41.470 Retrieving list of deployment files...
18:55:45.058 Downloading 10 deployment files...
18:55:46.336 Analyzing source code...
18:55:46.353 Warning: Due to `builds` existing in your configuration file, the Build and Development Settings defined in your Project Settings will not apply. Learn More: https://vercel.link/unused-build-settings
18:55:47.624 Installing build runtime...
18:55:48.205 Build runtime installed: 581.371ms
18:55:51.010 Looking up build cache...
18:55:51.501 Build cache found. Downloading...
18:55:53.000 Build cache downloaded [19.63 MB]: 1498.987ms
18:55:54.118 Installing dependencies...
18:55:54.466 yarn install v1.22.10
18:55:54.485 info No lockfile found.
18:55:54.490 [1/4] Resolving packages...
18:55:54.700 error An unexpected error occurred: "https://registry.yarnpkg.com/@vercel-monorepo-example%2fcommon: Not found".
18:55:54.700 info If you think this is a bug, please open a bug report with the information provided in "/vercel/path1/yarn-error.log".
18:55:54.701 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
18:55:54.721 Error: Command "yarn install" exited with 1
My configuration:
Versions:
| Tool | Version |
|---|---|
| Vercel CLI | 23.0.0 |
Tree:
| - packages
| - common
| - src
| - index.ts
| - package.json
| - tsconfig.json
| - services
| - my-api
| - src
| - index.ts
| - package.json
| - tsconfig.json
| - vercel.json
| - lerna.json
| - package.json
| - yarn.lock
Files:
# <rootDir>/lerna.json
{
"npmClient": "yarn",
"version": "independent",
"useWorkspaces": true
}
# <rootDir>/package.json
{
"name": "@vercel-monorepo-example/monorepo",
"description": "vercel-monorepo-example project root",
"version": "0.1.0",
"private": true,
"workspaces": [
"packages/*",
"services/*"
]
}
# <rootDir>/packages/common/package.json
{
"name": "@vercel-monorepo-example/common",
"description": "vercel-monorepo-example common",
"version": "0.1.0",
"main": "src/index.ts",
"files": ["src"]
}
# <rootDir>/packages/common/tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "es6", "esnext"],
"module": "commonjs",
"moduleResolution": "node",
"types": ["node"],
"sourceMap": true,
"declaration": true,
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"removeComments": true,
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"esModuleInterop": true,
"isolatedModules": true,
"resolveJsonModule": true,
"outDir": "./dist",
"baseUrl": ".",
"paths": {
"@src/*": ["src/*"]
}
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
// <rootDir>/packages/common/src/index.ts
export const sayHello = () => console.log('Helloworld!');
# <rootDir>/services/my-api/package.json
{
"name": "@vercel-monorepo-example/my-api",
"description": "vercel-monorepo-example api",
"version": "0.1.0",
"main": "src/index.ts",
"dependencies": {
"@vercel-monorepo-example/common": "0.1.0",
"express": "^4.17.1"
},
"devDependencies": {
"@types/express": "^4.17.12",
"@vercel/node": "^1.11.1",
"typescript": "^4.3.5"
}
}
# <rootDir>/services/my-api/tsconfig.json
# Use the same configuration than above (@vercel-monorepo-example/common)
# <rootDir>/services/my-api/vercel.json
{
"version": 2,
"github": {
"enabled": false
},
"builds": [{ "src": "src/*.ts", "use": "@vercel/node" }],
"rewrites": [{ "source": "/(.*)", "destination": "/src/index.ts" }]
}
// <rootDir>/services/my-api/src/index.ts
import type { VercelRequest, VercelResponse } from '@vercel/node';
import { sayHello } from '@vercel-monorepo-example/common';
const app = require('express')();
app.get('/', (req: VercelRequest, res: VercelResponse) => {
sayHello();
res.status(200);
res.json({ statusCode: 200 });
});
app.use((req: VercelRequest, res: VercelResponse) => {
res.status(404);
res.json({ statusCode: 404 });
});
export default app;
Vercel configuration:
Build & Development Settings, is set onOther Frameworkoptions and the commands are clear.Root Directoryis set onservices/my-apiand hasInclude source files outside of the Root Directory in the Build Step.option checked.