未捕获的 ReferenceError:进程未定义/第 0 行:解析错误

发布于 2025-01-11 06:08:36 字数 3234 浏览 0 评论 0 原文

对于一个简单的 创建 React 应用 项目时,我运行 npm install。 然后 npm start 打开默认的 Web 浏览器1。 按 F12 在控制台中显示两条错误消息。

运行 'npm start' 在浏览器中出现两个错误。

错误消息为:

  • Uncaught ReferenceError: process is not Defined
  • Line 0: Parsing error :当模式为 ES6 且在模块上下文中时,应该出现 ImportDeclaration

我应该怎么做才能纠正这些错误?


我在其他地方看到过这些错误,有时指的是 package.json 中软件包的版本问题。
请参阅下面的参考列表。 我相信参考文献3-6可能与这里的问题有关。

下面提供了 .eslintrc.jsonpackage.json 文件。
但由于它们几乎不足以重现错误,
这是一个链接 包含所有必需项目文件的 zip 文件2

.eslintrc.json

{
  "env": {
    "browser": true,
    "es2021": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended"
  ],
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "plugins": [
    "react"
  ],
  "rules": {
    "no-unused-vars": [
      "warn",
      {
        "argsIgnorePattern": "^_",
        "varsIgnorePattern": "^_"
      }
    ]
  }
}

package.json

{
  "name": "Uncaught ReferenceError",
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.4.1",
    "@testing-library/user-event": "^7.2.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version"
    ]
  },
  "devDependencies": {
    "eslint": "^7.32.0",
    "eslint-plugin-react": "^7.29.2"
  }
}

参考文献


1 就我而言,Google Chrome 版本 98.0.4758.102,64 位。 在 Windows 10 上运行。

2 通过运行 npm install 安装项目(本地) - 这可能 大约需要 5-9 分钟。
然后运行 ​​npm start 在默认 Web 浏览器中打开项目。

For a simple Create React App
project, I run npm install.
Then npm start opens the default web browser1.
Pressing F12 displays two error messages in the console.

Running 'npm start' gives two errors in the browser.

The error messages are:

  • Uncaught ReferenceError: process is not defined, and
  • Line 0: Parsing error: ImportDeclaration should appear when the mode is ES6 and in the module context.

What should I do to rectify these errors?


I have seen these errors elsewhere, sometimes referring to
version issues of the packages in package.json.
See the reference list below.
I believe references 3-6 may be related to the issues here.

The .eslintrc.json and package.json files are provided below.
But since they will hardly be sufficient to reproduce the error,
here is a link to
a zip file containing all the necessary project files.
2

.eslintrc.json :

{
  "env": {
    "browser": true,
    "es2021": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended"
  ],
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "plugins": [
    "react"
  ],
  "rules": {
    "no-unused-vars": [
      "warn",
      {
        "argsIgnorePattern": "^_",
        "varsIgnorePattern": "^_"
      }
    ]
  }
}

package.json :

{
  "name": "Uncaught ReferenceError",
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.4.1",
    "@testing-library/user-event": "^7.2.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version"
    ]
  },
  "devDependencies": {
    "eslint": "^7.32.0",
    "eslint-plugin-react": "^7.29.2"
  }
}

References


1
In my case Google Chrome Version 98.0.4758.102, 64-bit.
Running on Windows 10.

2
Install the project (locally) by running npm install – this may
take about 5-9 minutes.
Then run npm start to open the project in the default web browser.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴梦长久 2025-01-18 06:08:36

我应该如何纠正这些错误?

答案:将所有 npm 软件包更新到最新版本

尝试更新所有 npm 打包到最新版本。
目的是降低版本冲突的风险。

1. 全局安装 npm-check-updates

在命令行中,运行:
1

npm install --global npm-check-updates

2. 检查最新版本

预览 npm-check-updates 将升级哪些软件包:
2

npm-check-updates

如果该列表看起来不错,请继续将软件包更新到它们的版本
package.json 中的最新版本:

npm-check-updates --upgrade

这是 Windows 10 中的终端响应:

升级 package.json

3.安装最新版本

在命令行中,运行:
3

npm install

4. 检查浏览器和/或终端中的错误

在命令行中,运行:

npm start

浏览器现在显示不少于 5 个错误。

网络浏览器中的五个错误

终端确认这五个错误。

终端中的五个错误

^ 点击放大

哇。这看起来真的,真的很糟糕,不是吗?

没有。 不用担心! – 您现在所需要做的就是关闭服务器
(Ctrl+C) 并再次运行 npm start
– 如果您仍然看到错误,请再执行一次 – 关闭服务器
并再次运行 npm start
如果这也没有帮助,请尝试等待大约 3-7 分钟后再进行
再次尝试 npm start

当我尝试此操作时,浏览器中不再出现任何错误。

网络浏览器中没有错误!

终端报告编译成功!

终端显示:'编译成功!' ” title=

耶!

参考


1
我使用的是 Windows 10,但我希望此处提供的所有命令
在 Linux 和 macOS 上也能正常工作。

2
要获取选项列表,请运行 npm-check-updates --help

3
预计 npm install 命令大约需要 3-8 分钟
完成。
但如果您再次运行它,它应该在 10-15 秒内完成。

What should I do to rectify these errors?

Answer: update all npm packages to their latest versions

Try updating all npm packages to their latest versions.
The purpose is to decrease the risk of getting version conflicts.

1. Globally install npm-check-updates

In the command line, run :
1

npm install --global npm-check-updates

2. Check for the latest versions

Preview what packages npm-check-updates would upgrade :
2

npm-check-updates

If that list looks OK, go ahead and update the packages to their
latest versions in your package.json :

npm-check-updates --upgrade

Here is the terminal response in Windows 10:

Upgrading package.json

3. Install the latest versions

In the command line, run:
3

npm install

4. Check for errors in the browser and/or in the terminal

In the command line, run:

npm start

The browser now displays no less than five errors.

Five errors in the web browser

The terminal confirms the five errors.

Five errors in the terminal

^ click to enlarge

Wow. This looks really, really bad, doesn't it?

Nope. Fear not! – All you need to do now is close the server
(Ctrl+C) and run npm start once more.
– If you still see the errors, do it one more time – close the server
and run npm start again.
If that doesn't help either, try waiting about 3-7 minutes before you
try npm start again.

When I tried this, I no longer had any errors in the browser.

NO errors in the web browser!

And the terminal reported Compiled successfully!

The terminal says: 'Compiled successfully!'

Yay!

Reference


1
I am on Windows 10, but I expect all the commands provided here to
work just as fine on Linux and macOS.

2
To get a list of options, run npm-check-updates --help.

3
Expect the npm install command to take about 3-8 minutes
to complete.
But if you run it again, it should complete within 10-15 seconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文