首页 > 解决方案 > 当我在 WebStorm 中运行 JavaScript 文件时,说不能在模块外导入语句

问题描述

错误说这是 **import React, {Component} 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_enter code heremain_module.js:17:11**

标签: javascriptnode.jsreactjswebstorm

解决方案


看起来好像您正在尝试使用 Node.js 运行您的 React 应用程序,通过右键单击您的组件.js文件选择Run。这不应该起作用;您必须编译/构建您的应用程序,启动它托管的服务器,然后在浏览器中打开服务器 URL。对于使用 创建的应用程序create-react-app,请按照以下说明进行操作https://www.jetbrains.com/help/webstorm/2020.1/react.html#react_running_and_debugging


推荐阅读