npm 安装本地模块的依赖项 [英] npm install dependencies of local modules

查看:290
本文介绍了npm 安装本地模块的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用带有本地模块的 npm 时遇到问题.我的项目结构如下:

I'm having trouble using npm with a local module. My project's structure looks like:

package.json
local_module/
    - package.json
    - gulpfile.json
gulpfile.js

主项目的package.json本质上是:

{
    "dependencies": {
        "local_module": "file:local_module"
    },
    "devDependencies": {
        "gulp": "..."
    }
}

本地模块的package.json本质上是:

{
    "scripts": {
        "prepublish": "gulp release"
    },
    "devDependencies": {
        "gulp": "..."
    }
}

我的目的是通过将 local_module 作为自己的包作为主项目的依赖项来保持我的项目模块化.我想在主项目中运行 npm install 并使用 node_modules 中的 local_module.但是,local_module 需要安装 gulp 来运行 prepublish 步骤,并且当从主项目运行 npm install 时,它不会安装local_module,所以没有安装 gulp,所以它不能执行预发布步骤.

My intention is keep my project modular by keeping local_module as its own package that is used as a dependency for the main project. I want to run npm install in the main project and use local_module from node_modules. However, local_module needs gulp installed to run the prepublish step, and when running npm install from the main project, it does not install the dependencies for local_module, and so gulp isn't installed, so it can't do the prepublish step.

已经问了几个这样的问题,比如NPM 不安装模块依赖,但很多都是旧版本,而且 npm 版本太多,我找不到明确的解决方案.

Several questions like this have been asked, like NPM doesn't install module dependencies, but many are old and there are so many versions of npm that I can't get a clear solution.

如何在预发布步骤之前让 npm 安装 local_module 的依赖项?我尝试为主项目添加预安装步骤,例如

How can I get npm to install local_module's dependencies before the prepublish step? I tried adding a preinstall step for the main project, e.g.

"preinstall": "cd local_module && npm install"

但似乎 npm 在运行主项目的预安装之前尝试运行 local_module 的预发布步骤.我想要一个解决方案,它可以在一个 npm install 步骤中完成此操作,而不是在此之前有一个单独的步骤在本地模块中执行 npm install.

But it seems npm tries to run the prepublish step of local_module before running the preinstall for the main project. I want a solution that will do this in one npm install step rather than having a separate step before this to do npm install in the local module.

推荐答案

我找到了一个在不久的将来对我有用的解决方案.我将 local_modulepackage.json 更改为:

I have found a solution that will work for me in the immediate future. I changed local_module's package.json to:

{
    "scripts": {
        "prepublish": "npm install --ignore-scripts && gulp release"
    },
    "devDependencies": {
        "gulp": "..."
    }
}

npm install 从主项目运行时,预发布步骤首先在 local_module 中运行,所以我强制预发布也进行安装,以便 gulp 可用做实际的预发布步骤.然而,这并不理想.

When npm install is run from the main project, the prepublish step is run first in local_module, so I force prepublish to also do an install so that gulp is available to do the actual prepublish step. This is hardly ideal however.

这篇关于npm 安装本地模块的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆