question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Explicitly require the commands to help JS bundlers find all the code

See original GitHub issue

Node version (or tell us if you’re using electron or some other framework):

v12.8.1

ShellJS version (the most recent version/Github branch you see the bug on):

0.8.3

Operating system:

Ubuntu 18.04.3 LTS

Description of the bug:

First of all, thanks for an amazing library, it has been working great for me in all cases I needed it. Apart from today that I tried bundling one of my Node apps.

It would be great if the calls to requires were explicit and not dynamic as it’s done at https://github.com/shelljs/shelljs/blob/57df38c6ea264493a4219547331f2be049494ed5/shell.js#L24-L26

Reason is that when we want to bundle the whole Node application/script into a single JS file, it’s impossible to get it to bundle correctly.

One example is something I was trying to do today using Shadow-CLJS; more details at https://github.com/thheller/shadow-cljs/issues/290#issuecomment-524626179

I tried almost every popular bundler, including @zeit/ncc, Parcel-bundler, Rollup, and finally Webpack.

To be honest, I believe webpack managed to resolve the dynamic requires but then had other issues with __dirname which I use in my code (whole other different issue).

However, it would be much easier to integrate with these bundlers if the requires calls were explicit. Any specific reason why you go through the indirection of array of strings and a loop to require versus the direct require of the commands (other than saving a few characters)?

Thanks

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
lambrospetroucommented, Aug 30, 2019

Even if we did as you’ve suggested, bundlers probably would not recognize src/exec-child.js as a dependency of src/exec.js, which would break our module.

You are right. It seems that webpack manages to bundle the commands, but not the exec-child.js.

I used the following configuration in webpack.config.js:

const path = require('path');
module.exports = {
  target: 'node',
  node: {
    __dirname: false
  },
  mode: 'development',
  entry: './src/js/index.js',
  output: {
    filename: 'lib.js',
    path: path.resolve(__dirname, 'dist'),
    // library: 'dummyLibName',
    // libraryTarget: 'umd'
  }
};

and with the following source code in src/js/index.js:

const sh = require("shelljs");
console.log(":: SHELLS JS");
sh.echo(`exec: ${sh.exec("ls")}`);
console.log(":: SHELLS JS END ::");

and running the output file in a directory without the node_modules folder throws the following error which indeed proves that exec-child.js is not part of the bundle.

lambros@thinkunix:~/dev/tmp/app1$ node ~/dev/github/create-shadow-cljs-app/dist/lib.js
:: SHELLS JS
internal/modules/cjs/loader.js:716
    throw err;
    ^

Error: Cannot find module '/home/lambros/dev/github/create-shadow-cljs-app/dist/exec-child.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:713:15)
    at Function.Module._load (internal/modules/cjs/loader.js:618:27)
    at Function.Module.runMain (internal/modules/cjs/loader.js:931:10)
    at internal/main/run_main_module.js:17:11 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
webpack:///./node_modules/shelljs/src/common.js?:399
        throw e;
        ^

Error [ShellJSInternalError]: ENOENT: no such file or directory, open '/tmp/shelljs_26a91aa1f2f4ad713060'
    at Object.openSync (fs.js:447:3)
    at Object.readFileSync (fs.js:349:35)
    at execSync (webpack:///./node_modules/shelljs/src/exec.js?:89:17)
    at Object._exec (webpack:///./node_modules/shelljs/src/exec.js?:205:12)
    at Object.eval [as exec] (webpack:///./node_modules/shelljs/src/common.js?:335:23)
    at eval (webpack:///./src/js/index.js?:5:21)
    at Object../src/js/index.js (/home/lambros/dev/github/create-shadow-cljs-app/dist/lib.js:660:1)
    at __webpack_require__ (/home/lambros/dev/github/create-shadow-cljs-app/dist/lib.js:20:30)
    at /home/lambros/dev/github/create-shadow-cljs-app/dist/lib.js:84:18
    at Object.<anonymous> (/home/lambros/dev/github/create-shadow-cljs-app/dist/lib.js:87:10) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/tmp/shelljs_26a91aa1f2f4ad713060',
  name: 'ShellJSInternalError'
}
0reactions
shakedlokitscommented, Apr 15, 2022

Hey @nfischer, can you please help with this? What do I configure typescript with? I’m getting this error, it’s clearly because of how typescript bundles, but how do I configure it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

The JavaScript Modules Handbook – Complete Guide to ES ...
This article will show you all you need to know about ES Modules and Module Bundlers in plain English. Table of Contents 1....
Read more >
JavaScript Bundlers: An in-depth comparative Is Webpack ...
A bundler is a tool that puts together all your JavaScript code and its dependencies and throws a new JavaScript output file with...
Read more >
Bundling and Building with Parcel - Beginner JavaScript
a bundler is able to compress all your code. bundler will minify all the code. The way minification works is longer variables are...
Read more >
How to Bundle JavaScript With Rollup — Step-by-Step Tutorial
This week, we're going to build our first project using Rollup, which is a build tool for bundling JavaScript (and stylesheets, but we'll...
Read more >
CLI's built-in Bundler - Aurelia
au --help shows you all supported flags. Deploying Your App. Run build command au build --env prod , then copy following items to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found