Giter Site home page Giter Site logo

global-npm's Introduction

global-npm

Require global npm as a local node module.

NPM version Deps Build Status Build status

js-standard-style

Npm is about 25MB in size now, which is quite a heavy dependency for most modules. Luckily, nearly every node.js user has a npm installed globally.

However, normally, you can't require a global module directly, unless:

  1. set NODE_PATH environment variable to the folder where global modules are installed.
  2. link the local project with global module to use.
  3. require a file path instead of a module name.

The last one should be the best way for a third-party module.

If your module needs to depend on npm, why not just use the global one?

Usage

var npm = require('global-npm')

If the version of npm counts, please use semver to check npm.version by yourself.

License

MIT@Dracupid

global-npm's People

Contributors

bfred-it avatar dracupid avatar jameskmonger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

global-npm's Issues

Globally installed NPM version 8 causes "Cannot find module 'npm'"

This is due to a missing index.js file within npm being referenced in its package,json main property.

Tested with npm installed via linux homebrew.

When running the tests included in this project with npm 8 the tests fail with
Error: The programmatic API was removed in npm v8.0.0

can not working with `yarn run`

In our scenario, we use "global-npm" like this as below:
package.json
{ scripts: { "test-global-npm": "node test.js" } }
test.js
const { GLOBAL_NPM_PATH } = require('global-npm'); console.log(GLOBAL_NPM_PATH)

add when we exec yarn run test-global-npm ,throw the error below:
throw err ^ Error: Cannot find module 'npm'

when I dive into this issue and found that "yarn" rewrited process.env.npm_execpath to " /Users/xxxx/.yarn/bin/yarn.js", and this behavior leads to the final exception.

Error: Cannot find module 'npm' on Windows

When run an app in npm scripts, global-npm report an error that cannot find 'npm'.

image

OS: Windows 10
node: v8


It seems to be related to the environment variable npm_execpath.

When echo in bash directly, it's undefined:

image

But when echo in npm script:

image
image

It's a different path than which npm:

image
which is linked to C:\Users\xucha\AppData\Roaming\nvm\v8.9.2\npm here

Issues where NPM@8 needs instanciating

There is an issue where exported npm module is a class which needs instantiating before it can be used with [email protected].

Below is a quick-and-dirty patch here where passing in the instanciate=true prop to your loader function correctly creates a singleton instance which is valid.

module.exports = (function requireNPM (npmPath, instanciate) {
  GLOBAL_NPM_PATH = npmPath
  try {
    var npm = require(npmPath)
    if (instanciate) npm = new npm();
    if (npm && Object.keys(npm).length) {
      return npm
    }
  } catch (e) {
    if (e.message.indexOf('The programmatic API was removed') >= 0) {
      // for npm >= 8. Maybe programmatic api via require('npm') is no longer available
      // https://github.com/npm/cli/pull/3762
      return requireNPM(path.join(npmPath, 'lib/npm.js'), true)
    } else if (e.code !== 'MODULE_NOT_FOUND') {
      throw e
    }
  }
  throwNotFoundError()
})(GLOBAL_NPM_PATH)

I'm not certain if this is needed in all cases though since your original patch in #7 seems to imply this was already working in npm@8? If so maybe extend the catch to have a "try to instantiate this" clause if its an event emitter class return.

Incompatible with nodenv

This module is incompatible with nodenv.

With nodenv, npm bin points to $HOME/.nodenv/shims/npm

/usr/local/lib/node_modules/ramda-cli/node_modules/global-npm/index.js:13
  throw err
  ^

Error: Cannot find module 'npm'
    at throwNotFoundError (/usr/local/lib/node_modules/ramda-cli/node_modules/global-npm/index.js:11:13)
    at /usr/local/lib/node_modules/ramda-cli/node_modules/global-npm/index.js:39:5
    at Object.<anonymous> (/usr/local/lib/node_modules/ramda-cli/node_modules/global-npm/index.js:41:3)

But there is at least one workaround.

# global-npm reads this
export GLOBAL_NPM_PATH="$HOME/.nodenv/versions/10.16.0/lib/node_modules/npm"

Error: Cannot find module 'npm' on Electron for macOS

Because Electron's ENV is not as same as cli, so it can't find npm.

Electron's ENV:

{
  "TMPDIR": "/var/folders/xxxxxxx/T/",
  "__CF_USER_TEXT_ENCODING": "xxxxxxxxxx",
  "SHELL": "/bin/zsh",
  "HOME": "/Users/xxxxxxx",
  "Apple_PubSub_Socket_Render": "/private/tmp/com.apple.launchd.xxxxxx/Render",
  "SSH_AUTH_SOCK": "/private/tmp/com.apple.launchd.xxxxx/Listeners",
  "PATH": "/usr/bin:/bin:/usr/sbin:/sbin",
  "LOGNAME": "xxxxxxxx",
  "XPC_SERVICE_NAME": "com.electron.xxxxxxx54228",
  "USER": "xxxxxxx",
  "XPC_FLAGS": "0x0"
}

Suggestion: maybe to do something like shell-env to fix the problem.

  • macOS 10.14.6
  • electron: 4.2.4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.