blessed icon indicating copy to clipboard operation
blessed copied to clipboard

why are term.js & pty.js "required" but not dependencies?

Open dmison opened this issue 9 years ago • 9 comments

I see this comment stating that it would be bad to add term.js & pty.js to Blessed as dependencies.

Can you explain this because I don't understand why the code has requires for them but they are not dependencies.

For context I am encountering Blessed for the first time after having discovered blessed-react. I realize that the project is unrelated and might be the cause of other issues but I am curious about understanding the initial issue first.

I added them to my own project as dependencies (configuring Webpack to exclude pty.js due to other issues) in order to avoid the webpack failures but am paranoid about what other issues I might be getting myself into.

FYI Webpack throws this error:

ERROR in ./~/blessed/lib/widgets/terminal.js
Module not found: Error: Cannot resolve module 'term.js' in /home/dmison/Dev/BlessedStuff/Demo/node_modules/blessed/lib/widgets
 @ ./~/blessed/lib/widgets/terminal.js 93:14-32

ERROR in ./~/blessed/lib/widgets/terminal.js
Module not found: Error: Cannot resolve module 'pty.js' in /home/dmison/Dev/BlessedStuff/Demo/node_modules/blessed/lib/widgets
 @ ./~/blessed/lib/widgets/terminal.js 218:13-30

dmison avatar Jan 19 '16 00:01 dmison

@dmison as far as I can tell, term.js and pty.js are only needed for the ansi-drawing widgets and the terminal widgets.

gridsystem avatar Feb 05 '16 22:02 gridsystem

I think maybe a better option would be to move terminal widget to an independent module...

piranna avatar May 02 '16 13:05 piranna

The errors described in this issue are currently blocking blessed-dependent libraries from compiling with Webpack. We have two options on how to deal with this two dependencies:

  1. Declare term.js and pty.js as peer dependencies and let to blessed dependents include this packages as their dependencies if they need them.
  2. Follow @piranna idea. Move terminal widget into a separate module.

If we opt by (1), I would be happy to make a PR fixing this issue, but I believe it's up to @chjj to decide the best approach to take :)

derekstavis avatar Aug 19 '16 02:08 derekstavis

I am seeing the same errors too when I use webpack. What's the workaround?

c0debreaker avatar Nov 12 '17 22:11 c0debreaker

As I couldn't get pty.js to work correctly in this scenario, I just commented out the lines in terminal.js that deals with pty. It worked. I also installed term.js. No problems so far.

rob10e avatar Nov 23 '17 22:11 rob10e

As a quick fix, commenting the sections using require('pty') and require('term.js') works.

Though I think the approach to exclude all node_modules is a better solution (As long you don't want everything bundled into one file).

Use:

var webpack = require('webpack');
var path = require('path');
var fs = require('fs');

var nodeModules = {};
fs.readdirSync('node_modules')
  .filter(function(x) {
    return ['.bin'].indexOf(x) === -1;
  })
  .forEach(function(mod) {
    nodeModules[mod] = 'commonjs ' + mod;
  });

module.exports = {
  ...
  target: 'node',
  ...
  externals: nodeModules
}

source: http://jlongster.com/Backend-Apps-with-Webpack--Part-I

RRMoelker avatar Dec 15 '17 19:12 RRMoelker

I set this.term = {}; and this.pty = {}; in blessed/lib/widget.js. Works for now.

I used the wonderful patch-package module to make these changes part of my repo.

stefanpl avatar Feb 15 '20 16:02 stefanpl

These missing files can be ignored by webpack IgnorePlugin to bypass their resolving

const webpackModule = require('webpack');      

config.plugins.push(
  new webpackModule.IgnorePlugin(/pty.js/, /blessed\/lib\/widgets$/)
);

config.plugins.push(
  new webpackModule.IgnorePlugin(/term.js/, /blessed\/lib\/widgets$/)
);

PinKot avatar Nov 16 '20 17:11 PinKot

I found this solution that works for building backend node apps with Webpack. Seems to solve any issues like this where Webpack is not sure what to do with .bin files let nodeModules = {}; fs.readdirSync('node_modules') .filter(function (x) { return ['.bin'].indexOf(x) === -1; }) .forEach(function (mod) { nodeModules[mod] = 'commonjs ' + mod; });

Then in your webpack.config, add this: externals: nodeModules,

seanmac11741 avatar Dec 16 '21 16:12 seanmac11741