This question already has answers here:
Error: Do not use <img>. Use Image from 'next/image' instead. - Next.js using html tag <img /> ( with styled components )
(3 answers)
Closed 11 months ago.
I recently started making websites with Next.js, and I have been using a mix of Image and img for various use cases.
I know that Image is built-in to Next.js and is the better choice, but there are scenarios where I do not know the size or the ratio of the images I am loading, and thus the img seems to better suit.
During my recent project, this is the first time my npm run build command is failing with:
1:7 Error: Do not use <img>. Use Image from 'next/image' instead. See https://nextjs.org/docs/messages/no-img-element.
My other Next.js projects do not fail this way, and use the same next.config.js. Does anyone know why this is happening?
This is due to the new Next.js release, which has integrated ESLint with its own set of rules to enforce that <Image> should always be used. Your other projects don't fail because probably you are not using Next.js v11, or they may have their own ESLint config, where you haven't extended next. Ref.: nextjs.org/docs/basic-features/eslint
You can ignore linting during build by this:
// next.config.js
module.exports = {
eslint: { ignoreDuringBuilds: true },
// your other settings here ...
}
If you want to specifically disable the rule for a file or just a line do this: (can be done in two clicks if using ESLint extension in VSCode)
{/* eslint-disable-next-line #next/next/no-img-element */}
<img ... //>
// for whole file, add this to top:
/* eslint-disable #next/next/no-img-element */
// for a section:
/* eslint-disable #next/next/no-img-element */
// your code here...
/* eslint-enable #next/next/no-img-element */
You can also disable the rule using .eslintrc:
{
"extends": "next",
"rules": {
"#next/next/no-img-element": "off",
}
}
You can also ignore (all rules for) a complete file using eslintignore or ignorePatterns. Please refer: eslint.org/docs/user-guide/configuring/ignoring-code
How do I ignore this error. I have used the id attribute for some tags in order to reference them by id in javascript, but it shows this error:
CSS id selector '...' not found.
Please tell me how to ignore or disable this error.
maybe your css is not formatted properly.
try:
<style>
#phone {
your css here;
}
</style>
You need to make sure you mark the code with # to show that it's an id and not a class or something else.
Update: First section of answer no longer valid as of the lastest updates since original post which removed the css.validation option. see
If you are using the HTML CSS Support extension by ecmel, you can go to .vscode/settings.json and add
"css.validation": {
"id": false,
"class": false
}
This will turn off css validation for class name and id.
More information on this at the Visual Studio MarketPlace for this extension under Selector Validation here or the Github repository readme Selector Validation Section
Note:
1) Also don't forget to add a comma after the setting that comes before (as JSON format is comma separated).
Example:
{
"java.codeGeneration.generateComments": true,
"css.styleSheets": [
// (1)
"https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css",
"src/main/resources/static/css/styles.css"
],
"css.validation": {
"id": false,
"class": true
}
}
Sometimes you need to restart / close and reopen vscode after saving changed to the file for them take effect.
There is a way to configure styleSheets. This next bit is taken from their documentation see Additional Styles Section:
Additional Style Sheets
If it is not possible to specify local or remote styles in HTML or via
template inheritance, they can be specified in VS Code settings per
workspace folder in .vscode/settings.json and will suggest for all
HTML files within that workspace folder:
.vscode/settings.json
"css.styleSheets": [
// (1)
"https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css",
// (2)
"/site.css",
// (3)
"site.css",
// (4)
"./site.css"
]
gulp.task('pug-general', function(){
gulp.src('bundles/pug/*.pug')
.pipe(pug({
outputStyle: 'compact'
}))
.pipe(gulp.dest('page/'));
});
I've been looking for a way to change output style for pug templates because, when it compiles it outputs a single line html file. Although I've written outputStyle: 'compact' I was just trying to see if it worked.
Pug has a "pretty" option. However it's set to false by default so if you would like more white space collapsed, you might like to try this gulp package https://github.com/jonschlinkert/gulp-htmlmin
Is it possible to turn off the eslint rule for the whole file? Something such as:
// eslint-disable-file no-use-before-define
(Analogous to eslint-disable-line.) It happens to me quite often, that in a certain file, I'm breaking a specific rule on many places which is considered OK for that file, but I don't want to disable the rule for the whole project nor do I want to disable other rules for that specific file.
You can turn off/change a particular rule for a file by putting the configurations at the top of the file.
/* eslint no-use-before-define: 0 */ // --> OFF
or
/* eslint no-use-before-define: 2 */ // --> ON
More info
To disable a specific rule for the file:
/* eslint-disable no-use-before-define */
Note there is a bug in eslint where single line comment will not work -
// eslint-disable max-classes-per-file
// This fails!
Check this post
Let's have a breakdown of the scenarios, like you always do, dear awesome developer!
Here are two questions to ask yourself, first.
Question One: How many "rules" do you want to ignore?
All Rules
One or more Specific Rules (e.g. quotes or semi)
Question Two: How many "lines/files" do you want to ignore?
One or more Lines
All lines in one or more Files
Everywhere
Now, based on the your answers, there are 2 × 3 = 6 cases.
1) Disabling "All rules"
Case 1.1: You want to disable "All Rules" for "One or more Lines"
Two ways you can do this:
Put /* eslint-disable-line */ at the end of the line(s),
or /* eslint-disable-next-line */ right before the line.
Case 1.2: You want to disable "All Rules" for "One File"
Put the comment of /* eslint-disable */ at the top of the file.
Case 1.3: You want to disable "All rules" for "Some Files"
There are 3 ways you can do this:
You can go with 1.2 and add /* eslint-disable */ on top of the files, one by one.
You can put the file name(s) in .eslintignore. This works well, especially if you have a path that you want to be ignored. (e.g. apidoc/**)
Alternatively, if you don't want to have a separate .eslintignore file, you can add
"eslintIgnore": ["file1.js", "file2.js"] in package.json as
instructed here.
2) Disabling "Some Rules"
Case 2.1: You want to disable "Some Rules" for "One or more Lines"
Two ways you can do this:
You can put /* eslint-disable-line quotes */ (replace quotes with your rules) at the end of the line(s),
or /* eslint-disable-next-line no-alert, quotes, semi */ before the line.
Pro Tip: if you have multiple lines that you want to ignore the same rule(s) for, you can disable and enable the rules like this:
// Assume these lines are long engouh that max-len is gonna trigger
/* eslint-disable max-len */
console.log("I am a loooooooooo...ooooong line 1, but eslint doesn't bug!");
console.log("I am a loooooooooo...ooooong line 2, but eslint doesn't bug!");
console.log("I am a loooooooooo...ooooong line 3, but eslint doesn't bug!");
/* eslint-enable max-len */
console.log("I am a loooooooooo...ooooong line 4, AND eslint's GONNA CRY!"); // <--- eslint is gonna cry over this one only!
Case 2.2: You want to disable "Some Rules" for "One File"
Put the /* eslint-disable no-use-before-define */ comment at the top of the file.
More examples here.
Case 2.3: You want to disable "Some Rules" for "Some files"
This is less straightforward. You should create an "overrides" section in your .eslintrc and specify which rules should be disabled/modified for which globs/files. An example can be found in this answer.
You can tell ESLint to ignore specific files and directories by creating an .eslintignore file in your project’s root directory:
.eslintignore
build/*.js
config/*.js
bower_components/foo/*.js
The ignore patterns behave according to the .gitignore specification.
(Don't forget to restart your editor.)
You can also disable/enable a rule like this:
/* eslint-disable no-use-before-define */
... code that violates rule ...
/* eslint-enable no-use-before-define */
Similar to eslint-disable-line as mentioned in the question. It might be a better method if you don't want to have to restore a complicated rule configuration when re-enabling it.
It's better to add "overrides" in your .eslintrc.js config file.
For example if you wont to disable camelcase rule for all js files ending on Actions add this code after rules scope in .eslintrc.js.
"rules": {
...........
},
"overrides": [
{
"files": ["*Actions.js"],
"rules": {
"camelcase": "off"
}
}
]
To temporarily disable rule warnings in your file, use block comments in the following format:
/* eslint-disable */
This will disable ESLint until the
/* eslint-enable */
comment is reached.
You can read more about this topic here.
To disable specific rules for file(s) inside folder(s), you need to use the "overrides" key of your .eslintrc config file.
For example, if you want to remove the following rules:
no-use-before-define
max-lines-per-function
For all files inside the following main directory:
/spec
You can add this to your .eslintrc file...
"overrides": [
{
"files": ["spec/**/*.js"],
"rules": {
"no-use-before-define": ["off"],
"max-lines-per-function": ["off"]
}
}
]
Note that I used ** inside the spec/**/*.js glob, which means I am looking recursively for all subfolders inside the folder called spec and selecting all files that ends with js in order to remove the desired rules from them.
Accepted answer didn't work for me (maybe a different version of eslint...? I'm using eslint v3.19.0), but did find a solution with the following...
Place the following on the top of your file
/* eslint-disable no-use-before-define */
This will disable the rule for the entire file
/* eslint-disable */
//suppress all warnings between comments
alert('foo');
/* eslint-enable */
This will disable all eslint rules within the block.
Simple and effective.
Eslint 6.7.0 brought "ignorePatterns" to write it in .eslintrc.json like this example:
{
"ignorePatterns": ["fileToBeIgnored.js"],
"rules": {
//...
}
}
See docs
If you want to disable ESLint for one rule, you can add this to the top of the file:
/* eslint-disable NAME_OF_THE_RULE */
If you want to disable ESLint or TypeScript checks inside a file, you can add these lines at the top of the file. The first one will disable TypeScript checks, and the second one ESLint checks.
// #ts-nocheck
/* eslint-disable */
you can configure eslint overrides property to turn off specific rules on files which matches glob pattern like below.
Here, I want to turn off the no-duplicate-string rule for tests all files.
overrides: [
{
files: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
rules: {
'sonarjs/no-duplicate-string': 'off'
}
}
]
You can just put this for example at the top of the file:
/* eslint-disable no-console */
As of today, the answer does not work for me, but putting this at the top of the file does work:
/* eslint-disable #typescript-eslint/no-unused-vars */
It is important to know that at least in my case, the type of comment makes a difference. The previous comment works for me, but the following won't work:
// eslint-disable #typescript-eslint/no-unused-vars
Just add this to the top of your file.
This disables all eslint warnings respectively.
/* eslint-disable */
You can turn off specific rule for a file by using /*eslint [<rule: "off"], >]*/
/* eslint no-console: "off", no-mixed-operators: "off" */
Version: eslint#4.3.0
Simply create an empty file .eslintignore in your project root the type the path to the file you want it to be ignore.
Source: https://eslint.org/docs/2.13.1/user-guide/configuring#:~:text=To%20disable%20rule%20warnings%20in,*%2F%20alert('foo')%3B
Line Ignoring Files and Directories
To temporarily disable rule warnings in your file, use block comments in the following format:
/* eslint-disable */
alert('foo');
/* eslint-enable */
You can also disable or enable warnings for specific rules:
/* eslint-disable no-alert, no-console */
alert('foo');
console.log('bar');
/* eslint-enable no-alert, no-console /
To disable rule warnings in an entire file, put a / eslint-disable */ block comment at the top of the file:
/* eslint-disable */
alert('foo');
You can also disable or enable specific rules for an entire file:
/* eslint-disable no-alert */
alert('foo');
To disable all rules on a specific line, use a line comment in one of the following formats:
Following are some examples to disable ESLint for a page
alert('foo'); // eslint-disable-line
// eslint-disable-next-line
alert('foo');
To disable a specific rule on a specific line:
alert('foo'); // eslint-disable-line no-alert
// eslint-disable-next-line no-alert
alert('foo');
To disable multiple rules on a specific line:
alert('foo'); // eslint-disable-line no-alert, quotes, semi
// eslint-disable-next-line no-alert, quotes, semi
alert('foo');
Do following steps to disable ESLint from your project
open package.config file in your project.
remove all dependencies related to ESLint.
remove eslint.js/eslintconfig files from your project
run command npm install
now run your project
I have multiple sass files (with one style.scss containing all includes) and a couple of css libraries, which I want to combine in one final style.min.css.
I've configured 2 tasks with elixir:
Compiles my sass file: 'style.scss' (containing all includes) to css: 'public/css/style.css'
Combines the compiled css: 'public/css/style.css' with other
stylesheets (libraries) into the final: 'public/css/style.min.css'
Here's my gulpfile:
elixir(function(mix) {
mix
.sass('style.scss', 'public/css/style.css')
.styles([
'path-to-lib/some-random-lib/lib.css',
'path-to-lib/another-random-lib/lib.css',
'/public/css/style.css'
], 'public/css/style.min.css')
});
Problem: The sass compile task creates correct sourcemap, pointing to the right lines in .scss, but the second task which combines the styles - creates a sourcemap that is pointing to the lines in 'public/css/style.css', instead of the ones in the .scss files :(
Does anybody know a way how I can force the final sourcemap to point to the lines in the sass files?
Finally, after 3 months, I found a solution!
Instead of calling both - mix.sass to compile SASS and then mix.styles to combine css stylesheets, I just wrapped up everything in the mix.sass
elixir(function(mix) {
mix.sass([
'path-to-lib/some-random-lib/lib.css',
'path-to-lib/another-random-lib/lib.css',
'/public/css/style.css'
'style.scss'
], 'public/css/style.css')
});
This trick fixes the source map issue.