Visual studio code 在VS代码中,I';我得到这个错误,';未能加载模块。试图从package.json';

Visual studio code 在VS代码中,I';我得到这个错误,';未能加载模块。试图从package.json';,visual-studio-code,package.json,prettier,Visual Studio Code,Package.json,Prettier,当我使用VS代码并打开项目时,我在右下角收到以下通知: 加载模块失败。如果您在package.json中引用了prettier或插件,请确保您已运行npm install尝试从package.json加载prettier。 Source:Prettier代码格式(扩展) 运行npm安装无法解决此问题。有人知道为什么会这样,或者我能做些什么来修复它吗?这是一个适合我的解决方案 1.如果您从未在全球范围内安装过Prettier,请通过npm在全球范围内安装它 npm i prettier-g 2.

当我使用VS代码并打开项目时,我在右下角收到以下通知:

加载模块失败。如果您在package.json中引用了prettier或插件,请确保您已运行
npm install
尝试从package.json加载prettier。

Source:Prettier代码格式(扩展)


运行npm安装无法解决此问题。有人知道为什么会这样,或者我能做些什么来修复它吗?

这是一个适合我的解决方案

1.如果您从未在全球范围内安装过Prettier,请通过npm在全球范围内安装它

npm i prettier-g
2.在VS代码设置中搜索并使用
更漂亮的路径
扩展设置

//您可以导航到VS CodeSettings>Extensions>Prettier,查看所有Prettier扩展设置

3.
Prettier路径更新到全球安装的Prettier

例如

/usr/local/lib/node\u模块/prettier
(Mac OS)


\AppData\Roaming\npm\node\u modules\prettier
(Windows)

我刚刚遇到这个问题,发现package.json文件中有语法错误。一行后面有一个逗号,这似乎就是我的根本原因

我注意到了这一点,因为我在尝试运行一些角度测试时看到了以下输出:

C:\... [feature/migrate-away-from-angular-http +2 ~6 -0 | +0 ~5 -0 !]> ng test
10% building 3/3 modules 0 active20 11 2019 21:11:18.638:WARN [karma]: No captured browser, open http://localhost:9876/
20 11 2019 21:11:19.575:INFO [karma-server]: Karma v4.1.0 server started at http://0.0.0.0:9876/
20 11 2019 21:11:19.576:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
20 11 2019 21:11:19.594:INFO [launcher]: Starting browser Chrome

ERROR in ./src/app/app.component.spec.ts
Module not found: SyntaxError: C:\...\package.json (directory description file): SyntaxError: C:\...\package.json (directory description file): SyntaxError: Unexpected token } in JSON at position 167
 @ ./src/app/app.component.spec.ts 7:31-75
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts
ERROR in ./src/app/app.component.ts

通过全局执行
npm安装
修复了此问题

我在清理
节点时遇到了这个问题。我在全球安装了
eslint
和prettier。当我删除
node\u模块时
显示了这个错误

更新 现在我可以使用
prettier vscode
4.1.1和
prettier
2.0.4,试试看。我可以使用捆绑或本地安装的prettier版本

:

  • [4.0.0]将prettier更新为2.0
  • [4.1.0]添加了带有NodeModules的配置选项,以允许处理node_modules文件夹中的文件[默认值:false]
  • [4.1.0]支持从node_模块加载Prettier,即使它在package.json中不显示为直接依赖项

原职 我在检查
node\u modules
下的外部包的源代码时遇到了这个问题

解决方法是删除此软件包的
package.json
中的
prettier
项-无需安装本地/全局
prettier

保持
package.json
有效-无尾随逗号,不要只注释掉行。它起作用的原因是:

扩展将向下搜索树,直到我们提交一个package.json。如果package.json包含prettier,则扩展将使用它,否则它将退回到使用prettier的捆绑版本

我的猜测是,扩展希望使用包中的
prettier
,即使它是
devdependences


node\u modules
中的包中删除
devDependency
应该没有什么害处。这还可以使用与
prettier vscode
捆绑的
prettier
版本(无需安装)。

在设置prettier时,重要的是根据项目配置它。并不是每个项目都使用相同的代码样式,因此尊重您当前正在处理的任何项目的样式都很重要

演示repo
bahmutov/prettier配置示例有两个子文件夹,每个子文件夹都有其独特的代码样式,由prettier强制执行。事实上,你的每一次回购都有自己的风格;我使用子文件夹是为了使示例保持简单


npm安装--save dev--save exact prettier

尝试了这里提供的所有解决方案,没有帮助。
更新Visual Studio代码修复了此问题。

@tunji oyeniran所说的对我有用。 我在全球范围内安装了Pretter,正如他所说:

npm i prettier -g
然后我在我的电脑里找到了它

我使用pwd命令看到路径是正确的,然后我转到vscode,并查找灭绝路径:

我重新启动了vscode,看到格式化代码正在工作

此si my settings.json:

{
    "editor.minimap.enabled": false,
    "editor.fontSize": 12,
    "editor.formatOnSave": true,
    "editor.tabSize": 2,
    "liveServer.settings.donotShowInfoMsg": true,
    "editor.wordWrap": "on",
    "workbench.iconTheme": "material-icon-theme",
    "emmet.triggerExpansionOnTab": true,
    "emmet.showSuggestionsAsSnippets": true,
    "editor.snippetSuggestions": "top",
    "[javascript]": {
        // "editor.defaultFormatter": "esbenp.prettier-vscode",
        "editor.formatOnSave": true,
        "editor.formatOnPaste": true
    },
    "workbench.colorTheme": "Monokai",
    "window.zoomLevel": 0,
    "editor.columnSelection": false,
    "explorer.compactFolders": false,
    "typescript.updateImportsOnFileMove.enabled": "always",
    "javascript.updateImportsOnFileMove.enabled": "always",
    "liveServer.settings.donotVerifyTags": true,
    // "javascript.format.enable": true,
    "prettier.prettierPath": "/usr/local/lib/node_modules/prettier",
}
prettier路径是在此settings.json中自动添加的。

替代解决方案(无需全局安装prettier) 如果您有一个本地安装了prettier的项目(例如,通过
npm install-D prettier
),您不想全局添加它

当VSCode询问您是否要允许本地安装带有已安装扩展的prettier run时,您可能只是单击了“不允许”

在这种情况下,解决方案是:

  • 打开VSCode并键入
    cmd+shift+p
    以打开命令选项板
  • 类型
    Prettier:重置模块执行状态
  • 然后只需再次保存文件,VSCode就会询问您是否允许运行更漂亮的程序。您要在此处选择
    允许无处不在

  • 也有同样的问题。你是不是碰巧在代理后面?我的机器上好像有什么路径断了,或者什么东西丢失了。我们有一个package.json,其中包含一个运行prettier的脚本和一个linter。从package.json文件运行脚本并不适用于prettier,但是如果我自己格式化它(mac上的option+shift+f),它就可以工作。手动格式化每个文件是多么奇怪和烦人。这似乎是VS代码最近的一个“补充”。尽管过去也使用了prettier,但我们没有注意到这个问题。我不知道从哪个版本的VS代码开始,这就成了一个问题。从几天前开始就有同样的问题。没有代理或防火墙-在最新的VS代码更新中有更漂亮的东西。这是相关的github问题。他们正在寻找更多的信息
    {
        "editor.minimap.enabled": false,
        "editor.fontSize": 12,
        "editor.formatOnSave": true,
        "editor.tabSize": 2,
        "liveServer.settings.donotShowInfoMsg": true,
        "editor.wordWrap": "on",
        "workbench.iconTheme": "material-icon-theme",
        "emmet.triggerExpansionOnTab": true,
        "emmet.showSuggestionsAsSnippets": true,
        "editor.snippetSuggestions": "top",
        "[javascript]": {
            // "editor.defaultFormatter": "esbenp.prettier-vscode",
            "editor.formatOnSave": true,
            "editor.formatOnPaste": true
        },
        "workbench.colorTheme": "Monokai",
        "window.zoomLevel": 0,
        "editor.columnSelection": false,
        "explorer.compactFolders": false,
        "typescript.updateImportsOnFileMove.enabled": "always",
        "javascript.updateImportsOnFileMove.enabled": "always",
        "liveServer.settings.donotVerifyTags": true,
        // "javascript.format.enable": true,
        "prettier.prettierPath": "/usr/local/lib/node_modules/prettier",
    }