使用与package.json不同的配置使用npm [英] use npm with different configuration than package.json

查看:314
本文介绍了使用与package.json不同的配置使用npm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由package.json驱动的复杂生产环境.

I have a complex production environment driven by package.json.

问题: 我希望在本地安装一些其他软件包,请密切注意它们的列表和版本.

The problem: I wish to install locally some additional packages, keep an eye on the list and versions of them.

解决方案(到达那里的方法): 指向 npm 来使用另一个配置文件(不包括在git中),这将保留我的私人依赖关系.使用文件通过 npm install 将软件包添加到本地 node_modules .因此,实际上我只需要更改npm的配置上下文.

Solution (how to get there): point npm to use another config file, excluded from git, which would keep my private dependencies. Use the file to add packages to local node_modules with npm install. So actually all I need is to change configuration context of npm.

我不知道如何指向npm来使用其他配置(例如 gulp -gulpfile ).

I have no clue how to point npm to use a different config (something like gulp -gulpfile).

从评论中更新不能依赖开发者.我使用的东西90%的其他开发人员不需要在他们的node_modules中安装(实际上,我可以破坏的环境以一种奇怪的方式来更新git共享的核心项目范围的中的开发依赖项. > package.json ).

Update from comments The dev-dependencies is not the way to go. I use stuff 90% of other developers do not need to get installed in their node_modules (in fact I could break their environment in a strange way updating dev-dependencies in git-shared core project-wide package.json).

推荐答案

首先,您应该知道您尝试做的不仅很奇怪,而且还违反了NodeJS中的许多良好实践和模式.

First of all, you should know that what you are trying to is not only weird, but also against a lot of good practices and patterns in NodeJS.

尽管如此,如果可行的话,这是有可能的,几乎不会给您或其他平台上的其他开发人员带来任何麻烦.

Nevertheless, it's possible and if it's done right will almost never cause any trouble to you, or other developers in different platforms.

您可以在 Meteor 构建流程中使用一些启发.让我们从概念上将您的项目分为两个不同的部分:

You can use something a little inspired on the Meteor build flow. Let's break your project conceptually into two different parts:

  • 基础:处理项目初始化.
  • 内部原始项目.
  • Base: Handles project initialization.
  • Internal The original project.

项目结构:

- build.js (base build script)
- package.json (base package)
- internal/
  - package.template.json (project package template)
  - app.js (your actual project files)
  - [...]  (your actual project files)

起点是在项目的根目录中为 Base 创建一个package.json文件.该软件包将仅包含用于生成 Internal 项目的软件包文件的依赖项.我强烈建议您使用 Mozilla的Convict 之类的东西来确保使用ENV正确完成配置.此处的目标是在运行时编写另一个package.json文件.

The starting point is create a package.json file to the Base, in the root of the project. This package will hold only the dependencies to build the package file of the Internal project. I strongly advise you to use something like Mozilla's Convict to ensure the configurations are done correctly using ENVs. The goal here is to write another package.json file at runtime.

我们还假设您的环境中有一个环境变量USE_WEIRD_MODULES="true".您可以在 Base 项目中使用此事实,以使用 build.js 文件定义要安装的模块.您可以阅读它并在运行时进行编辑.准备就绪后,将其另存为internal/package.json.然后,在internal/目录中运行npm install.

Let's also assume in the your environment there's an Environment Variable USE_WEIRD_MODULES="true". You can use this fact in the Base project to define which modules you are going to install using the build.js file. You can read it and edit in runtime. When it's ready, save it as internal/package.json. Then, run npm install inside the internal/ directory.


build.js

let pkg = require('./package.template.json');    

if(process.env.USE_WEIRD_MODULES) {

    // Install your weird packages        
    pkg.dependencies.some_dev_package = '^v0.2.2';
    pkg.dependencies.another_dev_package = '^v0.2.2';


}

// Don't forget to save the package.json in the end
fs.writeFileSync('./internal/package.json', JSON.stringify(pkg, null, 2), 'utf8');

请不要忘记将internal/package.json文件放入存储库忽略文件(例如gitignore)中.

Don't forget to put the internal/package.json file in your repository ignore file (ex: gitignore).

为最大程度地减少这种粗暴更改对项目的影响,您可以在主package.json中的postinstall脚本中定义所有构建例程.它将允许在项目根目录中使用简单的npm install在内部处理所有这些步骤.

To minimize the impact of this brute changes in the project, you can define in the main package.json all the building routine in the postinstall script. It will allow a simple npm install in the root of the project handle all this steps internally.

类似这样的东西:

package.json

{
  [...]
  "scripts": {
    "postinstall": "node build.js && cd internal/ && npm install",
    "start": "node internal/app.js"
  }
  [...]
}

这篇关于使用与package.json不同的配置使用npm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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