0

我正在尝试从我的 Node.js 项目的根目录运行npm install,因为我想安装位于我的package.json中的所有项目依赖项。但是,当我在命令行中运行npm install时,它会显示据报道已安装的依赖项列表,但是当我转到该目录并搜索子目录node_modules/时,它不存在。因此,这些依赖项并没有真正安装,正如我尝试运行npm start时所证明的那样,它会开始抛出有关模块丢失的错误。我究竟做错了什么?

这是我的package.json的副本(出于隐私考虑,删除了细节):

{
  "name": "xxxxxxxxx",
  "version": "x.x.x",
  "description": "xxxxxxxxx",
  "main": "xxxxxxxxx.js",
  "scripts": {
    "start": "node xxxxxxxxx.js",
    "lint": "jshint **/**/*.js --reporter=node_modules/jshint-stylish/stylish.js",
    "test": "mocha test/"
  },
  "pre-commit": [
    "lint",
    "test"
  ],
  "repository": {
    "type": "git",
    "url": "https://xxxxxxxxx.git"
  },
  "author": "xxxxxxxxx",
  "license": "xxxxxxxxx",
  "dependencies": {
    "bluebird": "^2.9.13",
    "boom": "^2.6.1",
    "good": "^5.1.2",
    "good-console": "^4.1.0",
    "hapi": "^8.2.0",
    "hapi-swagger": "^0.6.4",
    "joi": "^5.1.0",
    "lodash": "^3.3.1",
    "lodash-deep": "^1.5.3",
    "lout": "^6.2.0",
    "request": "^2.55.0"
  },
  "devDependencies": {
    "chai": "^2.0.0",
    "jshint-stylish": "^1.0.2",
    "mocha": "^2.1.0",
    "pre-commit": "^1.0.7"
  }
}

如前所述,此处列出的所有依赖项都以绿色文本显示在命令提示符中,向我表明它们已下载。但是,它们没有被下载,因为node_modules/甚至不存在。命令提示符如下所示:

C:\development>npm install
npm http GET https://registry.npmjs.org/boom
npm http GET https://registry.npmjs.org/good-console
npm http GET https://registry.npmjs.org/bluebird
npm http GET https://registry.npmjs.org/good
npm http GET https://registry.npmjs.org/joi
npm http GET https://registry.npmjs.org/hapi-swagger
npm http GET https://registry.npmjs.org/lout
npm http GET https://registry.npmjs.org/hapi
npm http GET https://registry.npmjs.org/lodash-deep
npm http GET https://registry.npmjs.org/chai
npm http GET https://registry.npmjs.org/lodash
npm http GET https://registry.npmjs.org/jshint-stylish
npm http GET https://registry.npmjs.org/mocha
npm http GET https://registry.npmjs.org/pre-commit
npm http GET https://registry.npmjs.org/request
npm http 304 https://registry.npmjs.org/good-console
npm http 304 https://registry.npmjs.org/boom
npm http 304 https://registry.npmjs.org/bluebird
npm http 304 https://registry.npmjs.org/good
npm http 304 https://registry.npmjs.org/hapi-swagger
npm http 304 https://registry.npmjs.org/hapi
npm http 304 https://registry.npmjs.org/lodash-deep
npm http 304 https://registry.npmjs.org/chai
npm http 304 https://registry.npmjs.org/joi
npm http 304 https://registry.npmjs.org/lodash
npm http 304 https://registry.npmjs.org/lout
npm http 304 https://registry.npmjs.org/jshint-stylish
npm http 304 https://registry.npmjs.org/mocha
npm http 304 https://registry.npmjs.org/pre-commit
npm http 304 https://registry.npmjs.org/request

C:\development>ll
05/13/2015  12:32 PM    <DIR>          .
05/13/2015  12:32 PM    <DIR>          ..
05/12/2015  05:24 PM                93 .gitignore
05/12/2015  05:24 PM                15 .jshintignore
05/12/2015  05:24 PM             3,957 .jshintrc
05/12/2015  05:24 PM                36 .npmrc
05/12/2015  05:24 PM    <DIR>          config
05/12/2015  05:24 PM             1,363 xxxxx.json
05/12/2015  05:24 PM    <DIR>          lib
05/12/2015  05:24 PM             1,201 package.json
05/12/2015  05:24 PM             1,113 readme.md
05/12/2015  05:24 PM             2,046 xxxxx.js

C:\development>npm start

module.js:340
    throw err;
          ^
Error: Cannot find module 'hapi'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (C:\path)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

请注意上面没有子目录node_modules/存在,以及当它在命令提示符中列出已下载时找不到模块“hapi”的事实。

到目前为止,我尝试过的是:

  • 在运行npm install之前手动创建node_modules/目录
  • 检查目录权限
  • 其他stackoverflow文章

但我仍然没有成功。请注意,与我的问题最接近的 stackoverflow 是npm install doesn't install any dependencies,但不同之处在于该帖子上的“答案”并没有解决我的问题(这是删除目录node_modules/,但我的没有'甚至不存在)。

我能够在我拥有的其他 Node.js 项目中运行npm install,并成功从他们的package.json安装依赖项。我只是无法让这个例子工作。

4

1 回答 1

0

注意 npmrc 中的根文件夹 .. 可能是您所有的依赖项都下载到根文件夹.. Npm 有时会这样做.. 我也遇到过这个问题... 大多数情况下发生在文件夹中没有 package.json 文件时.. 2nd将来自您的日志,我看不到模块下载的信息.. 可能无法访问 npm 或其他东西.. 将 npm 注册表 comfig 设置为 http,然后再试一次。更改您的路径 D: 或其他东西.. 如果发生这种情况,会引发 npm 的错误

于 2015-05-14T20:01:11.973 回答