简体   繁体   English

将ESLint与sublime集成时出错

[英]Error integrating ESLint with sublime

I am new to Node.js, npm and sublime environment and I am finding it difficult to integrate ESlint with sublime editor. 我是Node.js,npm和sublime环境的新手,我发现很难将ESlint与sublime编辑器集成。

The steps which I followed is as follows: 我遵循的步骤如下:

  1. Installed ESLint with command: npm install -g eslint 使用命令安装ESLint: npm install -g eslint
  2. Installed ESLint with Sublime package control 使用Sublime包控件安装ESLint

Now getting following error in Sublime editor: 现在在Sublime编辑器中收到以下错误:

module.js:327
    throw err;
    ^

Error: Cannot find module 'eslint'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Users\MyUser\AppData\Roaming\Sublime Text 3\Packages\ESLint\linter.js:12:17)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
[Finished in 0.2s with exit code 1]

Go to "Preferences / Package Settings / ESLint / Settings - User" in the menu bar, and enter the following (file paths may vary): 转到菜单栏中的“首选项/包设置/ ESLint /设置 - 用户”,然后输入以下内容(文件路径可能不同):

{
  "node_path": "/usr/local/bin",
  "node_modules_path": "/usr/local/lib/node_modules"
}

Here's the GitHub page: https://github.com/polygonplanet/sublime-text-eslint . 这是GitHub页面: https//github.com/polygonplanet/sublime-text-eslint Scroll down to the "Configuring ESLint" section, it's all documented there. 向下滚动到“配置ESLint”部分,它都记录在那里。

If Jeremy's answer still doesn't help, make sure your npm folder is exposed in your environment variable. 如果Jeremy的答案仍无效,请确保您的环境变量中公开了您的npm文件夹。

On Windows 7, PATH would include something like: 在Windows 7上, PATH将包含以下内容:

C:\Users\<YOUR_USERNAME>\AppData\Roaming\npm;

(Location: Control Panel\\All Control Panel Items\\System\\Advanced system settings\\Advanced\\Environment Variables\\User variables...\\PATH ) (位置: 控制面板\\所有控制面板项目\\系统\\高级系统设置\\高级\\环境变量\\用户变量... \\ PATH

I got ESlint working for me on Sublime Text 3 and Win 7 by editing the file "Preferences -> Package Settings -> ESLint -> Settings - Default" file, inserting an additional 3rd value for the 'Config File' as you can see in the screen-shot below: 我通过编辑文件“首选项 - >包设置 - > ESLint - >设置 - 默认”文件,在Sublime Text 3和Win 7上为我工作,为'配置文件'插入额外的第3个值,如您所见屏幕截图如下:

Steps to follow for setting up ESlint on Sublime Text 3 with Win 7+ 使用Win 7+在Sublime Text 3上设置ESlint的步骤

The file code would look like this, if you don't want to see the screen-shot: 如果您不想看到屏幕截图,文件代码将如下所示:

{ "node_path": "C:/Program Files/nodejs",
"node_modules_path": "C:/Users/<UserName>/AppData/Roaming/npm/node_modules",
"config_file": "C:/Users/<UserName>/AppData/Roaming/npm/node_modules/eslint/node_modules/debug/.eslintrc"}

You can further see which file formats are supported for configuration here: https://eslint.org/docs/user-guide/configuring 您可以在此处进一步查看支持配置的文件格式: https//eslint.org/docs/user-guide/configuring

This error is what npm would throw if it doesn't find the eslint module. 如果没有找到eslint模块,这个错误就是npm会抛出的错误。

I had the same error and I realized that it was for incorrect node_modules_path, it "$HOME/.npm-global" and I just change that to "$HOME/.npm-global/bin" (+ "/bin" ) and it worked for me. 我有同样的错误,我意识到这是错误的node_modules_path,它是"$HOME/.npm-global" ,我只是将其更改为"$HOME/.npm-global/bin" (+ "/bin" )和它对我有用。

i encountered that issue installing: npm install -g eslint solved that problem in next way: 我遇到了安装问题:npm install -g eslint以下一个方式解决了这个问题:

  1. get known Eslint version from terminal; 从终端获知Eslint版本;

     eslint -v 
  2. added a line to dependencies in package.json with that known eslint version 使用已知的eslint版本在package.json中为依赖项添加了一行

     "dependencies": { ... "eslint": "^4.19.1" } 
  3. final step : in terminal i updated all written in this package.json 最后一步:在终端我更新了这个package.json中的所有内容

     npm install 

Not sure, if this issue has been solved in the meanwhile, but there has been a discussion some years ago, about installing eslint globally or locally per project. 不确定,如果这个问题在此期间已经解决了,但几年前就已经进行过讨论 ,关于在全球或本地每个项目安装eslint。 As far as I understood the discussion, it is recommended to install eslint and eslint plugins locally to avoid the issue. 据我所知,讨论时,建议在本地安装eslint和eslint插件以避免此问题。

In order to use Eslint with Sublime, I installed eslint in my project with: 为了将Eslint与Sublime一起使用,我在我的项目中安装了eslint:

npm install eslint --save-dev

Furthermore I installed SublimeLinter and SublimeLinter-eslint to integrate the Eslint into Sublime and let it run whenever a file is saved. 此外,我安装了SublimeLinterSublimeLinter-eslint ,将Eslint集成到Sublime中,并在保存文件时让它运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM