Husky giving error SyntaxError: Use of const in strict mode

假装没事ソ 提交于 2019-11-28 05:13:14

问题


I am using Husky with Lint-staged and Stylelint

  "scripts": {
    "precommit": "lint-staged",

  },
  "lint-staged": {
    "*.scss": ["stylelint --syntax scss"
    ]
  },

OS - Latest OSX

Node - 6.10.0

NPM - 3.10.00

I'm getting this error on git commit

> husky - npm run -s precommit

/Users/jitendravyas/app/node_modules/lint-staged/src/index.js:6
const path = require('path')
^^^^^
SyntaxError: Use of const in strict mode.
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/jitendravyas/app/node_modules/lint-staged/index.js:2:1)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)

> husky - pre-commit hook failed (add --no-verify to bypass)
> husky - to debug, use 'npm run precommit'

回答1:


I was using node 7.10.0 and had the same problem. I found an issue logged on it's repo where mmoutenot posted an interesting reply which got me thinking so I looked into it further and found the solution!

Husky assumes that everyone uses nvm to manage node versions and looks for it under NVM_DIR(set to $HOME/.nvm) or if installed with brew BREW_NVM_DIR(set to /usr/local/opt/nvm)

if either path exists than it loads the node version using load_nvm.

When I initially started working with Node I did use nvm but later on moved on to using n and didn't realise that .nvm had not been cleaned up under the home directory so it was pointing to an older version of node(0.12.7) - which caused above mentioned error.

If you are still using nvm to manage node versions please ensure that you update to node version that supports ES6 features(const in this case).

If you are not using nvm than ensure that the nvm is not available on above mentioned paths. Husky ends up using the current version in that case (i.e. 6.10.0 in your case)

Alternatively you can try the solution that mmoutenot mentioned on husky issue




回答2:


I'm guessing you need to pass a glob of what files to lint for stylelint also:

"scripts": {
  "precommit": "lint-staged",

},
"lint-staged": {
  "*.scss": ["stylelint \"**/*.scss\" --syntax scss"
  ]
},


来源:https://stackoverflow.com/questions/43932038/husky-giving-error-syntaxerror-use-of-const-in-strict-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!