Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
I can't launch my localhost websocket server

I am trying to create a localhost websocket server with node 16.13 and I don't know why but I nothing seems to work. I can't launch my index.js with node index.js, I receive this error message -> Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.(Use node --trace-warnings ... to show where the warning was created)

Also this message in the browser Access to script at 'file:///.../server/index.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

In my html file I've used <script type="module" src="./server/index.js"></script>

(index.js file) 
import WebSocket from 'ws';
    
const wss = new WebSocket.Server({ port: 8080});

const ws = new WebSocket("ws://localhost:8080");

ws.addEventListener("open", () => {
    console.log("We're connected")

    ws.send("Hey, how is going?");
});

ws.on("connection", ws => {
    console.log("New client Connected");

    ws.on("message", data => {
        console.log(`Client has sent Us: ${data}`);
    });
    
});

ws.on("close", () => {
    console.log("client has disconnected");
});```
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are importing your module with ESM syntax and nodejs only support CommonJs

Change

import WebSocket from 'ws';

For:

const ws = required('ws')
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!