mxgraph 3.7.3 fails to install using npm
npm install of mxgraph 3.7.3 fails during 'grunt build' in the postinstall. The error output is shown below.
The problem is that in the 3.7.3 release the javascript shipped under javascript/src/js is one big mxClient.js file instead of the individual files used in previous releases. The postinstall script runs grunt build which loads Gruntfile.js and tries to merge all of the javascript files into one file (dist/build.js) with a factory function to load it and export it . Line 14 of Gruntfile.js looks for the mxClient.include pattern in javascript/src/js/mxClient.js but doesn't find any and then tries to call map on undefined because the regular expression does not match anything.
My workaround for now is to stick with version 3.7.2-2 as my code relies on the factory that the grunt build wraps around the mxgraph javscript.
> [email protected] postinstall c:\dev\mx\mxweb\node_modules\mxgraph
> grunt build
Loading "Gruntfile.js" tasks...ERROR
>> TypeError: Cannot read property 'map' of null
Warning: Task "build" not found. Use --force to continue.
Aborted due to warnings.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules\chokidar\node_modules\
fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":
"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm ERR! code ELIFECYCLE
npm ERR! errno 3
npm ERR! [email protected] postinstall: `grunt build`
npm ERR! Exit status 3
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
The same problem in version 3.7.4, when will it be fixed?
Another error is
require(parentFolderName === "node_modules" ? "load-grunt-parent-tasks" : "load-grunt-tasks")(grunt)
this need add 2nd parameter for scope.
It'll be fixed when someone submits a PR to fix it :)
@davidjgraph, thanks for your quick reply.
To change the mxClient.js code more modular, we need get all the needed variables to can be exported (like ES6 module import/export).
We can use the files name as module name before version 3.7.2, but now all the codes turned into one file, it is hard for program to recognise how many and which variables are needed to be exported.
I suggest you give or maintain a list for those first level variables who may be needed for using in the outer.
Then these mxClient.js can be transfer to JS module after exec the script npm install --save mxgraph
I have a another problem about npm install
> [email protected] postinstall /Users/hjzheng/shuyunWorkplace/alidw-fe/node_modules/mxgraph
> grunt build
Running "copy:main" (copy) task
Copied 141 files
Running "concat:dist" (concat) task
Running "webpack:examples" (webpack) task
Warning: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration.output.path: The provided value "javascript/examples/webpack/dist" is not an absolute path! Use --force to continue.
Aborted due to warnings.
I know how to fix it, see the https://github.com/jgraph/mxgraph/issues/65
so far, I just use mxgraph 3.7.1 version for my project, 3.7.2-1, 3.7.2-2, 3.7.3 and 3.7.4 have the same issue that https://github.com/jgraph/mxgraph/issues/83
@davidjgraph I would like to start putting together a pull request to fix this issue. I have a few questions in order to get started. I don't see the necessary files in either this repo or the Javascript repo to build the npm package. Files such as package.json, Gruntfile, etc.. I was thinking the best place to put these would be inside the javascript folder. thoughts? or is there another repo that I can submit a PR for.
Additionally, as mentioned above, in the npm package the mxGraph library has been changed to a single minimized file which breaks the factory object you previously built to export all the classes. Was this done in error or was there a reason. I don't see the previous object __mxOutput in that minimized file.
Thanks for any assistance!
workaround created on my fork.
npm install --save git+https://github.com/NicCOConnor/mxgraph.git
this goes back to the old method of compiling from multiple files to create a dist.js file.
I'll submit a PR once I get some feedback from @davidjgraph on the additional files in the root of the repo.
@NicCOConnor I also could not find the files that create the npm distribution. My workaround was to write a node script that downloads the latest from GitHub and performs the same steps as Gruntfile.js. I commit the output file to source control and copy this file to node_modules in a package.json postinstall step.
I run this whenever I change the tag I want from mxgraph. Since there isn't a package.json file I had to add a browser entry in package.json:
"browser": {
"mxgraph": "./node_modules/mxgraph/mxgraph.js"
},
mx/install.js:
const path = require('path');
const fs = require('fs');
const rimraf = require('rimraf');
const { spawn } = require('child_process');
(function () {
function install() {
const tag = 'v3.7.4';
const uri = 'https://github.com/jgraph/mxgraph.git';
const workdir = path.join(__dirname, 'work');
const distdir = path.join(__dirname, 'dist');
return new Promise((resolve, reject) => {
removedir(distdir)
.then(() => {
return removedir(workdir);
})
.then(() => {
return clone(uri, tag, workdir);
})
.then(() => {
return fixup(workdir, distdir);
})
.then(() => {
resolve();
})
});
}
function removedir(workdir) {
return new Promise((resolve, reject) => {
console.log(`...removing: [${workdir}]`);
rimraf(workdir, () => {
resolve();
});
});
}
function clone(uri, tag, workdir) {
return new Promise((resolve, reject) => {
console.log(`...clone from ${uri}: (${tag}): to ${workdir}`);
const args = ['clone'];
if (tag) {
args.push('-b');
args.push(tag);
}
args.push(uri);
args.push(workdir);
const child = spawn('git', args);
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.on('data', (chunk) => {
process.stdout.write(chunk);
});
child.stderr.on('data', (chunk) => {
process.stderr.write(chunk);
});
child.on('close', (code) => {
resolve();
});
});
}
function fixup(srcdir, distdir) {
const dist_header = '(function (root, factory) {\n' +
"if (typeof define === 'function' && define.amd) {\n" +
'define([], factory);\n' +
"} else if (typeof module === 'object' && module.exports) {\n" +
'module.exports = factory();\n' +
'} else {\n' +
'root.mxgraph = factory();\n' +
'}\n' +
'}(this, function () {\n' +
'return function (opts) {\n' +
// Opts will be passed into this function, expand them out as if
// they were globals so they can get picked up by the logic in
// mxClient.js.
'for (var name in opts) { this[name] = opts[name]; }\n' +
'var __mxOutput = {};\n';
const dist_footer = 'return __mxOutput;\n' +
'};\n' +
'}));';
return new Promise((resolve, reject) => {
console.log(`...fixup from ${srcdir} to ${distdir}`);
const basedir = path.join(srcdir, './javascript/src');
//Find all of the includes in mxClient.js
const mxClientContent = fs.readFileSync(path.join(basedir, './js/mxClient.js'), 'utf8');
let deps = mxClientContent.match(/mxClient\.include\([^"']+["'](.*?)["']/gi).map((str) => {
return "." + str.match(/mxClient\.include\([^"']+["'](.*?)["']/)[1];
});
deps = ["./js/mxClient.js"].concat(deps.slice(0));
let stripIncludeForFile = true; //strip includes from first client (mxClient.js)
let content = dist_header;
for (const file of deps) {
console.log(file);
//Add the file data
const fileData = fs.readFileSync(path.join(basedir, file), 'utf8');
if (stripIncludeForFile) {
content += fixString(stripIncludes(fileData));
stripIncludeForFile = false;
} else {
content += fixString(fileData);
}
//Add export for the file after the data
const exportName = path.basename(file, '.js');
const moduleName = path.basename(file, '.js');
content += makeExport(exportName, moduleName);
//Extra exports needed from some files
if (exportName === 'mxHierarchicalLayout') {
content += makeExport('mxHierarchicalEdgeStyle', 'mxHierarchicalEdgeStyle');
}
}
content += dist_footer;
//Write the output
fs.mkdirSync(distdir);
const destpath = path.join(distdir, 'mxgraph.js');
fs.writeFileSync(destpath, content);
//Done
resolve();
})
}
function makeExport(exportName, moduleName) {
return `\n__mxOutput.${exportName} = typeof ${moduleName} !== 'undefined' ? ${moduleName} : undefined;\n`;
}
function fixString(data) {
return data.replace(/[\r]/g, ''); //Remove carriage returns
}
function stripIncludes(data) {
//In mxClient.js remove the include data from the end of the file.
const index = data.indexOf('PREPROCESSOR-REMOVE-START');
if (index !== -1) {
return data.substring(0, index);
}
return data;
}
//Run
install()
.then(() => {
console.log('complete');
process.exit(0);
})
.catch((err) => {
console.log(`... failed: ${err}`);
process.exit(1);
});
})();
phew, that looks intense. I'm not sure if you can compare my forked changes, but essentially what I did was
- Fork the repo
- copy the
Gruntfile.jsandpackage.jsonfrom the existing npm package (3.7.4) - install the npm package directly from git based off my forked repo
npm install --save git+https://github.com/NicCOConnor/mxgraph.git
it's not the most elegant but it gets me back to where I was with 3.7.2-2
Eventually, I'd like to turn these into proper Typescript / node modules for better Angular consumption, or even Angular components. but I'm not there yet, with regard to the javascript ecosystem.
My goal was to be able to use the newer mxgraph versions after 3.7.2-2 and to be able to use newer versions of npm (e.g. 5.3.0). I wanted to avoid grunt because of the issues that were reported with some of the grunt plugins when using newer versions of npm.
@NicCOConnor Thanks a lot for your fork !
Unfortunately, npm install mxgraph is still not working using version 3.7.5 😢
I get the same error as the original reporter.
@PMudra yes,It is still not working. 😞
This error applies to 3.7.5 too.
@NicCOConnor thanks for your fork. However I am getting issue while installing form your forked even after using the admin privilege in windows console. Error: EPERM: operation not permitted, unlink '***\node_modules\fsevents\nod e_modules\ansi-regex\package.json'
Pls advise if I am missing anything in my setup .. Thanks again for your help...
@sbdevw Try downgrading npm to 5.3 looks like there's an issue with 5.4 on windows. https://github.com/npm/npm/issues/18380
the fork builds successfully with npm 5.4.2 on OSX
Thanks @NicCOConnor downgrading npm to 5.3 worked !!
Can't install on Linux - tried npm 5.3 and 5.4.2, same error as originally reported.
Node Version 8.6.0 (default npm bundled as 5.3.0) on ubuntu works if you use @NicCOConnor 's fork.
FROM ubuntu:17.10
ENV DEBIAN_FRONTEND=noninteractive
#
# includes npm 5.3.0 which is what we want
ENV NODE_VERSION=v8.6.0
RUN apt-get update && apt-get install -y curl git zutils
RUN curl -o /tmp/node.tar.xz https://nodejs.org/dist/${NODE_VERSION}/node-${NODE_VERSION}-linux-x64.tar.xz
RUN cd /usr && tar xJvf /tmp/node.tar.xz --strip 1
# https://github.com/jgraph/mxgraph/issues/83
# specifically a pain.
#
# This fails
# RUN npm install mxgraph
# This fails
# RUN npm install --save git+https://github.com/jgraph/mxgraph.git
# This works
RUN npm install --save git+https://github.com/NicCOConnor/mxgraph.git
Thanks @NicCOConnor , installing the npm package directly from git based off repo npm install --save git+https://github.com/NicCOConnor/mxgraph.git works
This error applies to 3.7.5 too. !!!!!!!!!! help.