├── .npmrc ├── .gitattributes ├── .github ├── SECURITY.md └── workflows │ └── ci.yml ├── .verb.md ├── .editorconfig ├── .gitignore ├── test.js ├── LICENSE ├── package.json ├── index.js ├── README.md └── .eslintrc.json /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | ## Security contact information 2 | 3 | To report a security vulnerability, please use the 4 | [Tidelift security contact](https://tidelift.com/security). 5 | Tidelift will coordinate the fix and disclosure. 6 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | This is partially based on the code used by npm internally to resolve the global prefix. 2 | 3 | ## Usage 4 | 5 | ```js 6 | var prefix = require('{%= name %}'); 7 | //=> '/usr/local' (this path will differ by system and user-defined config) 8 | ``` 9 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org/ 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | indent_style = space 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | package-lock.json -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * global-prefix 3 | * 4 | * Copyright (c) 2015 Jon Schlinkert. 5 | * Licensed under the MIT license. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | require('mocha'); 11 | const fs = require('fs'); 12 | const path = require('path'); 13 | const assert = require('assert'); 14 | const prefix = require('./'); 15 | 16 | describe('prefix', () => { 17 | it('should resolve the path to the windows global prefix', () => { 18 | if (process.platform === 'win32') { 19 | assert.equal(prefix, process.env.APPDATA ? path.join(process.env.APPDATA, 'npm') : path.dirname(process.execPath)); 20 | } 21 | }); 22 | 23 | it('should resolve the path to the global prefix', () => { 24 | assert(fs.existsSync(prefix)); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: [push, pull_request] 3 | env: 4 | CI: true 5 | 6 | jobs: 7 | test: 8 | name: Tests for Node ${{ matrix.node }} on ${{ matrix.os }} 9 | runs-on: ${{ matrix.os }} 10 | 11 | strategy: 12 | fail-fast: false 13 | matrix: 14 | node: [16, 18, 20, 22] 15 | os: 16 | - ubuntu-latest 17 | # Windows tests are currently broken 18 | # - windows-latest 19 | - macos-latest 20 | 21 | steps: 22 | - name: Clone repository 23 | uses: actions/checkout@v4 24 | 25 | - name: Set Node.js version 26 | uses: actions/setup-node@v4 27 | with: 28 | node-version: ${{ matrix.node }} 29 | 30 | - run: node --version 31 | - run: npm --version 32 | 33 | - name: Install npm dependencies 34 | run: npm install 35 | 36 | - name: Run tests 37 | run: npm test 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-present, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "global-prefix", 3 | "description": "Get the npm global path prefix.", 4 | "version": "4.0.0", 5 | "homepage": "https://github.com/jonschlinkert/global-prefix", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "contributors": [ 8 | "Alexandr Bogachev (https://github.com/rmbaad)", 9 | "Brian Woodward (https://twitter.com/doowb)", 10 | "Charlike Mike Reagent (https://i.am.charlike.online)", 11 | "JasonChang (https://packagist.org/packages/jason-chang)", 12 | "Jon Schlinkert (http://twitter.com/jonschlinkert)", 13 | "Jorrit Schippers (https://www.ncode.nl)", 14 | "Mathias Rasmussen (chrome://dino)", 15 | "Ross Fenning (http://rossfenning.co.uk)" 16 | ], 17 | "repository": "jonschlinkert/global-prefix", 18 | "bugs": { 19 | "url": "https://github.com/jonschlinkert/global-prefix/issues" 20 | }, 21 | "license": "MIT", 22 | "files": [ 23 | "index.js" 24 | ], 25 | "main": "index.js", 26 | "engines": { 27 | "node": ">=16" 28 | }, 29 | "scripts": { 30 | "test": "mocha" 31 | }, 32 | "dependencies": { 33 | "ini": "^4.1.3", 34 | "kind-of": "^6.0.3", 35 | "which": "^4.0.0" 36 | }, 37 | "devDependencies": { 38 | "mocha": "^10.7.3" 39 | }, 40 | "keywords": [ 41 | "global", 42 | "module", 43 | "modules", 44 | "npm", 45 | "path", 46 | "prefix", 47 | "resolve" 48 | ] 49 | } 50 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * global-prefix 3 | * 4 | * Copyright (c) 2015-present Jon Schlinkert. 5 | * Licensed under the MIT license. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | const fs = require('fs'); 11 | const os = require('os'); 12 | const path = require('path'); 13 | const ini = require('ini'); 14 | let prefix; 15 | 16 | const getPrefix = () => { 17 | if (process.env.PREFIX) return process.env.PREFIX; 18 | if (prefix) return prefix; 19 | 20 | // Start by checking if the global prefix is set by the user 21 | let home = os.homedir(); 22 | 23 | // os.homedir() returns undefined if $HOME is not set; path.resolve requires strings 24 | if (home) { 25 | prefix = tryConfigPath(path.resolve(home, '.npmrc')); 26 | } 27 | 28 | if (prefix) { 29 | return prefix; 30 | } 31 | 32 | // Otherwise find the path of npm 33 | let npm = tryNpmPath(); 34 | if (npm) { 35 | // Check the built-in npm config file 36 | prefix = tryConfigPath(path.resolve(npm, '..', '..', 'npmrc')); 37 | 38 | if (prefix) { 39 | // Now the global npm config can also be checked. 40 | prefix = tryConfigPath(path.resolve(prefix, 'etc', 'npmrc')) || prefix; 41 | } 42 | } 43 | 44 | if (!prefix) { 45 | let { APPDATA, DESTDIR, OSTYPE } = process.env; 46 | 47 | // c:\node\node.exe --> prefix=c:\node\ 48 | if (process.platform === 'win32' || OSTYPE === 'msys' || OSTYPE === 'cygwin') { 49 | prefix = APPDATA ? path.join(APPDATA, 'npm') : path.dirname(process.execPath); 50 | return prefix; 51 | } 52 | 53 | // /usr/local/bin/node --> prefix=/usr/local 54 | prefix = path.dirname(path.dirname(process.execPath)); 55 | 56 | // destdir only is respected on Unix 57 | if (DESTDIR) { 58 | prefix = path.join(DESTDIR, prefix); 59 | } 60 | } 61 | 62 | return prefix; 63 | } 64 | 65 | function tryNpmPath() { 66 | try { 67 | return fs.realpathSync(require('which').sync('npm')); 68 | } catch (err) { /* do nothing */ } 69 | } 70 | 71 | function tryConfigPath(configPath) { 72 | try { 73 | return ini.parse(fs.readFileSync(configPath, 'utf-8')).prefix; 74 | } catch (err) { /* do nothing */ } 75 | } 76 | 77 | /** 78 | * Expose `prefix` 79 | */ 80 | 81 | Reflect.defineProperty(module, 'exports', { 82 | get() { 83 | return getPrefix(); 84 | } 85 | }); 86 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # global-prefix [![NPM version](https://img.shields.io/npm/v/global-prefix.svg?style=flat)](https://www.npmjs.com/package/global-prefix) [![NPM monthly downloads](https://img.shields.io/npm/dm/global-prefix.svg?style=flat)](https://npmjs.org/package/global-prefix) [![NPM total downloads](https://img.shields.io/npm/dt/global-prefix.svg?style=flat)](https://npmjs.org/package/global-prefix) [![CI Status](https://img.shields.io/github/actions/workflow/status/jonschlinkert/global-prefix/ci.yml)](https://github.com/jonschlinkert/global-prefix/actions) 2 | 3 | > Get the npm global path prefix. 4 | 5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. 6 | 7 | ## Install 8 | 9 | Install with [npm](https://www.npmjs.com/): 10 | 11 | ```sh 12 | $ npm install --save global-prefix 13 | ``` 14 | 15 | This is partially based on the code used by npm internally to resolve the global prefix. 16 | 17 | ## Usage 18 | 19 | ```js 20 | var prefix = require('global-prefix'); 21 | //=> '/usr/local' (this path will differ by system and user-defined config) 22 | ``` 23 | 24 | ## About 25 | 26 |
27 | Contributing 28 | 29 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 30 | 31 |
32 | 33 |
34 | Running Tests 35 | 36 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 37 | 38 | ```sh 39 | $ npm install && npm test 40 | ``` 41 | 42 |
43 | 44 |
45 | Building docs 46 | 47 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 48 | 49 | To generate the readme, run the following command: 50 | 51 | ```sh 52 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 53 | ``` 54 | 55 |
56 | 57 | ### Related projects 58 | 59 | You might also be interested in these projects: 60 | 61 | * [global-modules](https://www.npmjs.com/package/global-modules): The directory used by npm for globally installed npm modules. | [homepage](https://github.com/jonschlinkert/global-modules "The directory used by npm for globally installed npm modules.") 62 | * [global-paths](https://www.npmjs.com/package/global-paths): Returns an array of unique "global" directories based on the user's platform and environment. The… [more](https://github.com/jonschlinkert/global-paths) | [homepage](https://github.com/jonschlinkert/global-paths "Returns an array of unique "global" directories based on the user's platform and environment. The resulting paths can be used for doing lookups for generators or other globally installed npm packages. Node.js / JavaScript.") 63 | 64 | ### Contributors 65 | 66 | | **Commits** | **Contributor** | 67 | | --- | --- | 68 | | 23 | [jonschlinkert](https://github.com/jonschlinkert) | 69 | | 15 | [doowb](https://github.com/doowb) | 70 | | 2 | [phated](https://github.com/phated) | 71 | | 1 | [rmbaad](https://github.com/rmbaad) | 72 | | 1 | [avengerpenguin](https://github.com/avengerpenguin) | 73 | | 1 | [jorrit](https://github.com/jorrit) | 74 | | 1 | [mathiasvr](https://github.com/mathiasvr) | 75 | | 1 | [tunnckoCore](https://github.com/tunnckoCore) | 76 | 77 | ### Author 78 | 79 | **Jon Schlinkert** 80 | 81 | * [GitHub Profile](https://github.com/jonschlinkert) 82 | * [Twitter Profile](https://twitter.com/jonschlinkert) 83 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) 84 | 85 | ### License 86 | 87 | Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). 88 | Released under the [MIT License](LICENSE). 89 | 90 | *** 91 | 92 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on October 11, 2018._ -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint:recommended" 4 | ], 5 | 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | 13 | "parserOptions":{ 14 | "ecmaVersion": 9, 15 | "sourceType": "module", 16 | "ecmaFeatures": { 17 | "modules": true, 18 | "experimentalObjectRestSpread": true 19 | } 20 | }, 21 | 22 | "globals": { 23 | "document": false, 24 | "navigator": false, 25 | "window": false 26 | }, 27 | 28 | "rules": { 29 | "accessor-pairs": 2, 30 | "arrow-spacing": [2, { "before": true, "after": true }], 31 | "block-spacing": [2, "always"], 32 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 33 | "comma-dangle": [2, "never"], 34 | "comma-spacing": [2, { "before": false, "after": true }], 35 | "comma-style": [2, "last"], 36 | "constructor-super": 2, 37 | "curly": [2, "multi-line"], 38 | "dot-location": [2, "property"], 39 | "eol-last": 2, 40 | "eqeqeq": [2, "allow-null"], 41 | "generator-star-spacing": [2, { "before": true, "after": true }], 42 | "handle-callback-err": [2, "^(err|error)$" ], 43 | "indent": [2, 2, { "SwitchCase": 1 }], 44 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 45 | "keyword-spacing": [2, { "before": true, "after": true }], 46 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 47 | "new-parens": 2, 48 | "no-array-constructor": 2, 49 | "no-caller": 2, 50 | "no-class-assign": 2, 51 | "no-cond-assign": 2, 52 | "no-const-assign": 2, 53 | "no-control-regex": 2, 54 | "no-debugger": 2, 55 | "no-delete-var": 2, 56 | "no-dupe-args": 2, 57 | "no-dupe-class-members": 2, 58 | "no-dupe-keys": 2, 59 | "no-duplicate-case": 2, 60 | "no-empty-character-class": 2, 61 | "no-eval": 2, 62 | "no-ex-assign": 2, 63 | "no-extend-native": 2, 64 | "no-extra-bind": 2, 65 | "no-extra-boolean-cast": 2, 66 | "no-extra-parens": [2, "functions"], 67 | "no-fallthrough": 2, 68 | "no-floating-decimal": 2, 69 | "no-func-assign": 2, 70 | "no-implied-eval": 2, 71 | "no-inner-declarations": [2, "functions"], 72 | "no-invalid-regexp": 2, 73 | "no-irregular-whitespace": 2, 74 | "no-iterator": 2, 75 | "no-label-var": 2, 76 | "no-labels": 2, 77 | "no-lone-blocks": 2, 78 | "no-mixed-spaces-and-tabs": 2, 79 | "no-multi-spaces": 2, 80 | "no-multi-str": 2, 81 | "no-multiple-empty-lines": [2, { "max": 1 }], 82 | "no-native-reassign": 0, 83 | "no-negated-in-lhs": 2, 84 | "no-new": 2, 85 | "no-new-func": 2, 86 | "no-new-object": 2, 87 | "no-new-require": 2, 88 | "no-new-wrappers": 2, 89 | "no-obj-calls": 2, 90 | "no-octal": 2, 91 | "no-octal-escape": 2, 92 | "no-proto": 0, 93 | "no-redeclare": 2, 94 | "no-regex-spaces": 2, 95 | "no-return-assign": 2, 96 | "no-self-compare": 2, 97 | "no-sequences": 2, 98 | "no-shadow-restricted-names": 2, 99 | "no-spaced-func": 2, 100 | "no-sparse-arrays": 2, 101 | "no-this-before-super": 2, 102 | "no-throw-literal": 2, 103 | "no-trailing-spaces": 0, 104 | "no-undef": 2, 105 | "no-undef-init": 2, 106 | "no-unexpected-multiline": 2, 107 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 108 | "no-unreachable": 2, 109 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 110 | "no-useless-call": 0, 111 | "no-with": 2, 112 | "one-var": [0, { "initialized": "never" }], 113 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 114 | "padded-blocks": [0, "never"], 115 | "quotes": [2, "single", "avoid-escape"], 116 | "radix": 2, 117 | "semi": [2, "always"], 118 | "semi-spacing": [2, { "before": false, "after": true }], 119 | "space-before-blocks": [2, "always"], 120 | "space-before-function-paren": [2, "never"], 121 | "space-in-parens": [2, "never"], 122 | "space-infix-ops": 2, 123 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 124 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 125 | "use-isnan": 2, 126 | "valid-typeof": 2, 127 | "wrap-iife": [2, "any"], 128 | "yoda": [2, "never"] 129 | } 130 | } 131 | --------------------------------------------------------------------------------