作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试通过 react js 创建 hello world 应用程序。我在 IntelliJ IDEA 中创建了 NodeJS 应用程序。创建一个 helloworld.js 文件。并将此代码添加到此文件
import React from 'react';
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
);
{
"name": "testjsapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"react-dom": "^16.12.0"
}
}
"C:\Program Files\nodejs\node.exe" D:\projects\testjsapp\hello\helloworld.js
D:\projects\testjsapp\hello\helloworld.js:2
import React from 'react';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:891:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11
Process finished with exit code 1
最佳答案
您正在尝试在 Node.js 中执行一个 React 程序,但它被设计为运行 在网络浏览器中 . Node.js 中没有 DOM 供您操作。那是行不通的。
由于您正在运行的程序使用 JSX,因此您需要先对其进行转译,然后它才能在那里工作。
返回 to the tutorial . section on setting up a local development environment provides an approach to getting up and running或者您可以查看 selection of toolchains您可以从中选择。
关于javascript - 语法错误 : Cannot use import statement outside a module when start NodeJS from IntellijIDEA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60004485/
我是一名优秀的程序员,十分优秀!