【问题标题】:Run nested npm script from parent directory in Netlify?从 Netlify 的父目录运行嵌套的 npm 脚本?
【发布时间】:2023-03-29 23:13:01
【问题描述】:

我正在尝试在 Netlify 上设置持续部署。我已经链接了我的 Github 帐户,所以我现在需要设置我的构建命令:

复杂之处在于我的带有构建命令(来自 create-react-app)的 package.json 不在项目根目录中。相反,它的 1 个文件夹如下所示:

react         // this is a folder
-package.json // has build commands for React
server        // this is a folder
-package.json // has build commands for my backend as a service 

由于我将后端用作服务,因此我不需要部署到 Netlify 的“服务器”文件夹中的任何内容,只需要“反应”文件夹的内容。

我可以从项目根目录运行 react/package.json 中的命令吗?

在我的本地机器上,我可以使用以下命令从根目录运行构建命令:

{
    "scripts": {
        "netlify": "cd ./react && npm run build"
    }
}

但在 Netlify 上出现错误:

9:56:31 AM: Executing user command: npm run netlify
9:56:31 AM: > @ netlify /opt/build/repo
9:56:31 AM: > cd ./react && npm run build
9:56:32 AM: > testproject@0.0.1 build /opt/build/repo/react
9:56:32 AM: > npm-run-all build-css build-js
9:56:32 AM: sh: 1: npm-run-all: not found
9:56:32 AM: npm ERR! file sh
9:56:32 AM: npm ERR! code ELIFECYCLE
9:56:32 AM: npm ERR! errno ENOENT
9:56:32 AM: npm ERR! syscall spawn
9:56:32 AM: npm ERR! testproject@0.0.1 build: `npm-run-all build-css build-js`
9:56:32 AM: npm ERR! spawn ENOENT
9:56:32 AM: npm ERR!
9:56:32 AM: npm ERR! Failed at the testproject@0.0.1 build script.
9:56:32 AM: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
9:56:32 AM: npm WARN Local package.json exists, but node_modules missing, did you mean to install?
9:56:32 AM: npm ERR! A complete log of this run can be found in:
9:56:32 AM: npm ERR!     /opt/buildhome/.npm/_logs/2018-03-01T02_56_32_022Z-debug.log
9:56:32 AM: npm ERR! code ELIFECYCLE
9:56:32 AM: npm ERR! errno 1
9:56:32 AM: npm ERR! @ netlify: `cd ./react && npm run build`
9:56:32 AM: npm ERR! Exit status 1

【问题讨论】:

    标签: netlify


    【解决方案1】:

    免责声明:我为 Netlify 工作

    如果有 /package.json,我们的构建过程只会为您“npm install”。我认为您可能会使用此构建命令成功构建:

    cd ./react && npm install && npm run build

    ...假设你在 package.json 中有 npm-run-all :)

    然而,这不是解决问题的最佳方法。最佳做法是改用netlify.toml file 为您的构建设置base。在开始构建之前,我们将 cd 到这个目录,然后我们将拿起你的 package.json 并自动进行安装,这样你的构建命令将被简化为 npm run build,任何克隆你的 repo 的人也会为在 Netlify 上取得成功做好准备 :)

    [build] base = "react"

    ...是您在该文件中需要的全部内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-25
      • 2021-09-20
      • 2021-09-04
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      相关资源
      最近更新 更多