【问题标题】:NPM start not working after adding create react app typescript添加创建反应应用程序打字稿后,NPM 开始无法正常工作
【发布时间】:2021-10-12 04:21:08
【问题描述】:

我正在尝试启动我的 create react 应用程序(Typescript),但我总是收到此错误。我已经完成了所有步骤,并且添加了 .env 文件(SKIP_PREFLIGHT_CHECK=true),我还更新了我的 NPM,甚至重新安装了它。我该怎么办?

> Executing task: npm run start <


> my-app@0.1.0 start C:\Users\raahi\OneDrive\Desktop\typescript-react\my-app
> react-scripts start


There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.

The react-scripts package provided by Create React App requires a dependency:

  "babel-jest": "^26.6.0"

Don't try to install it manually: your package manager does it automatically.
However, a different version of babel-jest was detected higher up in the tree:

  C:\Users\raahi\node_modules\babel-jest (version: 24.9.0)

Manually installing incompatible versions is known to cause hard-to-debug issues.

If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.

To fix the dependency tree, try following the steps below in the exact order:

  1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
  2. Delete node_modules in your project folder.
  3. Remove "babel-jest" from dependencies and/or devDependencies in the package.json file in your project folder.
  4. Run npm install or yarn, depending on the package manager you use.

In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:

  5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
     This may help because npm has known issues with package hoisting which may get resolved in future versions.

  6. Check if C:\Users\raahi\node_modules\babel-jest is outside your project directory.
     For example, you might have accidentally installed something in your home folder.

  7. Try running npm ls babel-jest in your project folder.
     This will tell you which other package (apart from the expected react-scripts) installed babel-jest.

If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.

P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my-app@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the my-app@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\raahi\AppData\Roaming\npm-cache\_logs\2021-08-07T19_47_30_824Z-debug.log
The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command npm run start" terminated with exit code: 1.

【问题讨论】:

    标签: typescript npm create-react-app


    【解决方案1】:

    我发现了同样的错误。如果您尝试运行npm ls babel-jest,它将列出您项目中安装的所有 babel-jest 版本。您可能会看到 babel-jest 安装了不同的版本,因为每个依赖项都安装了不同版本的 babel-jest。

    我通过 2 个步骤找到了解决方法:

    1. 尝试以使用相同版本的 babel-jest 的方式匹配依赖项的版本
    2. 您可以在 package.json 上使用resolutions 来解决版本冲突
       "devDependencies": {
            "@babel/core": "^7.15.0",
            "@babel/preset-env": "^7.15.0",
            "@babel/preset-typescript": "^7.15.0",
            "@types/jest": "^26.0.24",
            "@types/react-router-dom": "^5.1.8",
            "@types/uuid": "^8.3.1",
            "@typescript-eslint/eslint-plugin": "^4.29.1",
            "@typescript-eslint/parser": "^4.29.1",
            "babel-jest": "^26.6.0",
            "eslint": "^7.32.0",
            "eslint-config-standard": "^16.0.3",
            "eslint-plugin-import": "^2.24.0",
            "eslint-plugin-node": "^11.1.0",
            "eslint-plugin-promise": "^5.1.0",
            "eslint-plugin-react": "^7.24.0",
            "husky": "^4.3.8",
            "jest": "26.6.0",
            "lint-staged": "^11.1.2",
            "typescript": "^4.3.5"
          },
          "resolutions" : {
            "babel-jest": "26.6.0",
            "jest": "26.6.0"
          }
    

    解决问题后,您的npm ls babel-jest 将如下所示:

    【讨论】:

      【解决方案2】:

      您好像不小心在主文件夹中安装了一些东西。看起来您正在尝试在 C:\Users\raahi\OneDrive\Desktop\typescript-react\my-app 文件夹中开发您的应用程序,但在过去的某个时候,您可能不小心将东西安装到您的主文件夹 C:\Users\raahi\node_modules\babel-jest

      如果您删除文件夹C:\Users\raahi\node_modules 以及C:\Users\raahi\package.jsonC:\Users\raahi\package-lock.json(如果最后两个文件存在),它应该可以解决您的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-22
        • 2021-09-21
        • 2023-01-28
        • 2020-09-06
        • 1970-01-01
        • 2017-11-30
        • 2020-02-07
        • 2015-02-20
        相关资源
        最近更新 更多