Skip to content

How does require.resolve work? #8526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ccorcos opened this issue Sep 14, 2016 · 9 comments
Closed

How does require.resolve work? #8526

ccorcos opened this issue Sep 14, 2016 · 9 comments
Labels
feature request Issues that request new features to be added to Node.js. module Issues and PRs related to the module subsystem. question Issues that look for answers.

Comments

@ccorcos
Copy link

ccorcos commented Sep 14, 2016

I'd like to submit a PR that resolves the root project's package name so that you can use absolute require paths. However, I'm finding it very hard to figured out how this bit of code works:

https://github.com/nodejs/node/blob/7b73f559029a10474b74a83bfb1117ab512785d4/lib/module.js

Seems like it should be a one-liner somewhere. I just need to add the base project as one of the "paths" to search. Any ideas where that might be?

I'm thinking its in here.

Module._resolveLookupPaths = function(request, parent) {

Something like paths.push(path.dirname(parent.id)).

@cjihrig
Copy link
Contributor

cjihrig commented Sep 14, 2016

Can you give an example, preferably in runnable code, that demonstrates exactly what you want to happen? Also, keep in mind that the chances of landing a PR that changes the module system behavior is extremely small.

@mscdex mscdex added question Issues that look for answers. module Issues and PRs related to the module subsystem. labels Sep 14, 2016
@imyller
Copy link
Member

imyller commented Sep 14, 2016

Be careful when attempting to resolve paths/files in the parent path of current package.

For example, current package could be npm link symlinked to the project and when attempting to resolve root project's location you might wander into completely different root path or even filesystem inaccessible to current user.

All this assuming I understood correctly your intentions about "resolving the root project's package name".

As @cjihrig suggested, runnable code would demonstrate your intentions best.

@ccorcos
Copy link
Author

ccorcos commented Sep 14, 2016

@cjihrig Here's basically what I want to work:

mkdir cool-app
cd cool-app
npm init -y
echo "module.exports = { name: 'chet' }" > defs.js
echo "console.log('hello' + require('cool-app/defs').name)" > index.js
node index.js
> hello chet

basically cool-app resolves to the project root, just like it were an npm package dependency.

@imyller
Copy link
Member

imyller commented Sep 15, 2016

So you basicly want virtual version of:

ln -sf .. node_modules/<root package name>

Tested this and symlink actually works and allows require('<root package name>') aliasing.

If this is implemented in module.js resolver, I'm bit worried about (theoretically) possible name collision with npm installed packages.

@cjihrig cjihrig added the feature request Issues that request new features to be added to Node.js. label Sep 16, 2016
@cjihrig
Copy link
Contributor

cjihrig commented Sep 16, 2016

I'm not sure that making this change is worth the risk, especially when you can already use relative paths. I'll leave this open in case another collaborator is willing to champion this.

@evanlucas
Copy link
Contributor

-1 from me. The module system is locked.

@imyller
Copy link
Member

imyller commented Sep 18, 2016

-1 from me too.

Locked module API and high risk for hard-to-debug userland regressions if implemented.

@JacksonTian
Copy link
Contributor

-1 from me.

@cjihrig
Copy link
Contributor

cjihrig commented Sep 18, 2016

I think there are enough -1s to warrant closing this. Thanks anyway though!

@cjihrig cjihrig closed this as completed Sep 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. module Issues and PRs related to the module subsystem. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

6 participants