Node: Node server.js execution fails - Win10 node 4.4.3 npm 3.10.7

Created on 27 Sep 2016  路  10Comments  路  Source: nodejs/node

  • Version: v.4.4.3
  • npm v.: v.3.10.7
  • Platform: 64-bit (Windows 10)
  • Subsystem: express ^4.13.4

I was trying to start an express server, but on running node ./server.js an error was thrown:

D:\projects\react-auth\server\server.js:1
(function (exports, require, module, __filename, __dirname) { 锟斤拷c
                                                              ^

SyntaxError: Unexpected token ILLEGAL
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

The package.json file I used is the following: https://github.com/auth0-blog/react-auth0/blob/master/server/package.json

And the server.js file is the following: https://github.com/auth0-blog/react-auth0/blob/master/server/server.js

I am using nvm for Windows to version node.

module question

Most helpful comment

@javlc It looks like your editor saves the file using UTF-16 as its encoding, rather than UTF-8 which is Node expects, so the best way forward is probably checking your editor (or git?) settings.

I鈥檓 closing this as it鈥檚 very unlikely that it鈥檚 a bug with Node itself, but feel free to ask any follow-up questions you may have!

I ran into this issue not quite long. This was because I used this powershell command to create the file.
echo sometext > server.js. This command is supposed to copy "sometext" into server.js. It creates the file if it doesn't already exist.

All 10 comments

Can you post the output of node -p "require('fs').readFileSync('server.js')"?

@addaleax: the output is:
<Buffer ff fe 63 00 6f 00 6e 00 73 00 74 00 20 00 65 00 78 00 70 00 72 00 65 00 73 00 73 00 20 00 3d 00 20 00 72 00 65 00 71 00 75 00 69 00 72 00 65 00 28 00 ... >

@javlc It looks like your editor saves the file using UTF-16 as its encoding, rather than UTF-8 which is Node expects, so the best way forward is probably checking your editor (or git?) settings.

I鈥檓 closing this as it鈥檚 very unlikely that it鈥檚 a bug with Node itself, but feel free to ask any follow-up questions you may have!

@addaleax : It doesn't look to me like my file's encoding is wrong.
The failure is as node was reading through the express library code, which is where the error is pointing.
I am not too much into digging the node modules code. If you point to me where I could start looking, I'll be grateful.
I particularly want the _probable?_ location where this bit is:
(function (exports, require, module, __filename, __dirname) {

Thanks.

It doesn't look to me like my file's encoding is wrong.

ff fe is the UTF-16 LE byte order mark, and bytes like 63 00 6f 00 6e 00 73 00 74 00 definitely are latin characters encoded using UTF-16 LE.

Since you鈥檝e expressed interest in looking at the relevant core files, here is where file contents are being read, with the explicit assumption that the content is valid UTF-8.

I particularly want the probable? location where this bit is:

Here is where it鈥檚 applied to the module source (content of Module.wrap) and here is the definition of that bit.

Hope that helps a bit!

Actually that helps a lot, heaps of bits.
I will need to pinpoint the reason of the different encoding in such file (I did not knowingly did that, for sure).
Thank you for the insight.

As an FYI,
I recreated the file from the editor and got the following output:
<Buffer 63 6f 6e 73 74 20 65 78 70 72 65 73 73 20 3d 20 72 65 71 75 69 72 65 28 27 65 78 70 72 65 73 73 27 29 3b 0a 63 6f 6e 73 74 20 61 70 70 20 3d 20 65 78 ... >

All I can think about a reason, is that the initial file (the one with the encoding UTF-16) was created using either CMD or powershell (don't quite recall as I was experimenting a quicker way of creating files without using any GUI).

I recreated the file from the editor and got the following output:

Yeah, that looks about right. :)

It doesn't look to me like my file's encoding is wrong.

ff fe is the UTF-16 LE byte order mark, and bytes like 63 00 6f 00 6e 00 73 00 74 00 definitely are latin characters encoded using UTF-16 LE.

Since you鈥檝e expressed interest in looking at the relevant core files, here is where file contents are being read, with the explicit assumption that the content is valid UTF-8.

I particularly want the probable? location where this bit is:

Here is where it鈥檚 applied to the module source (content of Module.wrap) and here is the definition of that bit.

Hope that helps a bit!

This is a brilliant explanation.

@javlc It looks like your editor saves the file using UTF-16 as its encoding, rather than UTF-8 which is Node expects, so the best way forward is probably checking your editor (or git?) settings.

I鈥檓 closing this as it鈥檚 very unlikely that it鈥檚 a bug with Node itself, but feel free to ask any follow-up questions you may have!

I ran into this issue not quite long. This was because I used this powershell command to create the file.
echo sometext > server.js. This command is supposed to copy "sometext" into server.js. It creates the file if it doesn't already exist.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

addaleax picture addaleax  路  3Comments

akdor1154 picture akdor1154  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments

stevenvachon picture stevenvachon  路  3Comments

srl295 picture srl295  路  3Comments