简体   繁体   English

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

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

I'm trying to deploy my web app into heroku and I have been getting this problem since I added the "emailjs" module.我正在尝试将我的 web 应用程序部署到 heroku 中,自从我添加了“emailjs”模块以来,我一直遇到这个问题。 Before that my deploy worked well and now im getting the same problem every time.在此之前,我的部署运行良好,现在我每次都遇到同样的问题。 to matter what i try.无论我尝试什么。 Heroku logs results after trying to deploy Heroku 尝试部署后记录结果

**this is my code in index.js: ** ` **这是我在 index.js 中的代码:**`

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

reportWebVitals();

` `

The thing is: when i change the "import" to "require" the code throws me more and more errors.问题是:当我将“导入”更改为“要求”时,代码会抛出越来越多的错误。

**this is my packakge.json: **这是我的包裹。json:

** **

{
  "name": "chipsi-puntos",
  "version": "0.1.0",
  "private": true,
  "engines": {
    "node": "18.12.1"
  },
  "dependencies": {
    "@emailjs/browser": "^3.10.0",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.4.3",
    "react-scripts": "5.0.1",
    "sass": "^1.56.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"
    ]
  }
}

Im deployng thru heroku using github. If you have any advice, ill be so thankfull <3我使用 github 通过 heroku 进行部署。如果您有任何建议,我将不胜感激 <3

I've tried all the solutions that are written here: "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6我已经尝试了这里写的所有解决方案: “Uncaught SyntaxError: Cannot use import statement outside a module” when importing ECMAScript 6

I also tried to switch the "import" to "require" and didnt work.我还尝试将“导入”切换为“需要”,但没有用。

I expect my code to do well on heroku.我希望我的代码在 heroku 上运行良好。

Pd: it works 100% OK on my localhost. Pd:它在我的本地主机上 100% 正常工作。

I think when you're using import statements you should have "type": "module" in the package.json file.我认为当您使用 import 语句时,您应该在 package.json 文件中包含 "type": "module" 。 Since you don't have it, you should use require statements instead.既然你没有它,你应该改用 require 语句。 If I were you, I'll try adding "type": "module" to the package.json.如果我是你,我会尝试将"type": "module"添加到 package.json。 You could also try invoking the flag: --experimental-modules您也可以尝试调用标志: --experimental-modules

More info on that can be found here有关更多信息,请参见 此处

You need to add "type": "module" in your package.json file.您需要在package.json文件中添加"type": "module"

{
  "name": "Name of your app",
  "version": "0.1.0",
  "homepage": ".",
  "private": true,
  "type": "module",
  ...
}

Change Your index.js file:更改您的 index.js 文件:

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById("root")
);

暂无
暂无

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

相关问题 SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module 带有 npm 模块的“语法错误:无法在模块外使用导入语句” - “SyntaxError: Cannot use import statement outside a module” with a npm module 类型:模块导致:语法错误:无法在模块外使用导入语句 - type: module is causing: SyntaxError: Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句是什么意思? - What does it mean SyntaxError: Cannot use import statement outside a module? SyntaxError:无法在模块外部使用导入语句(来自依赖项) - SyntaxError: Cannot use import statement outside a module (from dependency) 如何解决“未捕获的语法错误:无法在模块外使用导入语句”? - How to resolve “Uncaught SyntaxError: Cannot use import statement outside a module”? 依赖项中的“SyntaxError:无法在模块外使用导入语句” - “SyntaxError: Cannot use import statement outside a module” in a dependency NodeJS --&gt; SyntaxError: 不能在模块外使用 import 语句 - NodeJS --> SyntaxError: Cannot use import statement outside a module 预呈现会导致 SyntaxError:无法在模块外使用 import 语句 - Prerendering causes a SyntaxError: Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句 | 在 android:app:bundleReleaseJsAndAssets - SyntaxError: Cannot use import statement outside a module | on android :app:bundleReleaseJsAndAssets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM