blessed
blessed copied to clipboard
why are term.js & pty.js "required" but not dependencies?
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 as far as I can tell, term.js and pty.js are only needed for the ansi-drawing widgets and the terminal widgets.
I think maybe a better option would be to move terminal widget to an independent module...
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:
- Declare
term.js
andpty.js
as peer dependencies and let toblessed
dependents include this packages as their dependencies if they need them. - 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 :)
I am seeing the same errors too when I use webpack. What's the workaround?
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.
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
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.
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$/)
);
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,