【问题标题】:Is it necessary to have a package.json file before I can install npm modules?在我安装 npm 模块之前是否需要有一个 package.json 文件?
【发布时间】:2016-09-23 21:58:00
【问题描述】:

我基本上有一个完全空的文件夹,里面只有一个文件test.js。我想安装几个模块,但是当我尝试执行npm install express 之类的操作时,我收到一条错误消息,提示我没有package.json 文件。

$ npm install express

> node-icu-charset-detector@0.1.3 install /Users/me/node_modules/node-icu-charset-detector
> node-gyp rebuild

  CXX(target) Release/obj.target/node-icu-charset-detector/node-icu-charset-detector.o
../node-icu-charset-detector.cpp:7:10: fatal error:
      'unicode/ucsdet.h' file not found
#include <unicode/ucsdet.h>
         ^
1 error generated.
make: *** [Release/obj.target/node-icu-charset-detector/node-icu-charset-detector.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:270:23)
gyp ERR! stack     at emitTwo (events.js:87:13)
gyp ERR! stack     at ChildProcess.emit (events.js:172:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)
gyp ERR! System Darwin 15.0.0
gyp ERR! command "/usr/local/Cellar/node/5.1.0/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/me/node_modules/node-icu-charset-detector
gyp ERR! node -v v5.1.0
gyp ERR! node-gyp -v v3.0.3
gyp ERR! not ok
npm WARN install:node-icu-charset-detector@0.1.3 node-icu-charset-detector@0.1.3 install: `node-gyp rebuild`
npm WARN install:node-icu-charset-detector@0.1.3 Exit status 1
/Users/me
├── express@4.13.3
├── UNMET PEER DEPENDENCY react@>=0.14.0 <0.15.0
└── UNMET PEER DEPENDENCY react-dom@>=0.14.0 <0.15.0

npm WARN ENOENT ENOENT: no such file or directory, open '/Users/me/package.json'
npm WARN EPEERINVALID mongoskin@1.4.13 requires a peer of mongodb@~1.4 but none was installed.
npm WARN EPEERINVALID react-router-component@0.28.0 requires a peer of react@>=0.14.0 <0.15.0 but none was installed.
npm WARN EPEERINVALID react-router-component@0.28.0 requires a peer of react-dom@>=0.14.0 <0.15.0 but none was installed.
npm WARN EPEERINVALID react-tap-event-plugin@0.2.1 requires a peer of react@^0.14.0 but none was installed.
npm WARN EPACKAGEJSON me No description
npm WARN EPACKAGEJSON me No repository field.
npm WARN EPACKAGEJSON me No README data
npm WARN EPACKAGEJSON me No license field.

【问题讨论】:

  • 你能包含错误信息吗?
  • 对不起,我现在包含它。再次执行此操作后,似乎还有其他错误,并且说没有package.json 文件的消息只是一个警告。您知道可能导致此错误的原因吗?
  • 您要求安装在此目录中的模块 (/Users/me) 与全局模块 (/usr/local/lib) 之间似乎出现了问题。创建一个本地 package.json (npm init) 可以简化诊断。
  • 哦,我明白了。在我意识到我不应该在全球范围内安装所有东西之前,我实际上用-g 做了很多安装,所以这可能是我出错的原因。我将尝试删除我的全局 node_modules 文件夹并重新安装节点。感谢您抽出宝贵的时间。编辑:这样做之后,它安装得很好。我认为你是对的。

标签: node.js module npm package.json


【解决方案1】:

npm install基本上有三种主要形式:

  1. npm install:阅读package.json,并将其中的所有内容安装到此目录中。
  2. npm install express:将 express 安装到这个目录中。
  3. npm install --save express:将express安装到这个目录,并保存到package.json中。

第二种形式不需要package.json 在这里存在:

$ npm install express
express@4.13.3 node_modules/express
├── escape-html@1.0.2
├── array-flatten@1.1.1
...

如果您收到错误消息,则说明其他问题。

【讨论】:

    【解决方案2】:

    不,不需要 package.json。

    1. 您可以在其中包含多个不同的模块名称 你想在 npm install 时安装。

    2. 您可以更好地控制模块的版本

    3. 您可以分离模块,如依赖项和开发依赖项

    “npm install express”将为您安装 express 模块。

    【讨论】:

    • 当我执行npm install express 时,我收到一条错误消息,提示我需要一个package.json 文件。
    • 如果您有 express 文件夹,请转到 node_modules 文件夹,将其删除并点击 npm install express
    【解决方案3】:

    注意:如果您使用的是作用域包Scoped Packages,恐怕是。

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 2020-09-21
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多