Whenever I run index.js in nodejs I get this error message.
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/sync' is not defined by "exports" in D:Scuola5f_InformatcaTpsitTelegramnode_modulescsv-parsepackage.json
at new NodeError (node:internal/errors:371:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:453:9)
at packageExportsResolve (node:internal/modules/esm/resolve:729:3)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (D:Scuola5f_InformatcaTpsitTelegramnode_modulesprettytableprettytable.js:1:13)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (D:Scuola5f_InformatcaTpsitTelegramindex.js:3:21)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
This error only comes up when I try to add this line of code
const PrettyTable = require('prettytable');
I’m creating a bot, and I would like to send a message in tabular form but when I try to add require(‘pretty table’) if gives me the error above.
Things I’ve tried:
Installed prettytable with npm,
Deleted node_modules and reinstalled every package,
Run npm update
But nothing has worked.
Here is the node_modulescsv-parsepackage.json file for reference
{
"version": "5.0.4",
"name": "csv-parse",
"description": "CSV parsing implementing the Node.js `stream.Transform` API",
"keywords": [
"csv",
"parse",
"parser",
"convert",
"tsv",
"stream"
],
"author": "David Worms <[email protected]> (https://www.adaltas.com)",
"contributors": [
"David Worms <[email protected]> (https://www.adaltas.com)",
"Will White (https://github.com/willwhite)",
"Justin Latimer (https://github.com/justinlatimer)",
"jonseymour (https://github.com/jonseymour)",
"pascalopitz (https://github.com/pascalopitz)",
"Josh Pschorr (https://github.com/jpschorr)",
"Elad Ben-Israel (https://github.com/eladb)",
"Philippe Plantier (https://github.com/phipla)",
"Tim Oxley (https://github.com/timoxley)",
"Damon Oehlman (https://github.com/DamonOehlman)",
"Alexandru Topliceanu (https://github.com/topliceanu)",
"Visup (https://github.com/visup)",
"Edmund von der Burg (https://github.com/evdb)",
"Douglas Christopher Wilson (https://github.com/dougwilson)",
"Joe Eaves (https://github.com/Joeasaurus)",
"Mark Stosberg (https://github.com/markstos)"
],
"exports": {
".": {
"import": "./lib/index.js",
"require": "./dist/cjs/index.cjs"
},
"./sync": {
"import": "./lib/sync.js",
"require": "./dist/cjs/sync.cjs"
},
"./browser/esm": "./dist/esm/index.js",
"./browser/esm/sync": "./dist/esm/sync.js"
},
"devDependencies": {
"@rollup/plugin-eslint": "^8.0.1",
"@rollup/plugin-node-resolve": "^13.0.6",
"@types/mocha": "^9.0.0",
"@types/node": "^16.11.7",
"coffeelint": "^2.1.0",
"coffeescript": "^2.6.1",
"csv-generate": "^4.0.4",
"csv-spectrum": "^1.0.0",
"each": "^1.2.2",
"eslint": "^8.2.0",
"mocha": "^9.1.3",
"pad": "^3.2.0",
"rollup": "^2.60.0",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-globals": "^1.4.0",
"should": "^13.2.3",
"stream-transform": "^3.0.4",
"ts-node": "^10.4.0",
"typescript": "^4.4.4"
},
"files": [
"dist",
"lib"
],
"homepage": "https://csv.js.org/parse/",
"license": "MIT",
"main": "./dist/cjs/index.cjs",
"mocha": {
"inline-diffs": true,
"loader": "./test/loaders/all.mjs",
"recursive": true,
"reporter": "spec",
"require": [
"should"
],
"throw-deprecation": true,
"timeout": 40000
},
"repository": {
"type": "git",
"url": "https://github.com/adaltas/node-csv.git",
"directory": "packages/csv-parse"
},
"scripts": {
"build": "npm run build:rollup && npm run build:ts",
"build:rollup": "npx rollup -c",
"build:ts": "cp lib/*.ts dist/cjs && cp lib/*.ts dist/esm",
"lint": "npm run lint:lib && npm run lint:samples && npm run lint:test",
"lint:lib": "eslint --fix lib/*.js",
"lint:samples": "eslint --fix samples/*.js",
"lint:test": "coffeelint --fix test/*.coffee",
"preversion": "npm run build && git add dist",
"pretest": "npm run build",
"test": "mocha 'test/**/*.{coffee,ts}'",
"test:legacy": "mocha --loader=./test/loaders/legacy/all.mjs 'test/**/*.{coffee,ts}'"
},
"type": "module",
"types": "dist/esm/index.d.ts",
"typesVersions": {
"*": {
".": [
"dist/esm/index.d.ts"
],
"sync": [
"dist/esm/sync.d.ts"
],
"browser/esm": [
"dist/esm/index.d.ts"
],
"browser/esm/sync": [
"dist/esm/sync.d.ts"
]
}
},
"gitHead": "bab8d89a6eb3bc073233e27b7af0a50284b1590f"
}
And here is the prettytableprettytable.js file
var parse = require('csv-parse/lib/sync');
var fs = require('fs');
var PrettyTable = function () {
// Skeleton structure of table with list of column names, row and max width of each column element
this.table = {
'columnNames': [],
'rows': [],
'maxWidth': []
};
this.version = '0.3.1';
};
// Define list of columns for the table
PrettyTable.prototype.fieldNames = function (names) {
this.table.columnNames = names;
for (var i = 0; i < names.length; i++) {
this.table.maxWidth.push(names[i].length);
}
};
// Add a single row to the table
PrettyTable.prototype.addRow = function (row) {
this.table.rows.push(row);
for (var i = 0; i < row.length; i++) {
if (row[i].toString().length > this.table.maxWidth[i]) {
this.table.maxWidth[i] = row[i].toString().length;
}
}
};
// Single function to create table when headers and array of rows passed
PrettyTable.prototype.create = function (headers, rows) {
// Add table headers
this.fieldNames(headers);
// Add rows one by one
for (var i = 0; i < rows.length; i++) {
this.addRow(rows[i]);
}
};
// Convert the table to string
PrettyTable.prototype.toString = function () {
var finalTable = '';
var columnString = '| ';
var rowString = '';
var lengthDifference = '';
// Draw a line based on the max width of each column and return
var drawLine = function (table) {
var finalLine = '+';
for (var i = 0; i < table.maxWidth.length; i++) {
finalLine += Array(table.maxWidth[i] + 3).join('-') + '+';
}
return finalLine;
};
// If no columns present, return empty string
if (this.table.columnNames.length === 0) {
return finalTable;
}
// Create the table header from column list
for (var i = 0; i < this.table.columnNames.length; i++) {
columnString += this.table.columnNames[i];
// Adjust for max width of the column and pad spaces
if (this.table.columnNames[i].length < this.table.maxWidth[i]) {
lengthDifference = this.table.maxWidth[i] - this.table.columnNames[i].length;
columnString += Array(lengthDifference + 1).join(' ');
}
columnString += ' | ';
}
finalTable += drawLine(this.table) + 'n';
finalTable += columnString + 'n';
finalTable += drawLine(this.table) + 'n';
// Construct the table body
for (i = 0; i < this.table.rows.length; i++) {
var tempRowString = '| ';
for (var k = 0; k < this.table.rows[i].length; k++) {
tempRowString += this.table.rows[i][k];
// Adjust max width of each cell and pad spaces as necessary
if (this.table.rows[i][k].toString().length < this.table.maxWidth[k]) {
lengthDifference = this.table.maxWidth[k] - this.table.rows[i][k].toString().length;
tempRowString += Array(lengthDifference + 1).join(' ');
}
tempRowString += ' | ';
}
rowString += tempRowString + 'n';
}
// Remove newline from the end of the table string
rowString = rowString.slice(0, -1);
// Append to the final table string
finalTable += rowString + 'n';
// Draw last line and return
finalTable += drawLine(this.table) + 'n';
return finalTable;
};
// Write the table string to the console
PrettyTable.prototype.print = function () {
console.log(this.toString());
};
// Write the table string to the console as HTML table formats
PrettyTable.prototype.html = function (attributes) {
// If attributes provided, add them as inline properties, else create default table tag
var htmlTable = '';
if (typeof attributes == 'undefined') {
htmlTable = '<table>';
}
else {
var attributeList = [];
for (var key in attributes) {
attributeList.push(key + '='' + attributes[key] + ''');
}
var attributeString = attributeList.join(' ');
htmlTable = '<table ' + attributeString + '>';
}
// Define the table headers in <thead> from table column list
var tableHead = '<thead><tr>';
for (var i = 0; i < this.table.columnNames.length; i++) {
var headerString = '<th>' + this.table.columnNames[i] + '</th>';
tableHead += headerString;
}
tableHead += '</tr></thead>';
htmlTable += tableHead;
// Construct the table body from the array of rows
var tableBody = '<tbody>';
for (i = 0; i < this.table.rows.length; i++) {
var rowData = '<tr>';
for (var k = 0; k < this.table.rows[i].length; k++) {
var cellData = '<td>' + this.table.rows[i][k] + '</td>';
rowData += cellData;
}
rowData += '</tr>';
tableBody += rowData;
}
// Close all tags and return
tableBody += '</tbody>';
htmlTable += tableBody;
htmlTable += '</table>';
return htmlTable;
};
// Create the table from a CSV file
PrettyTable.prototype.csv = function (filename) {
var csvdata = fs.readFileSync(filename, 'utf8');
var records = parse(csvdata);
var lineCounter = 0;
for (var i = 0; i < records.length; i++) {
if (lineCounter === 0) {
this.fieldNames(records[i]);
lineCounter += 1;
}
else {
this.addRow(records[i]);
lineCounter += 1;
}
}
};
// Create the table from a JSON file
PrettyTable.prototype.json = function (filename) {
var rowKeys = '';
var rowVals = '';
var jsondata = JSON.parse(fs.readFileSync(filename, 'utf8'));
for (var i = 0; i < jsondata.length; i++) {
rowKeys = Object.keys(jsondata[i]);
rowVals = [];
for (var k = 0; k < rowKeys.length; k++) {
rowVals.push(jsondata[i][rowKeys[k]]);
}
if (this.table.columnNames.length === 0) {
this.fieldNames(rowKeys);
}
this.addRow(rowVals);
}
};
// Sort the table given a column in ascending or descending order
PrettyTable.prototype.sortTable = function (colname, reverse) {
// Find the index of the column given the name
var colindex = this.table.columnNames.indexOf(colname);
// Comparator method which takes the column index and sort direction
var Comparator = function ( a, b) {
if (typeof reverse === 'boolean' && reverse === true) {
if (a[colindex] < b[colindex]) {
return 1;
}
else if (a[colindex] > b[colindex]) {
return -1;
}
else {
return 0;
}
}
else {
if (a[colindex] < b[colindex]) {
return -1;
}
else if (a[colindex] > b[colindex]) {
return 1;
}
else {
return 0;
}
}
}
// Sort array of table rows
this.table.rows = this.table.rows.sort(Comparator);
};
// Delete a single row from the table given row number
PrettyTable.prototype.deleteRow = function (rownum) {
if (rownum <= this.table.rows.length && rownum > 0) {
this.table.rows.splice(rownum - 1, 1);
}
};
// Clear the contents from the table, but keep columns and structure
PrettyTable.prototype.clearTable = function () {
this.table.rows = [];
};
// Delete the entire table
PrettyTable.prototype.deleteTable = function () {
this.table = {
'columnNames': [],
'rows': [],
'maxWidth': []
};
};
module.exports = PrettyTable;
I’m new to this so I would really appreciate all the help I can get, thank you.
2
Answers
I fixed the problem by modifying exports in node_modulescsv-parsepackage.json. I switched it from "./sync" to "./lib/sync" as one of the comments stated. Thanks for the help.
Encountered a similar issue with the csv-stringify package running node.js v18.15.0 which was resolved using the same fix (replacing "./sync" with "./lib/sync" in csv-stringify’s package.json file).
Submitted a github issue with this info the package’s repository.
— Update —
For my case, this turned out to be due to using an outdated method for importing the synchronous stringify function. As also mentioned in the
updated github issue, the import signature apparently changed between csv-stringify versions 5.5.3 and 6.3.0 from:
to
Updating this import line resolved my issue.