简体   繁体   English

如何以编程方式使用 NPM?

[英]How to use NPM programmatically?

After seeing this post and other resources, I wanted to try using NPM programmatically so simply I tried:看到这篇文章和其他资源后,我想尝试以编程方式使用 NPM,所以我尝试了:

const npm = require('npm');

but I run into an error of Error: Cannot find module 'npm' .但我遇到了一个错误Error: Cannot find module 'npm'

I did some research and all I saw was to try running npm install npm@latest -g which just updates NPM if I'm not mistaken but I ran it anyways and no change.我做了一些研究,我看到的只是尝试运行npm install npm@latest -g如果我没有记错,它只会更新 NPM,但我还是运行了它并且没有任何变化。 NPM is already installed globally and I did try installing it locally which got rid of the error but broke the rest of my project so I assume there's a reason that wasn't recommended. NPM 已经在全球范围内安装,我确实尝试在本地安装它消除了错误但破坏了我项目的其余部分,所以我认为有一个不推荐的原因。

All of the posts I've seen have been older so is this not supported anymore?我看过的所有帖子都比较旧了,所以现在不再支持了吗? All the documentation I can find is about the cli and not using it as a module.我能找到的所有文档都是关于 cli 的,而不是将它用作模块。

Use

$ npm i npm

without the global -g Flag没有全局-g标志


Note: The NPM API is not intended for external use (even tho it is somewhat documented).注意: NPM API 不适合外部使用(即使它有一些文档记录)。 The problem is, the NPM API does not guarantee stability.问题是,故宫API并不能保证稳定性。 In fact, it doesn't even follow semantic versioning.事实上,它甚至不遵循语义版本控制。 An update to the NPM API is likely to break your script if you heavily rely on it.如果您严重依赖 NPM API,更新 NPM API 可能会破坏您的脚本。

If you want to use NPM externally, it would be better to use it as it is right now: a command-line program.如果你想在外部使用 NPM,最好使用它现在的样子:一个命令行程序。

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

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