Reputation: 1645
I have a module npm and it is using the ES6 syntax, when I using the npm i myModuleName
I have an error with syntax import and export.
My code
'use strict';
import merge from 'ngraph.merge'
import createGraph from 'ngraph.graph'
import fetch from './lib/loader'
module.exports = async function convertPrecompute(graph, layout, settings){
settings = merge(settings,{
positionsPos: 'data/positions.bin',
linksPos: 'data/links.bin',
labelsPos: 'data/labels.json',
});
await Promise.all([
fetch(settings.positionsPos, { responseType: 'arraybuffer' }).then(toInt32Array),
fetch(settings.linksPos, { responseType: 'arraybuffer' }).then(toInt32Array),
fetch(settings.labelsPos).then(toJson)
]).then(loadLayout);
function loadLayout(data){
let positions = data[0];
let links = data[1];
let labels = data[2];
graph = initGraphFromLinksAndLabels(links, labels);
labels.forEach(function (label, index) {
var nodeCount = index * 3;
var x = positions[nodeCount + 0];
var y = positions[nodeCount + 1];
var z = positions[nodeCount + 2];
layout.setNodePosition(label, x, y, z);
});
}
function initGraphFromLinksAndLabels(links, labels) {
let srcIndex;
let g = createGraph({ uniqueLinkId: false });
labels.forEach(label => g.addNode(label));
links.forEach(processLink);
return g;
function processLink(link) {
if (link < 0) {
srcIndex = -link - 1;
} else {
var toNode = link - 1;
var fromId = labels[srcIndex];
var toId = labels[toNode];
graph.addLink(fromId, toId);
}
}
}
}
function toInt32Array(oReq) {
return new Int32Array(oReq.response);
}
function toJson(oReq) {
return JSON.parse(oReq.responseText);
}
I'm using this submodules in another project with Browserify and yeoman with this configuration
when running the app with the gulp serve
I'm getting this error
/home/vincenzo/GitLab/spycblock-ngraph/node_modules/ngraph.fromprecompute/index.js:12
import merge from 'ngraph.merge'
^
ParseError: 'import' and 'export' may appear only with 'sourceType: module'
if I change the code in the my submodule with ES5 it work, an example of my submodule with ES5
'use strict';
let merge = require('ngraph.merge');
let createGraph = require('ngraph.graph')
let fetch = require('./lib/loader')
//Startup https://dev.to/therealdanvega/creating-your-first-npm-package-2ehf
module.exports = async function convertPrecompute(graph, layout, settings){
settings = merge(settings,{
positionsPos: 'data/positions.bin',
linksPos: 'data/links.bin',
labelsPos: 'data/labels.json',
});
await Promise.all([
fetch(settings.positionsPos, { responseType: 'arraybuffer' }).then(toInt32Array),
fetch(settings.linksPos, { responseType: 'arraybuffer' }).then(toInt32Array),
fetch(settings.labelsPos).then(toJson)
]).then(loadLayout);
function loadLayout(data){
let positions = data[0];
let links = data[1];
let labels = data[2];
graph = initGraphFromLinksAndLabels(links, labels);
labels.forEach(function (label, index) {
var nodeCount = index * 3;
var x = positions[nodeCount + 0];
var y = positions[nodeCount + 1];
var z = positions[nodeCount + 2];
layout.setNodePosition(label, x, y, z);
});
}
function initGraphFromLinksAndLabels(links, labels) {
let srcIndex;
let g = createGraph({ uniqueLinkId: false });
labels.forEach(label => g.addNode(label));
links.forEach(processLink);
return g;
function processLink(link) {
if (link < 0) {
srcIndex = -link - 1;
} else {
var toNode = link - 1;
var fromId = labels[srcIndex];
var toId = labels[toNode];
graph.addLink(fromId, toId);
}
}
}
}
function toInt32Array(oReq) {
return new Int32Array(oReq.response);
}
function toJson(oReq) {
return JSON.parse(oReq.responseText);
}
Upvotes: 0
Views: 122