23

I am writing some node command line utilities. They all start with the line:

#!/usr/bin/env node

With Eclipse Juno and the Nodeclipse Node.js plugin, this line of code produces an error as shown:

Node shebang eclipse error

OK, so # is not a valid comment character in javascript, but it is a valid character in Linux/UNIX as the shebang of the first line in a file. But how can I set up Eclipse to ignore this error? This is a problem for me because code formatting does not work if you have errors. I have to delete the line. Hit CTRL-SHIFT-F and add the line back.

I have tried a lot of things and researched, but I can't find an answer.

There is a duplicate question out there, eclipse javascript syntax error on hashbang line, but my question has more info.

EDIT:

Looks like there was something added to jshint to allow shebangs in the first line. Maybe I need to update my node-eclipse, or maybe the node-eclipse project needs to update jshint?

  • My jshint eclipse integration is version 0.9.6.
  • My nodeclipse is 0.4.0.20130519...

I upgraded to

  • jshint eclipse integration 0.9.9.20131029
  • nodeclipse 0.7.0.20131101

That did not help.

Here is my JSHint version in eclipse:

enter image description here

EDIT 2:

Thanks for the answer VonC. But I think this shows that I do not have a BOM in the file. Any other ideas?

$ od -N 20 -t x1 hello.js
0000000 23 21 2f 75 73 72 2f 62 69 6e 2f 65 6e 76 20 6e
0000020 6f 64 65 0a
0000024

EDIT 3:

With regard to Paul Verest's answer below, I tried to turn off JSDT validation, but I can't seem to do it. I unchecked "Enable JavaScript semantic validation" (In Eclipse, see Window > Preferences > JavaScript > Validator > Errors/Warnings), but the issue remains.

I am now uninstalling Eclipse Web Developer Tools 3.4.2. That did not seem to help and now my CSS and HTML editors are gone. Now I have tried to disable JSDT validation by following some of the ideas in this SO question, How do I remove javascript validation from my eclipse project?.

So I went into my project properties and went to JavaScript > Validation. I have set everything to "Enabled project specific settings" and unchecked "Errors/Warnings", "JSDOC", etc. Even so, I think validation is still running since the problem persists! My "Builders" Properties only lists the "JSHint Problem Checker" which is enabled. (I am doing all this on a new test project with a hello.js).

EDIT 4, THE ANSWER

It was not easy, but I ended up hacking the .project file in Eclipse. I had this:

<natures>
    <nature>org.nodeclipse.ui.NodeNature</nature>
    <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>

I removed the jsdt nature and now the error on the shebang line is GONE! I did this in my test project and my real project. It worked! This is actually pretty helpful since I can format the file and I'm actually running jshint now.

halfer
  • 18,701
  • 13
  • 79
  • 158
Jess
  • 20,424
  • 18
  • 108
  • 130
  • I take it by the up votes that this is a problem for other people too. – Jess Nov 13 '13 at 14:52
  • For Cordova - remove platforms/android/cordova with js exlude path : http://stackoverflow.com/questions/3131878/how-do-i-remove-javascript-validation-from-my-eclipse-project – nonozor Aug 11 '15 at 08:25

2 Answers2

6

There are 2 option for JavaScript validation in Eclipse:

  • JSDT
  • JSHint (default since Nodeclipse 0.7)

As configration is stored per project, copy .* settings files from project created with 0.7 or re-configure it manually (just compare .* files with newly created project). Then put .jshintrc file like https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.ui/templates/.jshintrc

Try to check JSHint options, if it is possible.

Note that with JSHint usage, ~~this question becomes general JSHint question (not Eclipse or Nodeclipse related).~~

UPDATE:

.project content since 0.7 :

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>ProjectName</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>com.eclipsesource.jshint.ui.builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.nodeclipse.ui.NodeNature</nature>
        <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
    </natures>
</projectDescription>

As @Jess discovered this was error shown by JSDT, not JSHInt, so removing <nature>org.eclipse.wst.jsdt.core.jsNature</nature> will stop JSDT from displaying it (even with JavaScript semantic validation already turned off since 0.7)

UPDATE 3:

Drawback of <nature>org.eclipse.wst.jsdt.core.jsNature</nature> removed will be that code assist and click-though to definition will not work (It actually works in rare cases when JSDocs are defined e.g. http://www.nodeclipse.org/nodejs/javascript/sources/books/2013/10/22/JSDT-require-JSDoc.html or within 1 .js file)

JSDoc support

Even click-through to definition click-through to definition

Paul Verest
  • 51,779
  • 39
  • 174
  • 288
  • More precise answer than mine. +1 – VonC Nov 13 '13 at 12:19
  • Thank you! Please see edits to the question. I believe that I am finally running JSHint since I see some problems that JSHint will detect, but JSDT will not. However, the problem remains. There is still an error on the shebang line. – Jess Nov 13 '13 at 14:42
  • 1
    Yes, finally solved. Though I don't remember now what would be implication of `org.eclipse.wst.jsdt.core.jsNature` removed. It may be that code assist and click-though to definition will not work (It actually works in rare cases when JSDocs are definied http://www.nodeclipse.org/nodejs/javascript/sources/books/2013/10/22/JSDT-require-JSDoc.html) – Paul Verest Nov 14 '13 at 07:06
  • I attempted this and i got back the same error with the shebang line – devdar Mar 15 '14 at 16:59
  • Then you are likely to be using old Nodeclipse version (latest is 0.11 http://www.nodeclipse.org/history). JSDT is not used for validation, but JSHint-Eclipse. Please create new question or an issue in Nodeclipse if there is need to follow. – Paul Verest Mar 16 '14 at 12:45
  • Is the answer that you have to remove the `jsNature` or can one fix the error in JSHint config? The link to https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.ui/common-templates/.jshintrc is dead. Can you please edit to precisely say what is the JSHint [option](http://www.jshint.com/docs/options/) that needs to be set to stop this error? – Fuhrmanator Sep 19 '14 at 15:11
  • To deactivate JSDT's flagging of this error, remove the source files from JSDT's include path in Project Properties > JavaScript > Include Path > Source. Select all the folders and click Remove. – Fuhrmanator Sep 19 '14 at 15:56
  • The link https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.ui/templates/.jshintrc is not dead, but GitHub sometimes failes to find .jshintrc under https://github.com/Nodeclipse/nodeclipse-1/tree/master/org.nodeclipse.ui/templates – Paul Verest Sep 22 '14 at 03:38
  • For your solution please add an answer or start discussion at https://github.com/Nodeclipse/nodeclipse-1/issues – Paul Verest Sep 22 '14 at 03:38
4

Update:

The issue entered in nodeclipse points out to JSHint issue 66.
As Paul Verest remarks in his answer (upvoted), this could be as simple as making sure JSHint check the code.
Since commit 63da9, JSHint knows how to ignore that shebang directive.

// If the first line is a shebang (#!), remove it and move on.
// Shebangs are used by Node scripts.
if (lines[0] && lines[0].substr(0, 2) == '#!')
  lines.shift();

Original answer

Are you sure '#' is not a valid character (yet used in this question)?

Double-check the encoding of your node.js file, because if it is UTF-8 with BOM, then the javascript couldn't be launched properly.

See "What's different between utf-8 and utf-8 without BOM?", and the wikipedia article on shebang (section "Magic number")

The shebang characters are represented by the same two bytes in extended ASCII encodings, including UTF-8, which is commonly used for scripts and other text files on current Unix-like systems.
However, UTF-8 files may begin with the optional byte order mark (BOM); if the "exec" function specifically detects the bytes 0x23 0x21, then the presence of the BOM (0xEF 0xBB 0xBF) before the shebang will prevent the script interpreter from being executed.
Some authorities recommend against using the byte order mark in POSIX (Unix-like) scripts, for this reason and for wider interoperability and philosophical concerns.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • On further thought, I appreciate the attempt, but I don't think this is the answer. The script runs fine, and jshint running in eclipse seems to be running fine also. **[jshint](http://www.nodeclipse.org/history)** is detecting the first line as an error since it is not valid javascript. – Jess Nov 11 '13 at 19:21
  • @Jess I simply meant: "Is it in UTF-8 *without* BOM, or UTF-8 *with* BOM?" The BOM extra first character could trigger the "invalid token" error message. – VonC Nov 11 '13 at 19:42
  • Hi @VonC. This is a really good idea, but I don't have a BOM. I edited the question to show the first bytes in HEX. – Jess Nov 11 '13 at 20:12
  • It seems that eclipse typical operation is **[no BOM](http://stackoverflow.com/questions/2905582/working-with-utf-8-files-in-eclipse)** and also **[this](http://www.eclipse.org/forums/index.php/m/725818/)**. – Jess Nov 11 '13 at 20:17
  • 1
    @Jess I see (and agree with my old answer). Perhaps that would be something to mention to https://github.com/Nodeclipse/eclipse-node-ide/issues, or rather https://github.com/Nodeclipse/nodeclipse-1/issues, unless it is JSHint which is too picky. – VonC Nov 11 '13 at 20:28
  • thanks for hanging in there with me. I reported an issue at github. We will see what they say: https://github.com/Nodeclipse/nodeclipse/issues/3 – Jess Nov 11 '13 at 20:44
  • +1 for being so darn helpful! :D I'm waiting to see the problem solved before marking an answer, but I won't forget. – Jess Nov 12 '13 at 14:13
  • Is there a solution to this yet i am having the same issue i had no other choice but to remove my project from JavaScript 'Include Path' in Eclipse – devdar Mar 15 '14 at 16:58