SyntaxError: 无法在模块外部使用import语句 next.js

huangapple go评论65阅读模式
英文:

SyntaxError: Cannot use import statement outside a module next.js

问题

以下是您提供的代码的翻译:

我在尝试在我的Next.js项目中运行register.tsx页面时一直收到下面的错误我有一个使用Typescript的Next.js项目

import React, { useState } from 'react';
^^^^^^

SyntaxError: 无法在模块外部使用import语句
 internalCompileFunction (node:internal/vm:73:18) 
 wrapSafe (node:internal/modules/cjs/loader:1176:20) 
 Module._compile (node:internal/modules/cjs/loader:1218:27) 
 Module._extensions..js (node:internal/modules/cjs/loader:1308:10) 
 Module.load (node:internal/modules/cjs/loader:1117:32) 
 Module._load (node:internal/modules/cjs/loader:958:12) 
 Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) 
 node:internal/main/run_main_module:23:47 

这是我的register.tsx文件中的代码我在想这可能与axios有关但我将代码中的那部分注释掉后仍然出现相同的错误

import React, { useState } from 'react';
import axios from 'axios';

export default function Register() {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');

  const handleRegister = async (e) => {
    e.preventDefault();

    try {
      const response = await axios.post('/api/register', { email, password });
      console.log(response.data); // 注册成功,根据需要处理响应
    } catch (error) {
      console.log(error.response.data); // 注册失败,根据需要处理错误
    }
  };

  return (
    <div>
      <h1>注册</h1>
      <form onSubmit={handleRegister}>
        <input
          type="email"
          placeholder="邮箱"
          value={email}
          onChange={(e) => setEmail(e.target.value)}
          required
        />
        <input
          type="password"
          placeholder="密码"
          value={password}
          onChange={(e) => setPassword(e.target.value)}
          required
        />
        <button type="submit">注册</button>
      </form>
    </div>
  );
}
英文:

I keep getting this error below when I try to run the register.tsx page in my next.js project. I have a Next.Js project using typescript.

    import React, { useState } from &#39;react&#39;;
^^^^^^
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1176:20)
at Module._compile (node:internal/modules/cjs/loader:1218:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47

Here is the code in my register.tsx file. I was thinking it has something to do with axios, but I commented that part of the code and it is still giving me the same error.

   import React, { useState } from &#39;react&#39;;
import axios from &#39;axios&#39;;
export default function Register() {
const [email, setEmail] = useState(&#39;&#39;);
const [password, setPassword] = useState(&#39;&#39;);
const handleRegister = async (e: { preventDefault: () =&gt; void; }) =&gt; {
e.preventDefault();
try {
const response = await axios.post(&#39;/api/register&#39;, { email, password });
console.log(response.data); // Registration successful, handle the response as needed
} catch (error) {
console.log(error.response.data); // Registration failed, handle the error as needed
}
};
return (
&lt;div&gt;
&lt;h1&gt;Register&lt;/h1&gt;
&lt;form onSubmit={handleRegister}&gt;
&lt;input
type=&quot;email&quot;
placeholder=&quot;Email&quot;
value={email}
onChange={(e) =&gt; setEmail(e.target.value)}
required
/&gt;
&lt;input
type=&quot;password&quot;
placeholder=&quot;Password&quot;
value={password}
onChange={(e) =&gt; setPassword(e.target.value)}
required
/&gt;
&lt;button type=&quot;submit&quot;&gt;Register&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;
);
}

答案1

得分: 0

在 package.json 中,我添加了 "type": "module",这解决了问题。

英文:

In package.json, I added "type": "module", which fixed the problem.

huangapple
  • 本文由 发表于 2023年5月17日 23:07:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定