Code Monkey home page Code Monkey logo

resolve-from's Introduction

resolve-from

Resolve the path of a module like require.resolve() but from a given path

Install

$ npm install resolve-from

Usage

const resolveFrom = require('resolve-from');

// There is a file at `./foo/bar.js`

resolveFrom('foo', './bar');
//=> '/Users/sindresorhus/dev/test/foo/bar.js'

API

resolveFrom(fromDirectory, moduleId)

Like require(), throws when the module can't be found.

resolveFrom.silent(fromDirectory, moduleId)

Returns undefined instead of throwing when the module can't be found.

fromDirectory

Type: string

Directory to resolve from.

moduleId

Type: string

What you would use in require().

Tip

Create a partial using a bound function if you want to resolve from the same fromDirectory multiple times:

const resolveFromFoo = resolveFrom.bind(null, 'foo');

resolveFromFoo('./bar');
resolveFromFoo('./baz');

resolve-from for enterprise

Available as part of the Tidelift Subscription.

The maintainers of resolve-from and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

Related

  • resolve-cwd - Resolve the path of a module from the current working directory
  • import-from - Import a module from a given path
  • import-cwd - Import a module from the current working directory
  • resolve-pkg - Resolve the path of a package regardless of it having an entry point
  • import-lazy - Import a module lazily
  • resolve-global - Resolve the path of a globally installed module

resolve-from's People

Contributors

bendingbender avatar coreyfarrell avatar ranyitz avatar richienb avatar samverschueren avatar sindresorhus avatar zkochan 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  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  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  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

resolve-from's Issues

Require resolves to the sibling node_modules if lookup failed in the given dir.

Hi! ๐Ÿฎ

I have the following dir structure:
some-dir/node_modules/mah-package
some-dir/mah-project/resolve-from-here

If I run

resolveFrom('resolve-from-here', 'mah-package')

it resolves to some-dir/node_modules/mah-package. This can be avoided by keeping only subdirs of the given dir here: https://github.com/sindresorhus/resolve-from/blob/master/index.js#L18

I can issue a PR if you confirm that it's not expected behavior.

Use the real path for resolving dependencies

I created a similar issue at the resolve repo.

Node uses the real path of modules (except when executed with --preserve-symlinks) to resolve dependencies. That is why pnpm's symlinked node_modules work fine with Node applications.

However, resolve-from does not use the real path of modules to resolve dependencies. As a consequence, toolings that use resolve-from break on projects that use pnpm. These are issues related to resolve but I checked them with resolve-from and they fail with it as well: pnpm/pnpm#857, pnpm/pnpm#801

Would you accept a pull request to change the behavior of resolve-from to work the way Node does? It won't change anything for projects that use npm/Yarn because they don't leverage symlinks but it would be a huge help for the pnpm community!

Support for not using realpath

https://github.com/sindresorhus/resolve-from/blob/main/index.js#L16 unconditionally uses realpathSync which is a problem in contexts where we don't want to give lstat permissisons to every directory in the hierarchy (sandboxes for example). Node's --preserve-symlinks --preserve-symlinks-main options let you work around this.

I'm running into this because this is how eslint loads it's configuration (through import-fresh). Happy to take a stab at this if this something you want to support in this project.

dependencies were not found

These dependencies were not found:
module in ./node_modules/require_optional/node_modules/resolve-from/index.js

I'm using Nuxt 2.0.0 & mongoose 5.4.0 & mongodb 3.1.10

Add Yarn PnP support?

Yarn v2 is moving away from node_modules to use Plug'n'Play instead. If I understand this all correctly, it would be very helpful if this package supported PnP-based resolution in addition to the traditional node_modules approach, since many other packages rely on this package for resolution as an alternative to require.resolve. For reference:
Yarn Plug'n'Play

Thanks for all of your amazing open source work!

Browserify support

Does not work in browserify.

paths: Module._nodeModulePaths(fromDir)

index.js:20 Uncaught TypeError: Module._nodeModulePaths is not a function
    at resolveFileName (index.js:20)
    at resolveFrom (index.js:31)
    at module.exports (index.js:34)
    at module.exports.moduleId (index.js:11)
    at Object.5../foo (index.js? [sm]:9)
    at o (_prelude.js:1)
    at r (_prelude.js:1)
    at _prelude.js:1

Based on this issue (#2), I would guess that you would say this is Browserify's responsibility, but posted for posterity

Resolving require('module') for webpack

Mongoose is meant to work in the browser. However, including their module in webpack is broken. Determined to resolve this for them, I did a bit of digging. npm ls in a package using mongoose reveals:

โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”‚ โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”‚ โ”‚ โ””โ”€โ”ฌ [email protected]
โ”‚ โ”‚ โ”‚   โ”œโ”€โ”€ [email protected]        # Here's you!
โ”‚ โ”‚ โ”‚   โ””โ”€โ”€ [email protected]
โ”‚ โ”‚ โ””โ”€โ”ฌ [email protected]
โ”‚ โ”‚   โ”œโ”€โ”€ [email protected]
โ”‚ โ”‚   โ””โ”€โ”€ [email protected]
โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”‚ โ””โ”€โ”€ [email protected]
โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ””โ”€โ”€ [email protected]

The error the webpack build is outputting is:

ERROR in ./~/require_optional/~/resolve-from/index.js
Module not found: Error: Cannot resolve module 'module' in /<my project>/node_modules/require_optional/node_modules/resolve-from
 @ ./~/require_optional/~/resolve-from/index.js 3:13-30

The dependency it is referring to is in this package:

'use strict';
var path = require('path');
var Module = require('module');  // Here's the problem!!!

// ...

I'm sure solving their build will take more than resolving this and it's understandable if you're not supporting webpack. This is just an investigation that's piqued my interest.

So, two questions/requests:

  1. module is not a dependency of this package. Where is it coming from? What is this?
  2. Is there a different solution that would work in both cases? Is it out of the scope of this package to have a .web.js implementation of this to resolve the issue for webpack?

Test fails under node.js 12 due to expanded error message.

Identified while working sindresorhus/meta#10, I didn't want to mix any fixes in with the bulk change PR's. Two ways I can think to fix this, do you have a preference? I think the t.is() way since it gives better information if it fails?

t.is(moduleNotFoundError.message.split(/[\n\r]+/)[0], 'Cannot find module \'./nonexistent\'');
t.true(moduleNotFoundError.message.startsWith('Cannot find module \'./nonexistent\''));

Error message:

  1 test failed

  resolveFrom()

  /usr/src/npm/sindresorhus/resolve-from/test.js:14

   13:   t.is(moduleNotFoundError.code, 'MODULE_NOT_FOUND');                       
   14:   t.is(moduleNotFoundError.message, 'Cannot find module \'./nonexistent\'');
   15:                                                                             

  Difference:

  - `Cannot find module './nonexistent'โŠ
  - Require stack:โŠ
  - - /usr/src/npm/sindresorhus/resolve-from/fixture/noop.js`
  + `Cannot find module './nonexistent'

Support for extensions other than `.js`

I work with TS. I'd like to use resolve-from to resolve paths to .ts files. However, this module only considers .js files. Would you consider adding a configurable set of extensions, like what we can do with Webpack?

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.