javascript - How to compile or check syntax error in Node.js application - Stack Overflow

I have created a node.js application. I need to pile or do a check whether syntax error occurs before r

I have created a node.js application. I need to pile or do a check whether syntax error occurs before running the node.js service.I need to integrate this in jenkins. Kindly guide me to validate the node.js application

I have created a node.js application. I need to pile or do a check whether syntax error occurs before running the node.js service.I need to integrate this in jenkins. Kindly guide me to validate the node.js application

Share Improve this question asked Mar 16, 2016 at 6:21 user2439278user2439278 1,2349 gold badges45 silver badges84 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

using eslint package https://www.npmjs./package/eslint It's a very mon library to check the syntax of any JS/TS framework

Node.js provides CLI option --check or -c for checking a given file for syntax errors without running it. When working with Linux or MacOS a mand like the following one helps with checking all obvious javascript files in a folder for containing basically working code:

find -name "*.js" | xargs node -c

Look into TypeScript. It is a language which allows you to get autoplete with types and fails pilation if you have some syntax errors. It is a superset of JavaScript and transpiles to JavaScript.

Using

node --check

Only finds very grave errors and can be tricky to execute.

I would suggest to use a linter like 'eslint'. Because:

  • It integrates easily with (npm lint or yarn lint)
  • It catches almost all possible errors (way more than node -check)

You can use 'node-syntax-error' module. it helps you to detect and report syntax errors in source code strings.

When you type node src.js you get a friendly error report about exactly where the syntax error is. This module lets you check for syntax errors and report them in a similarly friendly format that wrapping a try/catch around Function() or vm.runInNewContext() doesn't get you.

For example:

var fs = require('fs');
var check = require('syntax-error');

var file = __dirname + '/src.js';
var src = fs.readFileSync(file);

var err = check(src, file);
if (err) {
    console.error('ERROR DETECTED' + Array(62).join('!'));
    console.error(err);
    console.error(Array(76).join('-'));
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743715854a4494974.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信