NPM:找不到“npm link”模块后

时间:2014-07-03 09:55:10

标签: node.js npm

我正在为NodeJS开发两个模块,第一个模块名为aligator,第二个模块aligator-methods。第二个依赖于第一个工作。我正在同时开发这两个模块,我想要全局链接aligator所以我可以像在npm注册表上一样使用它,我只是全局安装它。要做到这一点,NPM文档说我需要使用npm link,但它不起作用。

模块package.json的文件aligator

{
  "name": "aligator",
  "version": "0.0.1",
  "description": "",
  "main": "index.js",
  "private": true,
  "directories": {
    "doc": "docs",
    "example": "examples",
    "test": "spec"
  },
  "scripts": {
    "test": "gulp jasmine"
  },
  "author": "Roc Alayo Arnabat",
  "license": "MIT",
  "devDependencies": {
    "gulp": "^3.6.2",
    "gulp-jasmine": "^0.2.0",
    "gulp-jshint": "^1.6.1",
    "gulp-rename": "^1.2.0",
    "jasmine-node": "^1.14.3"
  },
  "dependencies": {
    "bluebird": "^1.2.4",
    "lodash": "^2.4.1",
    "mathjs": "^0.22.0"
  }
}

模块package.json的文件aligator-methods

{
 "name": "aligator-methods",
 "version": "0.0.1",
 "description": "",
 "main": "index.js",
 "private": true,
 "directories": {
   "doc": "docs",
   "example": "examples",
   "test": "jasmine"
 },
 "scripts": {
   "test": "gulp jasmine"
 },
 "author": "",
 "license": "MIT",
 "devDependencies": {
   "gulp": "^3.6.2",
   "gulp-jasmine": "^0.2.0",
   "gulp-jshint": "^1.6.1",
   "gulp-rename": "^1.2.0",
   "jasmine-node": "^1.14.3"
 },
 "dependencies": {
   "lodash": "^2.4.1",
   "mathjs": "^0.22.0",
   "aligator": "^0.0.1"
 }
}

首先,我将模块全局链接:

$ cd ~/aligator
$ npm link
/usr/local/lib/node_modules/aligator -> /Users/roc/aligator

如果我没弄错的话,我已经创建了我的模块aligator的全局引用,现在我可以在计算机的任何地方使用这个模块。

然后我去了另一个模块并试图安装依赖项,但它给了我这个输出:

$ cd ~/aligator-methods
$ npm install
npm ERR! 404 404 Not Found: aligator
npm ERR! 404
npm ERR! 404 'aligator' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it
npm ERR! 404 It was specified as a dependency of 'aligator-methods'
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, or http url, or git url.

npm ERR! System Darwin 13.2.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/roc/aligator-methods
npm ERR! node -v v0.10.28
npm ERR! npm -v 1.4.16
npm ERR! code E404
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/roc/aligator-methods/npm-debug.log
npm ERR! not ok code 0

我甚至尝试将其直接链接到:

$ cd ~/aligator-methods
$ npm link aligator
/Users/roc/aligator-methods/node_modules/aligator -> /usr/local/lib/node_modules/aligator -> /Users/roc/aligator

但它也没有用。

有关可能发生的事情的任何想法吗?我在某处读到可能与我安装节点 npm 因为它是由Homebrew制作的,所以有时候我需要使用sudo,这似乎不太可能,但我尝试了他们提出的建议但它也没有用。

编辑:解决我的具体问题

在我的情况下,问题是main的{​​{1}}属性指向一个不存在的文件。

11 个答案:

答案 0 :(得分:32)

问题是main的{​​{1}}属性指向一个不存在的文件。看来该问题可能是由多种原因引起的,因此请务必查看其他答案。

答案 1 :(得分:24)

由于NVM,我遇到了这个问题,我为依赖项运行了一个版本的节点,为依赖项运行了另一个版本的节点。

答案 2 :(得分:10)

首次从npm link目录运行aligator时,您将创建从全局node_modules目录到aligator的链接。然后,当您从npm link aligator目录运行aligator-methods时,将aligator从本地安装的node_modules链接到原始源(如上面示例中的输出所示)。完成后,不再需要安装,因为它已经“安装”了。运行npm link aligator命令后,您看到了哪些错误?

如果您只想从本地目录安装依赖项,则可以尝试使用npm install。例如:

  

$ cd~ / aligator-methods
  $ npm install ../ aligator

答案 3 :(得分:5)

删除package-lock.json然后运行npm install再次为我解决了这个问题。

答案 4 :(得分:1)

对我来说,当我将本地软件包的版本号从0.1.0减少到0.0.1时,就发生了这种情况。在我链接到这个包的项目中,我仍然使用更高版本号。更新package.json中的依赖项修复了它。

答案 5 :(得分:1)

修复我的此版本问题;在npm v5.3.0中,我从repo中删除了node_modules我正在链接到另一个项目。

我发现在npm v3之后,他们尝试将所有node_modules依赖项放入一个node_modules目录(项目中的一个),以尽可能地平整结构(http://codetunnel.io/npm-5-changes-to-npm-link/)。

答案 6 :(得分:0)

对我而言,解决方案是我的要求声明中有一个拼写错误。

require('my_liberry')

一旦我键入了正确的内容,它就有用了。

答案 7 :(得分:0)

对我有用的是:

  1. 删除依赖项和使用者模块中的node_modules
  2. 运行npm unlink --no-save [dependency-module]
  3. 按照npm-link重新链接2链接命令

现在,我可以在本地完全测试未发布的模块了。

此外,还有一个npm pack命令,尽管功能不那么强大,它可以帮助您测试未发布的模块。

npm-pack

答案 8 :(得分:0)

使用peerDependency时

我正在开发两个软件包stejsstejs-loaderstejs-loader具有stejs作为peerDependency。当我在项目中运行npm link stejs-loadernpm link stejs时,遇到一个错误,提示stejs-loader找不到stejs。我通过在npm link stejs目录中运行stejs-loader来解决此问题。

答案 9 :(得分:0)

检查tsconfig模块的分辨率

如果像我一样,您碰巧将tsconfig modulees5更改为esnext,则moduleResolution的默认设置可能已更改。

没有将moduleResolution设置为“节点”,打字稿将无法解析node_modules软件包。

您可以在Compiler Options页面上阅读有关默认值如何取决于module的值的信息,target的默认值依次取决于this.setState({ series: [ { name: 'Bob', data: [ { x: 'Block 396 HDB Tampines', y: [ new Date('2019-03-05').getTime(), new Date('2019-03-08').getTime() ] }, { x: 'Eastpoint Mall', y: [ new Date('2019-03-02').getTime(), new Date('2019-03-05').getTime() ] }, { x: 'Bata', y: [ new Date('2019-03-05').getTime(), new Date('2019-03-07').getTime() ] }, { x: 'Barracuda Tech', y: [ new Date('2019-03-03').getTime(), new Date('2019-03-09').getTime() ] }, { x: 'Aromas of India', y: [ new Date('2019-03-08').getTime(), new Date('2019-03-11').getTime() ] }, { x: '517 Food court', y: [ new Date('2019-03-11').getTime(), new Date('2019-03-16').getTime() ] }, ] }, { name: 'Joe', data: [ { x: 'Aljunied Industrial Estate', y: [ new Date('2019-03-02').getTime(), new Date('2019-03-05').getTime() ] }, { x: 'Bata', y: [ new Date('2019-03-06').getTime(), new Date('2019-03-16').getTime() ] }, { x: 'YinJi Singapore', y: [ new Date('2019-03-03').getTime(), new Date('2019-03-07').getTime() ] }, { x: 'Aromas of India', y: [ new Date('2019-03-20').getTime(), new Date('2019-03-22').getTime() ] }, { x: 'Djitsun Mall', y: [ new Date('2019-03-10').getTime(), new Date('2019-03-16').getTime() ] } ] }, ], }) ,但可能将其设置为“ node”明确地。

答案 10 :(得分:0)

我遇到了类似的问题,我必须执行以下步骤来解决它:

在图书馆

  1. 将产生问题的库设置为 peerDependencies 中的 package.json 而不是 dependenciesdevDependencies,例如就我而言react
"peerDependencies": {
  "react": "^16.8.6",
  ...
}
  1. 运行npm install
  2. 构建库(就我而言,使用 rollup -c npm 脚本)

在我的主应用中

  1. 更改我的库版本以指向我在 package.json 中具有相对路径的本地项目,例如
"dependencies": {
  "my-library": "file:../../libraries/my-library",
  ...
}
  1. resolve.symlinks = false 添加到我的主应用程序的 webpack 配置

  2. --preserve-symlinks-main--preserve-symlinks 添加到我的 package.json 启动脚本中,例如:

"scripts": {
  "build": "set WEBPACK_CONFIG_FILE=all&& webpack",
  "start": "set WEBPACK_CONFIG_FILE=all&& webpack && node --preserve-symlinks-main --preserve-symlinks dist/server.js",
}
  1. 运行npm install
  2. 运行npm run start