英文:
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 'react';
^^^^^^
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 'react';
import axios from 'axios';
export default function Register() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleRegister = async (e: { preventDefault: () => void; }) => {
e.preventDefault();
try {
const response = await axios.post('/api/register', { 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 (
<div>
<h1>Register</h1>
<form onSubmit={handleRegister}>
<input
type="email"
placeholder="Email"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
<input
type="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
<button type="submit">Register</button>
</form>
</div>
);
}
答案1
得分: 0
在 package.json 中,我添加了 "type": "module",这解决了问题。
英文:
In package.json, I added "type": "module", which fixed the problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论