简体   繁体   English

'SyntaxError: 不能在模块外使用 import 语句'

[英]'SyntaxError: Cannot use import statement outside a module'

enter image description here This problem occurred while importing createGlobalStyle from styled-components.在此处输入图像描述从样式组件导入 createGlobalStyle 时出现此问题。 I also tried adding "type": "module" to the package.json file, but nothing meaningful.我还尝试在 package.json 文件中添加“type”:“module”,但没有任何意义。

[ GlobalStyle.jsx ] [全球风格.jsx]

    import { createGlobalStyle } from "styled-components";
// eslint-disable-next-line
import React from 'react';

const GlobalStyle = createGlobalStyle` 
    body {
        width: 100%;
        margin: 0;
    }
`; 

export default GlobalStyle; 

[ package.json ] [包.json]

{
  "name": "dev-lio",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.1.0",
    "react-dom": "^18.1.0",
    "react-router-dom": "^6.3.0",
    "react-scripts": "^5.0.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "babel-plugin-styled-components": "^2.0.7"
  }
}

Any advice would be appreciated.任何意见,将不胜感激。

[ login.jsx ] [登录.jsx]

import React from 'react'
import GlobalStyle from '../../components/GlobalStyle';
import GlobalFonts from '../../fonts/fonts'
import LoginForm from '../../components/LoginForm';

const Login = () => {
    return (
        <div>
            <LoginForm />
            <GlobalFonts />
            <GlobalStyle />
        </div>
    )
}

export default Login;

[ index.jsx ] [索引.jsx]

import React from 'react'
import GlobalStyle from '../../components/GlobalStyle';
import Header from '../../components/Header';
import Profile from '../../components/Profile';
import GlobalFonts from '../../fonts/fonts';

const Main = () => {
    return (
        <div>
            <Header />
            <Profile />
            <GlobalFonts />
            <GlobalStyle />
        </div>
    )
}

export default Main;

It is in the above two files where GlobalStyle is used.就是在上面两个文件中使用了 GlobalStyle。

import { createGlobalStyle } from "styled-components";

This import tag would imply that createGlobalStyle was not exported as default.此导入标记意味着 createGlobalStyle 未默认导出。 If you try to import a named value as default it will throw an error.如果您尝试将命名值导入为默认值,则会引发错误。

import createGlobalStyle from 'styled-components' 

would be the correct usage for a default export将是默认导出的正确用法

You could also try module.export, But I have never needed to use this in react.您也可以尝试 module.export,但我从来不需要在反应中使用它。 and most of the time the issue is in how I have implemented default/named imports大多数时候问题在于我如何实现默认/命名导入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module SyntaxError: 无法在动态导入 Nextjs 的模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module with dynamic import of Nextjs 导入 React = &#39;SyntaxError: 不能在模块外使用导入语句&#39; - Importing React = 'SyntaxError: Cannot use import statement outside a module' React 16: SyntaxError: 不能在模块外使用 import 语句 - React 16: SyntaxError: Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句——React - SyntaxError: Cannot use import statement outside a module — React 如何修复“语法错误:无法在模块外使用导入语句” - How to fix “SyntaxError: Cannot use import statement outside a module” SyntaxError:无法在 javascript 中的模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module in javascript 未捕获的 SyntaxError 无法在模块外使用 import 语句 - Uncaught SyntaxError Cannot use import statement outside a module SyntaxError:无法在模块 React JS Antd 之外使用 import 语句 - SyntaxError: Cannot use import statement outside a module React JS Antd SyntaxError: 不能在模块外使用 import 语句是什么意思? - What does it mean SyntaxError: Cannot use import statement outside a module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM