简体   繁体   中英

How to use NPM programmatically?

After seeing this post and other resources, I wanted to try using NPM programmatically so simply I tried:

const npm = require('npm');

but I run into an error of 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 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.

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.

Use

$ npm i npm

without the global -g Flag


Note: The NPM API is not intended for external use (even tho it is somewhat documented). The problem is, the NPM API does not guarantee stability. 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.

If you want to use NPM externally, it would be better to use it as it is right now: a command-line program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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