You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

npm无法创建node_modules目录问题求助

npm安装zod后未生成node_modules,lock文件出现异常路径e_modules/zod的解决办法

问题详情

复现步骤

  • 操作系统:Windows 11
  • 进入空目录执行初始化命令:npm init -y
  • 执行安装命令:npm install zod

异常现象

  • 未生成node_modules目录,无法正常导入zod模块
  • 网络连接正常,但package-lock.json中出现异常路径e_modules/zod,该包被标记为extraneous(无关包)

环境信息

  • Node.js版本:v18.18.0
  • npm版本:v9.8.1

相关配置文件

package.json:

{
  "name": "hello",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "zod": "*"
  }
}

package-lock.json(异常状态):

{
  "name": "hello",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "hello",
      "version": "1.0.0",
      "license": "ISC",
      "dependencies": {
        "zod": "*"
      }
    },
    "e_modules/zod": {
      "version": "3.22.4",
      "resolved": "https://registry.npmjs.org/zod/-/zod-3.22.4.tgz",
      "integrity": "sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==",
      "extraneous": true,
      "devDependencies": {
        "@babel/core": "^7.22.5",
        "@babel/preset-env": "^7.22.5",
        "@babel/preset-typescript": "^7.22.5",
        "@rollup/plugin-typescript": "^8.2.0",
        "@swc/core": "^1.3.66",
        "@swc/jest": "^0.2.26",
        "@types/benchmark": "^2.1.0",
        "@types/jest": "^29.2.2",
        "@types/node": "14",
        "@typescript-eslint/eslint-plugin": "^5.15.0",
        "@typescript-eslint/parser": "^5.15.0",
        "babel-jest": "^29.5.0",
        "benchmark": "^2.1.4",
        "dependency-cruiser": "^9.19.0",
        "eslint": "^8.11.0",
        "eslint-config-prettier": "^8.5.0",
        "eslint-plugin-ban": "^1.6.0",
        "eslint-plugin-import": "^2.25.4",
        "eslint-plugin-simple-import-sort": "^7.0.0",
        "eslint-plugin-unused-imports": "^2.0.0",
        "husky": "^7.0.4",
        "jest": "^29.3.1",
        "lint-staged": "^12.3.7",
        "nodemon": "^2.0.15",
        "prettier": "^2.6.0",
        "pretty-quick": "^3.1.3",
        "rollup": "^2.70.1",
        "ts-jest": "^29.1.0",
        "ts-morph": "^14.0.0",
        "ts-node": "^10.9.1",
        "tslib": "^2.3.1",
        "tsx": "^3.8.0",
        "typescript": "~4.5.0",
        "vitest": "^0.32.2"
      },
      "funding": {
        "url": "https://github.com/sponsors/colinhacks"
      }
    }
  }
}

解决办法

1. 清理异常文件与缓存

  • 删除当前目录下的package-lock.json文件
  • 执行命令清除npm缓存:
    npm cache clean --force
    

2. 重新安装依赖

在项目目录下重新执行安装命令:

npm install zod

3. 修复npm配置(若上述步骤无效)

  • 检查npm配置是否存在异常路径:
    npm config list
    
  • 若发现prefix或cache路径异常,重置配置:
    npm config delete prefix
    npm config set prefix "$HOME/AppData/Roaming/npm"
    
  • 之后再次执行npm install zod

4. 升级/降级npm版本

若问题仍存在,尝试切换npm版本:

  • 升级到最新稳定版:
    npm install -g npm@latest
    
  • 或降级到已知稳定版本(如v9.6.7):
    npm install -g npm@9.6.7
    

内容的提问来源于stack exchange,提问作者ertucode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 17:02:05