【发布时间】:2020-10-01 09:36:20
【问题描述】:
我试图运行一段带有 React “render” 方法的简单代码块,但浏览器没有显示文本。
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(<h1>Hello World</h1>,document.getElementById('root'));
我使用 VS Code 作为编辑器,因此我输入了“运行和调试 Node.js”命令。它提出了以下警告
(node:3004) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
在 package.json 文件中设置 "type:module" 解决了问题,但另一方面又出现了另一个问题
(node:18968) ExperimentalWarning: The ESM module loader is experimental.
warning.js:32
SyntaxError: Unexpected token '<'
at Loader.moduleStrategy (internal/modules/esm/translators.js:81:18)
at async link (internal/modules/esm/module_job.js:37:21)
那不允许我写任何标签。我错过了什么,我该如何解决?下面是index.html文件和文件结构
<html>
<head>
<title> React Test</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
【问题讨论】:
-
您是否使用
create-react-app设置了您的项目? -
是的,我已经成功加载了它附带的标准 React 项目
-
我猜缺少 JSX 支持。尝试
React.createElement('h1', null, 'Hello World')而不是<h1>Hello World</h1>来确认。见reactjs.org/docs/add-react-to-a-website.html#quickly-try-jsx -
也许您想在 vs 代码中下载
debugger for Chrome,设置launch.json文件并以这种方式运行 react 应用程序? More about it here -
@XHOxha 里面的
.vscode文件夹是什么src以及里面有什么index.html和index.js?
标签: node.js reactjs node-modules