【问题标题】:When I run npm run build I get this error?当我运行 npm run build 时出现此错误?
【发布时间】:2022-01-22 14:12:38
【问题描述】:

我对此很陌生,也很迷茫。我相信我改变了很多让我目前面临这个问题的事情。最终,我只是想npm run build我的 React.js 项目,以便我可以部署它。但我运行npm run build 并看到此错误:

my-blog git:(main) ✗ npm run build
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /Users/rondon/code/my-blog/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/Users/rondon/code/my-blog/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/rondon/.npm/_logs/2022-01-22T05_37_12_010Z-debug.log

我从中了解到的是,我需要创建一个 package.json 文件,所以我通过运行 npm init 在我的“my-blog”目录中完成了该文件。酷,我这样做了,现在我在“我的博客”中有一个新的 package.json 文件,我手动输入了 "build": "react-scripts build",这样我就可以运行 npm run build 而不会收到错误说我缺少构建脚本。这就是 package.json 文件现在的样子:

{
  "name": "my-blog",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "react-scripts build"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/rmera1026/my-blog.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/rmera1026/my-blog/issues"
  },
  "homepage": "https://github.com/rmera1026/my-blog#readme"
}

我再次npm run build,现在得到这个:

my-blog git:(main) ✗ npm run build

> my-blog@1.0.0 build
> react-scripts build

sh: react-scripts: command not found

最后,我认为我遇到了这个问题,因为我的文件可能是如何设置的,所以 here 是事情的样子。

【问题讨论】:

    标签: javascript node.js reactjs npm deployment


    【解决方案1】:

    您的 my-blog 项目有 2 个目录。一个是api,另一个是client

    您必须在您的client 目录中构建,但您在它之外。将您的目录更改为 client 并运行 npm run build

    cd client && npm run build
    

    【讨论】:

    • 您是否知道这是否允许我在 GitHub 上托管和自动部署我的应用程序,同时我将我的项目保存为 git 存储库中的“用户名”.github.io?
    • @rmera1026 您可以按照本教程进行操作:dev.to/yuribenjamin/…
    猜你喜欢
    • 2021-03-03
    • 1970-01-01
    • 2021-08-10
    • 2020-01-09
    • 2023-02-02
    • 2022-06-16
    • 2022-06-22
    • 2021-04-10
    • 2022-01-23
    相关资源
    最近更新 更多