简体   繁体   中英

Why am I getting a syntax error or server not defined error when running node server.js in command line for windows?

I have tried opening my server.js file multiple times as well as a test file i named helloworld.js in cmd as well as gitbash and gitcmd. Everytime I am returned with some sort of error whether it is a syntax:

在此处输入图片说明

or a cannot find module socket.io error

在此处输入图片说明

I have tried many things and ways including downloading gitbash and trying it there. I am fully new to any coding or dev and have read all of the other 20+ psots regarding the same thing but none of them seemed to have been resolved or documented?

Thank you

In your first attempt:

node server.js is a command you are expected to run on your shell (bash, Windows Power Shell, etc). It launches Node.js and tells it to run the server.js module.

You are running node at the shell, which launches node and presents you with a REPL. You are then trying to run node server.js as if it were JavaScript (which it isn't, because it is shell).

Where you are currently typing node , instead type node server.js .


In your second attempt:

You are trying to load a module called socket.io, but it is not installed. You have to install it first.

Look at the getting started guide for Socket.io.

It tells you how to set up a package manifest:

First let's create a package.json manifest file that describes our project. I recommend you place it in a dedicated empty directory (I'll call mine chat-example).

 { "name": "socket-chat-example", "version": "0.0.1", "description": "my first socket.io app", "dependencies": {} }

and install modules while recording them as dependancies for your package:

During development, socket.io serves the client automatically for us, as we'll see, so for now we only have to install one module:

 npm install --save socket.io

If you have not already, go through the Getting Started guide on the Socket.io website. From the guide:

  • Make sure you have Node along with npm installed
  • Perform an npm init to initialize your project
  • perform an npm install --save express
  • perform npm install --save socket.io
  • When you want to run your Node app, do node server.js

Here is an example of what a correctly-formatted Socket.io server file should look like:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  console.log('a user connected');
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

Make sure you have this in your client HTML file:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io();
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM