'How i can fix js import auto-hide problem in vs code

Firstly, I imported something from another module in js:

import cloneDeep  from './node_modules/lodash-es/cloneDeep.js'

But when I am not using this cloneDeep in my code and save my script file, then VS Code automatically hides that line.

Before Save

Before Save

After Save

After Save



Solution 1:[1]

vcode settings.json

{
  "editor.codeActionsOnSave": {
    "source.organizeImports": false
  },
}

Solution 2:[2]

try instead of import cloneDeep from './node_modules/lodash-es/cloneDeep.js

this: import cloneDeep from 'lodash-es/cloneDeep.js

you don't need to specify node_modules absolute path and if it's es6 import you need to add an entry in package.json:

"type": "module"

are you running it in NodeJs?

edit: if the code editor deleting it then it must be from a linter/extension causing it. Probably because you set the variable as global. either check the extension config or disable it or wrap the variable declaration in a function

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lisonge
Solution 2