У меня есть модуль npm, и он использует синтаксис ES6, когда я использую npm i myModuleName
У меня ошибка с импортом и экспортом синтаксиса.
Мой код
'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);
}
Я использую эти подмодули в другом проекте с Browserify и yeoman с этой конфигурацией
при запуске приложения с gulp serve
Я получаю эту ошибку
/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'
если я изменяю код в моем подмодуле с ES5, он работает, пример моего подмодуля с 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);
}