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

using shelljs in a JS Module is broken. #1071

Open
mathertel opened this issue Jan 25, 2022 · 6 comments
Open

using shelljs in a JS Module is broken. #1071

mathertel opened this issue Jan 25, 2022 · 6 comments

Comments

@mathertel
Copy link

nodejs v16.13.2, [email protected], windows

import  * as shell from 'shelljs'

Using shelljs in a JS Module (ES Module) results in a imported shell object with missing functions.

Anyone has found a solution to use shelljs in a ES Module based project ?

@nfischer
Copy link
Member

I think you want import shell from 'shelljs';. Here's an example (which is transpiled by babel, but hopefully still works with native ES module support):

https://github.com/shelljs/shx/blob/61aca968cd7afc712ca61a4fc4ec3201e3770dc7/src/shx.js#L1

Let me know if that works for you.

@nfischer nfischer added the question Question from a user (which may not require code/documentation changes to the project) label Jan 26, 2022
@nfischer
Copy link
Member

Just tried this out and it seems to work for me:

// index.mjs
import shell from 'shelljs';

console.log(shell.ls().stdout);

I can run this with:

$ node --version
v16.13.1

$ node index.mjs
index.mjs
node_modules
package-lock.json
package.json

@mathertel
Copy link
Author

Also have some findings:

It depends on the import statement.
I used import * as shell from 'shelljs' but the returned shell object contains only with the members config, default, env, error, exit, ShellString.
These are are added explicitly to exports.

After adding a line in shell.js:

exports.ls = exports.ls;

ls is available on the shell object.

Seems that the way you // Load all default commands creates this phenomena.

@nfischer nfischer reopened this Feb 14, 2022
@nfischer nfischer added feature es6 and removed question Question from a user (which may not require code/documentation changes to the project) labels Feb 14, 2022
nfischer added a commit that referenced this issue Feb 14, 2022
No change to logic. This documents that we support importing via
`import shell from 'shelljs'`.

Related to issue #1071, although some more investigation is required to
understand why the other syntax doesn't work.
nfischer added a commit that referenced this issue Feb 14, 2022
No change to logic. This documents that we support importing via
`import shell from 'shelljs'`.

Related to issue #1071, although some more investigation is required to
understand why the other syntax doesn't work.
@vold4
Copy link

vold4 commented Feb 23, 2022

Hi please help does anyone have an idea how to add an import module for babel / webpack in electron. In the electron, the module can be added functionally in the following line. But I don't know how to add "require.main.require" to the import.

var shell = require.main.require ('shelljs');

Thank you for every thought, I appreciate your work!

@sovetski
Copy link

sovetski commented Mar 3, 2024

Any update? Even if import shell from "shelljs"; works, why just don't support es6?

@nfischer
Copy link
Member

nfischer commented Mar 4, 2024

I'm not sure what you're requesting. I think import shell from 'shelljs' works and that's what we've documented. If that's not what you're looking for, can you explain how we can make this better?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants