简体   繁体   English

当有人安装我的 npm 模块时,如何告诉 npm 不要安装任何依赖项?

[英]How to tell npm to not to install any dependency when somebody installs my npm module?

I have created an NPM module which is already built and published.我创建了一个已经构建和发布的 NPM 模块。 so when somebody installs it, my module doesn't really need any extra dependency to work properly as it is already built.所以当有人安装它时,我的模块实际上不需要任何额外的依赖来正常工作,因为它已经构建好了。

However, the current behavior is that when I install my module in some other repository it updates some other modules in package-lock.json但是,当前的行为是,当我在其他存储库中安装我的模块时,它会更新 package-lock.json 中的其他一些模块

Is there a way to avoid this behavior as my module is already pre-built and doesn't need any dependency to work properly?有没有办法避免这种行为,因为我的模块已经预先构建并且不需要任何依赖即可正常工作?

One way you can do this is create your project to be a nested project.一种方法是将项目创建为嵌套项目。

main-project
|- package.json
|- sub-project
   |-package.json

Once you build your files in the main-project place them in sub-project whose package.json has no dependencies listed.main-project构建文件后,将它们放在package.json没有列出依赖项的sub-project You can then publish your inner sub-project to npm as a dependency free module.然后,您可以将内部sub-project作为无依赖模块发布到 npm。

That being said, I think the common practice is to include your dependencies as usual - as long as you export your built file correctly and the users import them correctly, it should not matter that your dependencies are installed or not - when they build, ideally they include only what they need (and not your project's dependencies) if all goes well.话虽如此,我认为通常的做法是像往常一样包含您的依赖项 - 只要您正确导出构建的文件并且用户正确导入它们,您的依赖项是否安装无关紧要 - 理想情况下,当它们构建时如果一切顺利,它们只包含他们需要的东西(而不是你项目的依赖项)。

NPM has something called optionalDependencies . NPM 有一个叫做optionalDependencies东西。

npm install package-name --save-optional

This command will save your package as an optional dependency.此命令会将您的包保存为可选依赖项。

Then you can use然后你可以使用

npm install --no-optional

to prevent installing the optional dependencies.以防止安装可选的依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM