Truffle - How to run Linter command before compile or migrate?

I am using truffle with VS Code IDE to build and smart contract. Currently i can run Linter via command (run on console), I am wondering if i can run automate this and run linter command before compile/migrate, and it fails if there is any linter issues.

1 Like

Welcome to community!
I think this is depends on how you set for your config of the Linter.


Hi @lalitbit welcome.

As per @skyge the answer is it depends.
Some of the potential options are as follows (though @skyge may have a better solution):

You could look at doing linting as part of Continuous Integration.

I use Juan Blanco's Solidity extension, so that in VS Code I get errors and warnings about my Solidity formatting and then use linting to catch anything I have missed.

You could add a script to package.json such as the example compile below, which first runs lint and then compiles.

  "scripts": {
    "compile": "npm run lint && truffle compile",
    "coverage": "solidity-coverage && cat coverage/ | coveralls",
    "lint": "npm run lint:js && npm run lint:sol",
    "lint:fix": "npm run lint:js:fix",
    "lint:js": "eslint .",
    "lint:js:fix": "eslint . --fix",
    "lint:sol": "solhint --max-warnings 0 \"contracts/**/*.sol\"",
    "test": "truffle test"

This is based on the OpenZeppelin package.json scripts.

1 Like

Hi @lalitbit
How did you get on Linting before compiling?

If the above reply solved your issue can you mark it as the solution, otherwise please ask more questions.