2016-07-01 53 views
0

我是JSHint的新手,並試圖運行它來驗證我的Javascript。我安裝了node.js,當我嘗試運行JSHint時,我得到下面的錯誤。用HTML運行JSHint並出現錯誤

C:\Users\574839\Desktop\testscr.js:1 
(function (exports, require, module, __filename, __dirname) { <!DOCTYPE html> 
                 ^
SyntaxError: Unexpected token < 
at Object.exports.runInThisContext (vm.js:53:16) 
at Module._compile (module.js:513:28) 
at Object.Module._extensions..js (module.js:550:10) 
at Module.load (module.js:458:32) 
at tryModuleLoad (module.js:417:12) 
at Function.Module._load (module.js:409:3) 
at Function.Module.runMain (module.js:575:10) 
at startup (node.js:160:18) 
at node.js:456:3 

我有以下選項的.jshintrc文件:

{ 
"bitwise": true, 
"camelcase": true, 
"curly": true, 
"eqeqeq": true, 
"es3": false, 
"forin": true, 
"freeze": true, 
"immed": true, 
"indent": 4, 
"latedef": "nofunc", 
    "newcap": true, 
    "noarg": true, 
    "noempty": true, 
    "nonbsp": true, 
    "nonew": true, 
"plusplus": false, 
"quotmark": "single", 
"undef": true, 
"unused": false, 
"strict": false, 
"maxparams": 10, 
"maxdepth": 5, 
"maxstatements": 40, 
"maxcomplexity": 8, 
"maxlen": 120, 
"asi": false, 
"boss": false, 
"debug": false, 
"eqnull": true, 
"esnext": false, 
"evil": false, 
"expr": false, 
"funcscope": false, 
"globalstrict": false, 
"iterator": false, 
"lastsemic": false, 
"laxbreak": false, 
"laxcomma": false, 
"loopfunc": true, 
"maxerr": false, 
"moz": false, 
"multistr": false, 
"notypeof": false, 
"proto": false, 
"scripturl": false, 
"shadow": false, 
"sub": true, 
"supernew": false, 
"validthis": false, 
"noyield": false, 
"browser": true, 
"node": true, 
"globals": { 
    "angular": false, 
    "$": false 
    } 
} 

可有人請指教,爲什麼我的錯誤?

我的JS代碼如下。

<!DOCTYPE html> 
<html> 
<body> 

<h1>What Can JavaScript Do?</h1> 

<p id="demo">JavaScript can change HTML content.</p> 

<button type="button" onclick="document.getElementById('demo').innerHTML='Hello JavaScript!'">Click Me!</button> 

</body> 
</html> 

回答

1

這是一個HTML文件,而不是一個JavaScript文件。但是幸運的是,如果你通過--extract選項,JSHint可以在HTML文件中工作。

documentation

--extract=[auto|always|never]

告訴JSHint到掉毛之前,從HTML文件中提取的JavaScript:

tmp ☭ cat test.html 
<html> 
    <head> 
    <title>Hello, World!</title> 
    <script> 
     function hello() { 
     return "Hello, World!"; 
     } 
    </script> 
    </head> 
    <body> 
    <h1>Hello, World!</h1> 
    <script> 
     console.log(hello()) 
    </script> 
    </body> 
</html> 

tmp ☭ jshint --extract=auto test.html 
test.html: line 13, col 27, Missing semicolon. 

1 error 

如果你將它設置爲總是 JSHint總會嘗試提取 JavaScript。如果你將它設置爲auto如果文件看起來像是一個HTML文件,它將僅嘗試 。

與此同時,它仍然不能識別像HTML這樣的屬性事件處理程序,如onclick這種情況,這是一種不好的做法。如果你真的想你的Javascript的HTML你已經把它在腳本標記,如下列:

testscr.html:

<!DOCTYPE html> 
<html> 
    <body> 

    <h1>What Can JavaScript Do?</h1> 

    <p id="demo">JavaScript can change HTML content.</p> 

    <button type="button" onclick="sayHello()">Click Me!</button> 
    <script> 
     function sayHello() { 
     document.getElementById('demo').innerHTML='Hello JavaScript!'; 
     } 
    </script> 
    </body> 
</html> 

此外,它看起來像你傳遞到節點,像node testscr.js,但你應該做的是把它傳遞給jshint:jshint --extract=always testscr.js

+0

有沒有辦法告訴JSHint從HTML中提取JS並只測試JS? – InfiniteLoop

+0

感謝法裏德!最後一個問題。除了在控制檯中顯示錯誤/警告之外,它是否會像日誌文件一樣生成?我不確定在哪裏尋找測試的輸出。 – InfiniteLoop

+0

劃傷我說的話,再看看答案。我更新了它。如果你想輸出到一個文件中,只需管它:'jshint --extract = always testscr.js> output.txt' –