admin管理员组文章数量:1026989
I have never seen this error in my life
?
And I just found it so odd...
My code here is seen like this:
const no = require('noparenth');
(at index.js:1)
and I have literally never seen this in my life...
the library in question is one that just allows you to invoke a function without parenthesis. I made it so it would make it easier to code + make it faster. the code is as seen here:
Function.prototype.valueOf = function() {
this.call(this);
return 0;
};
Thats it...
actual error:
C:\Users\summit\testing\index.js:1
��c
SyntaxError: Invalid or unexpected token
I have never seen this error in my life
?
And I just found it so odd...
My code here is seen like this:
const no = require('noparenth');
(at index.js:1)
and I have literally never seen this in my life...
the library in question is one that just allows you to invoke a function without parenthesis. I made it so it would make it easier to code + make it faster. the code is as seen here:
Function.prototype.valueOf = function() {
this.call(this);
return 0;
};
Thats it...
actual error:
C:\Users\summit\testing\index.js:1
��c
SyntaxError: Invalid or unexpected token
Share
Improve this question
asked Aug 30, 2022 at 23:36
summit techsummit tech
211 silver badge3 bronze badges
7
- 1 Looks like you have some randomish bytes at the start of that file. You could just delete it and re-type a new one. – Jared Smith Commented Aug 30, 2022 at 23:39
- What if you want to pass a function, say as a callback...? Easier to code, let alone faster? I doubt it. – user5734311 Commented Aug 30, 2022 at 23:43
- 1 You're going to have to debug this yourself. Nothing you've posted would cause that problem. Does node work with other files? If it does, then remove lines one a time until you find what line causes the problem. If not, then your node installation is corrupt somehow. Play with it, find what things make it behave differently, collect clues. If you're still stuck, update your question with the data you found. Otherwise, there's not much we can do to help you here. – Alex Wayne Commented Aug 30, 2022 at 23:54
-
3
Also, "invoke a function without parenthesis". I'm 100% sure this is a really bad idea. Adding a getter function for properties on some your objects, sure, but doing this to the global
Function
prototype is likely break so... many... things... – Alex Wayne Commented Aug 30, 2022 at 23:56 - 2 I think you have a BOM (byte order mark) at the start of the file. – CherryDT Commented Aug 31, 2022 at 13:19
2 Answers
Reset to default 5One possible way to get that type of characters is to save a file as UTF-16 LE
and load it as if it were UTF-8
.
If you use PowerShell, it's easy to produce a file in UTF-16 LE
encoding, for example if you run echo "" > index.js
. If you open the resulting file in VSCode, you can see at the bottom right that it's UTF-16 LE
.
Note that the resulting file is not really empty, and you can verify that by inputting require('fs').readFileSync('index.js')
in the Node.js REPL.
You'll get <Buffer ff fe 0d 00 0a 00>
which, when interpreted in UTF-16 LE
, consists of byte order mark (U+FEFF
), carriage return (U+000D
) and new line character (U+000A
).
Even if you open that file with a text editor and replace everything with your text, the byte order mark will still be there (as long as you still save in UTF-16 LE
). For example, if you save const x = 0
the buffer will bee like ff fe 63 00 ...
, notice that the ff fe
still is there.
When a program attempts to load it as UTF-8
, it will get <invalid character>
<invalid character>
c
<null character>
and so on.
What you're seeing in the output is exactly from <invalid character>
<invalid character>
c
(Unicode: U+FFFD U+FFFD U+0063), obviously not a valid token in Node.js.
As for your actual function, it will only be invoked when you have type coercions (like +myFunction
or myFunction + ""
) or if you directly call it like myFunction.valueOf()
.
And I expect it to make it (slightly) slower, because it calls a user-defined function rather than just native code.
And apart from the bad idea of modifying prototypes of objects you don't own, there is limited usefulness (if any) to this.
The this.call(this)
means that you can't add more arguments to the function call.
Also, it's like myFunction.call(myFunction)
, I'm not sure why you would want to do that. And when used on a method of an object, it's like myObject.myMethod.call(myObject.myMethod)
(with this
equal to the function itself) rather than myObject.myMethod.call(myObject)
And it will break things that depend on the behavior of myFunction + ""
etc.
It seems not that easy to find an example, nevertheless I found this example :
isNative(Math.sin)
was originally supposed to return true
, but after modifying the prototype the way shown above, it became 3
I ran into this exact issue. I created the file using "echo 'test' > test.js" and just removed the string "test" in the js file and added my code. But when I piled there was random characters before the const that I had no clue where it came from.
Solution: I deleted the file and created the file manually using the UI by right clicking on my folder and clicking on New File and my code piled fine. If this doesn't work, I copy and pasted an existing JS file I had from another project into my folder and that worked too.
Disclaimer: This was not researched and purely anecdotal but it's what worked for me, hopefully it can help someone else.
I have never seen this error in my life
?
And I just found it so odd...
My code here is seen like this:
const no = require('noparenth');
(at index.js:1)
and I have literally never seen this in my life...
the library in question is one that just allows you to invoke a function without parenthesis. I made it so it would make it easier to code + make it faster. the code is as seen here:
Function.prototype.valueOf = function() {
this.call(this);
return 0;
};
Thats it...
actual error:
C:\Users\summit\testing\index.js:1
��c
SyntaxError: Invalid or unexpected token
I have never seen this error in my life
?
And I just found it so odd...
My code here is seen like this:
const no = require('noparenth');
(at index.js:1)
and I have literally never seen this in my life...
the library in question is one that just allows you to invoke a function without parenthesis. I made it so it would make it easier to code + make it faster. the code is as seen here:
Function.prototype.valueOf = function() {
this.call(this);
return 0;
};
Thats it...
actual error:
C:\Users\summit\testing\index.js:1
��c
SyntaxError: Invalid or unexpected token
Share
Improve this question
asked Aug 30, 2022 at 23:36
summit techsummit tech
211 silver badge3 bronze badges
7
- 1 Looks like you have some randomish bytes at the start of that file. You could just delete it and re-type a new one. – Jared Smith Commented Aug 30, 2022 at 23:39
- What if you want to pass a function, say as a callback...? Easier to code, let alone faster? I doubt it. – user5734311 Commented Aug 30, 2022 at 23:43
- 1 You're going to have to debug this yourself. Nothing you've posted would cause that problem. Does node work with other files? If it does, then remove lines one a time until you find what line causes the problem. If not, then your node installation is corrupt somehow. Play with it, find what things make it behave differently, collect clues. If you're still stuck, update your question with the data you found. Otherwise, there's not much we can do to help you here. – Alex Wayne Commented Aug 30, 2022 at 23:54
-
3
Also, "invoke a function without parenthesis". I'm 100% sure this is a really bad idea. Adding a getter function for properties on some your objects, sure, but doing this to the global
Function
prototype is likely break so... many... things... – Alex Wayne Commented Aug 30, 2022 at 23:56 - 2 I think you have a BOM (byte order mark) at the start of the file. – CherryDT Commented Aug 31, 2022 at 13:19
2 Answers
Reset to default 5One possible way to get that type of characters is to save a file as UTF-16 LE
and load it as if it were UTF-8
.
If you use PowerShell, it's easy to produce a file in UTF-16 LE
encoding, for example if you run echo "" > index.js
. If you open the resulting file in VSCode, you can see at the bottom right that it's UTF-16 LE
.
Note that the resulting file is not really empty, and you can verify that by inputting require('fs').readFileSync('index.js')
in the Node.js REPL.
You'll get <Buffer ff fe 0d 00 0a 00>
which, when interpreted in UTF-16 LE
, consists of byte order mark (U+FEFF
), carriage return (U+000D
) and new line character (U+000A
).
Even if you open that file with a text editor and replace everything with your text, the byte order mark will still be there (as long as you still save in UTF-16 LE
). For example, if you save const x = 0
the buffer will bee like ff fe 63 00 ...
, notice that the ff fe
still is there.
When a program attempts to load it as UTF-8
, it will get <invalid character>
<invalid character>
c
<null character>
and so on.
What you're seeing in the output is exactly from <invalid character>
<invalid character>
c
(Unicode: U+FFFD U+FFFD U+0063), obviously not a valid token in Node.js.
As for your actual function, it will only be invoked when you have type coercions (like +myFunction
or myFunction + ""
) or if you directly call it like myFunction.valueOf()
.
And I expect it to make it (slightly) slower, because it calls a user-defined function rather than just native code.
And apart from the bad idea of modifying prototypes of objects you don't own, there is limited usefulness (if any) to this.
The this.call(this)
means that you can't add more arguments to the function call.
Also, it's like myFunction.call(myFunction)
, I'm not sure why you would want to do that. And when used on a method of an object, it's like myObject.myMethod.call(myObject.myMethod)
(with this
equal to the function itself) rather than myObject.myMethod.call(myObject)
And it will break things that depend on the behavior of myFunction + ""
etc.
It seems not that easy to find an example, nevertheless I found this example :
isNative(Math.sin)
was originally supposed to return true
, but after modifying the prototype the way shown above, it became 3
I ran into this exact issue. I created the file using "echo 'test' > test.js" and just removed the string "test" in the js file and added my code. But when I piled there was random characters before the const that I had no clue where it came from.
Solution: I deleted the file and created the file manually using the UI by right clicking on my folder and clicking on New File and my code piled fine. If this doesn't work, I copy and pasted an existing JS file I had from another project into my folder and that worked too.
Disclaimer: This was not researched and purely anecdotal but it's what worked for me, hopefully it can help someone else.
本文标签: javascriptNodejs SyntaxError Invalid or unexpected token (but its weird)Stack Overflow
版权声明:本文标题:javascript - Node.js SyntaxError: Invalid or unexpected token (but its weird) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745659405a2161797.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论