Issue
I am making a react application for the first time. I am running app.js on the client folder and its returning the following error:
C:\Program Files\nodejs\node.exe .\client\src\App.js
Uncaught SyntaxError C:\Users\nayye\alumni-b\client\src\App.js:1
import "./App.css";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1153:20)
at Module._compile (node:internal/modules/cjs/loader:1205:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
at Module.load (node:internal/modules/cjs/loader:1091:32)
at Module._load (node:internal/modules/cjs/loader:938:12)
at executeUserEntryPoint (node:internal/modules/run_main:83:12)
at <anonymous> (node:internal/main/run_main_module:23:47)
Process exited with code 1
This is my app.js file:
import "./App.css";
import axios from "axios";
import { useEffect } from "react";
function App() {
useEffect(() => {
axios.get("http://localhost:3001/users").then((response) => {
console.log(response.data);
});
}, []);
return (
<div className="App">
</div>
);
}
export default App;
My index.js file in the server folder is running without any issues and i tested it on postman as well.
Solution
Update For Node.js / NPM
Add "type": "module" to your package.json file.
{
// ...
"type": "module",
// ...
}
Answered By - joswxc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.