Skip to content
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

Ways to use global mdast plugins with CLI? #46

Closed
eush77 opened this issue Sep 6, 2015 · 16 comments
Closed

Ways to use global mdast plugins with CLI? #46

eush77 opened this issue Sep 6, 2015 · 16 comments
Labels

Comments

@eush77
Copy link
Contributor

eush77 commented Sep 6, 2015

What is the preferred way of using globally installed plugins with CLI?

$ echo "# hello" | mdast -u mdast-html
# hello

<stdin>
        1:1  error    Error: Cannot find module 'mdast-html'

It just worked before. I found two ways of working around it.

  • Including $(npm root -g) in $NODE_PATH:
$ echo "# hello" | env NODE_PATH="$(npm root -g):$NODE_PATH" mdast -u mdast-html
  • Specifying full path to a plugin:
$ echo "# hello" | mdast -u "$(npm root -g)/mdast-html"

Both ways are somewhat clumsy. Is there a simpler way of doing that or some relevant configuration option?

@wooorm
Copy link
Member

wooorm commented Sep 6, 2015

Are both mdast and mdast-html global in your example case? I agree that it should work, and it worked before, so this is indeed a bug.

@eush77
Copy link
Contributor Author

eush77 commented Sep 6, 2015

Are both mdast and mdast-html global in your example case?

Yes, they are.

@eush77
Copy link
Contributor Author

eush77 commented Sep 6, 2015

Just noticed that in my case mdast was npmlinked. After removing the link and doing npm i mdast -g global plugins are working again.

Perhaps mdast should also consult npm prefix as a last step if none of other patterns match. What do you think?

@wooorm
Copy link
Member

wooorm commented Sep 6, 2015

Seems to not be very simple: https://github.com/h2non/requireg#differences-with-require

@wooorm
Copy link
Member

wooorm commented Sep 6, 2015

Also: I don’t think many people will link mdast into global, so if for this to work 5+ paths need to be checked, I’d rather decline...

@eush77
Copy link
Contributor Author

eush77 commented Sep 6, 2015

if for this to work 5+ paths need to be checked, I’d rather decline

In reality, there are not so many of them.

From Node docs:

If the NODE_PATH environment variable is set to a colon-delimited list of absolute paths, then io.js will search those paths for modules if they are not found elsewhere.
[…]

Additionally, io.js will search in the following locations:
1: $HOME/.node_modules
2: $HOME/.node_libraries
3: $PREFIX/lib/node

Where $HOME is the user's home directory, and $PREFIX is io.js's configured node_prefix.

These are mostly for historic reasons.

Note that (1) these paths are practically deprecated, (2) none of them is where npm puts modules upon global install, and (3) they are part of the Node core, so default require() algorithm works just fine!

$ echo 'module.exports=function(){return function(ast){ast.children.push({"type":"text","value":"hi there"})}}' >~/.node_libraries/hello.js
$ echo '# hello' |mdast -u hello
# hello

hi there

<stdin>: no issues found

So this is not what this issue is about. It really is about paths used by npm but not native to the Node. From npm docs:

Global installs on Unix systems go to {prefix}/lib/node_modules. Global installs on Windows go to {prefix}/node_modules (that is, no lib folder.)

And so there are only two paths currently missing: {prefix}/lib/node_modules and {prefix}/node_modules.

@wooorm
Copy link
Member

wooorm commented Sep 6, 2015

That makes things better. Know if it's easy to access that prefix reliably, sync?

@eush77
Copy link
Contributor Author

eush77 commented Sep 6, 2015

First of all, npmrc needs to be taken into account, you can use rc for that.

In the past, this one-liner worked for me pretty well:

rc('npm', null, []).prefix || path.resolve(process.execPath, '../..')

Upd: On Windows default prefix should be path.resolve(process.execPath, '..') according to the docs.

@wooorm
Copy link
Member

wooorm commented Sep 6, 2015

Cool! Any reason why the windows case is not handled by your module?

@eush77
Copy link
Contributor Author

eush77 commented Sep 6, 2015

It is handled in 1.0.1 :)

@wooorm
Copy link
Member

wooorm commented Sep 6, 2015

One last Q: I'd like to only load global plug-ins when mdast is global too. That prevents a local mdast from loading a global plugin (by throwing a helpful error). Any idea if and how that would be possible? I have no clue, maybe you do, but this looks important.

@eush77
Copy link
Contributor Author

eush77 commented Sep 6, 2015

One idea is to check if process.argv[1] is {prefix}/bin/mdast ({prefix}/mdast on Windows). That is not very pretty though.

Alternatively, if __dirname is under {prefix}, then mdast is definitely global (but in this case it could be not installed directly by the user, and this test also fails if mdast is linked).

@wooorm wooorm closed this as completed in 5e2d32e Sep 7, 2015
@wooorm
Copy link
Member

wooorm commented Sep 7, 2015

I haven’t checked this on Windows. Are you able to test there?
I opted for checking if process.argv[1] is inside $prefix, which seemed like the simplest check for this.
I tested in npm-run scripts, and with using a linked mdast(1).
I think this should work 😄

@eush77
Copy link
Contributor Author

eush77 commented Sep 7, 2015

I haven’t checked this on Windows. Are you able to test there?

Nope, sorry.

@wooorm
Copy link
Member

wooorm commented Sep 7, 2015

Darn, well, I think this should work. So let’s leave it at that!

@eush77
Copy link
Contributor Author

eush77 commented Sep 7, 2015

👍

@wooorm wooorm added 🦋 type/enhancement This is great to have remark-cli labels Jan 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants