尝试在Heroku上使用Express backend + Vue前端托管Lerna monorepo。组件分别包含在/packages/server
和/packages/frontend
中。其目的是在部署期间构建Vue前端,并通过Express将其作为静态内容进行托管。
./package.json
{
"name": "my_app",
"private": true,
"scripts": {
"bootstrap": "lerna bootstrap",
"start": "lerna run start --stream",
"postinstall": "npm run bootstrap",
"build": "lerna run build --stream"
},
"devDependencies": {
"lerna": "^3.20.2"
}
}
我的构建阶段失败的原因是
lerna notice cli v3.20.2
lerna info Executing command in 2 packages: "npm run build"
frontend: > frontend@0.1.0 build /tmp/build_c2cbb32af790fe0e5e4852ce2bcab8e0/packages/frontend
frontend: > vue-cli-service build
frontend: sh: 1: vue-cli-service: not found
frontend: npm ERR! code ELIFECYCLE
frontend: npm ERR! syscall spawn
frontend: npm ERR! file sh
frontend: npm ERR! errno ENOENT
frontend: npm ERR! frontend@0.1.0 build: `vue-cli-service build`
frontend: npm ERR! spawn ENOENT
一个独立的Vue库不会以这种方式在构建阶段失败,所以问题很可能是lerna bootstrap
出了问题,或者Heroku是如何构建东西的,但我还不能弄清楚问题是什么。
create-react-app的类似设置不会失败,可能是因为react-scripts
是常规依赖项。
我在Lerna和devDependencies中是否遗漏了一些问题,或者这是Vue的问题?
发布于 2020-05-04 19:55:31
几天后,我终于发现了设置的问题所在。问题有两个方面:
首先,似乎vue-cli@4.3.0可能有问题,因为由于所有必需的依赖项都包含在devDependencies中,所以即使启动一个独立的项目也无法使用NODE_ENV=production
进行构建。
npx @vue/cli create test
cd test
export NODE_ENV=production
npm install
npm run build
对于本地开发,lerna bootstrap
一如预期地遵循NODE_ENV,甚至安装了devDependencies。但是,将其作为配置项的一部分运行时,会跳过devDependencies。
至于为什么一个独立的Vue项目在Heroku上构建没有任何问题,这在nodejs buildpack documentation中有详细介绍
默认情况下,Heroku将安装package.json中列在dependencies
devDependencies下的所有依赖项。
构建包只适用于单层包,所以一旦您在monorepo布局中有了嵌套包,嵌套的devDependencies就不会被触及。
将Vue依赖项移出devDependencies解决了我目前的构建问题。
https://stackoverflow.com/questions/61562545
复制