I'm trying to test d3's d3.csv() csv reading function but still getting an error
{ 'Error: read ECONNRESET': ' at exports._errnoException (util.js:1050:11)' }
My test script test1.js is in react app directory.
admin:reactStockCharts jvr23$ tree -L 1
.
├── BCHARTS-BITSTAMPUSD.csv
├── README.md
├── node_modules
├── package-lock.json
├── package.json
├── public
├── reactStockCharts.sublime-project
├── reactStockCharts.sublime-workspace
├── src
└── test1.js
Testing csv file BCHARTS-BITSTAMPUSD.csv resides in the same directory. The content is as follows:
Date,Open,High,Low,Close,Volume (BTC),Volume (Currency),Weighted Price
2017-07-11,2326.12,2399.0,2242.62,2336.78,16815.9742946,39367980.6825,2341.10614068
2017-07-10,2504.0,2527.88,2261.85,2323.45,17296.3404527,41650843.7716,2408.07261429
2017-07-09,2550.13,2564.65,2500.5,2502.28,4483.14413363,11362427.9698,2534.47750755
2017-07-08,2501.46,2555.0,2462.0,2550.07,5405.89088691,13584489.5168,2512.90486637
2017-07-07,2599.01,2605.0,2475.0,2501.46,9430.6154578,23870564.3605,2531.17778658
test1.js script content:
var d3 = require("d3");
d3.csv("BCHARTS-BITSTAMPUSD.csv", function(err, data) {
console.log(err);
console.log(data[0]);
});
Executing the script then gives
admin:reactStockCharts jvr23$ node test1.js
null
{ 'Error: read ECONNRESET': ' at exports._errnoException (util.js:1050:11)' }
The d3 package was installed sucessfully via $ npm i d3 before.
I was able to fix the issue by replacing
var d3 = require("d3");
d3.csv("BCHARTS-BITSTAMPUSD.csv", function(err, data) {
console.log(err);
console.log(data[0]);
});
with full path specification
var d3 = require("d3");
d3.csv("file:///full/path/to/BCHARTS-BITSTAMPUSD.csv", function(err, data) {
console.log(err);
console.log(data);
});
var d3 = require("d3");
d3.csv("file:///full/path/to/BCHARTS-BITSTAMPUSD.csv", function(err, data) {
console.log(err);
console.log(data);
});
is not a solution for when you want to deploy it online, because the path will change. You should use a relative path, as explained here: https://coderwall.com/p/8nhqeg/relative-paths-from-the-root-in-javascript
You can use path module to get absolute URL
var path = require("path");
var d3 = require("d3");
d3.csv(path.resolve("BCHARTS-BITSTAMPUSD.csv"), function(err, data) {
console.log(err);
console.log(data);
});
Related
I'm trying to create a folder if it does not exist, using NodeJs. But I'm getting Error: ENOENT: no such file or directory, mkdir when trying to create directory error. How can I fix it?
const folderName = `./images/logger`;
try {
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName);
}
} catch (err) {
console.error(err);
}
You need to add {recursive:true} option since you want to create more than one directory:
const folderName = `./images/logger`;
try {
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName,{recursive:true});
}
} catch (err) {
console.error(err);
}
I need to build and deploy a React / NextJS app to a Weblogic J2ee server with a specific context. I have some React experience, but taking my first steps with NextJS.
Currently the build/verification steps are;
Create a vanilla NextJS app
Add a next.config.js with a module.export to change the basepath
module.exports = {
basePath: '/test'
}
Execute npm run dev the application is available on 'http://localhost:3000/test'
Add an export script to the package.json "export": "next build && next export" to support static export
Add the export below to resolve issue 21079
//https://github.com/vercel/next.js/issues/21079
module.exports = {
images: {
loader: "imgix",
path: "",
}
}
Executed npm run export to create a static HTML export. Export is completed successfully to the out folder.
When inspecting the index.html in the out folder, all references to the static content still starts with /_next/static and not with /test/_next/static.
So this can be a misinterpretation of my side, please correct me if i am wrong here.
To be able to test the vanilla app on the J2EE applicationserver it has to be packed into a war file. To accomplish this i added the file warpack/warpack.ts to the project.
const fs = require('fs');
const archiver = require('archiver');
const rimraf = require('rimraf') ;
const distFolder = 'dist' ;
const warFile = distFolder + '/test.war';
const buildFolder = 'out';
const contextRoot = 'test';
// Destroy dist folder
rimraf(distFolder, (error) => {
if (!error) {
// Create dist folder
if (!fs.existsSync(distFolder)){
fs.mkdirSync(distFolder);
}
const output = fs.createWriteStream(warFile);
const archive = archiver('zip', {});
output.on('close', () => {
console.log('war (' + warFile + ') ' + archive.pointer() + ' total bytes');
});
// write archive to output file
archive.pipe(output);
// add build folder to archive
archive.directory(buildFolder,'');
// add weblogic.xml
const weblogicXML = '<?xml version="1.0" encoding="UTF-8"?><weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.2/weblogic-web-app.xsd"><weblogic-version>10.3.6</weblogic-version><context-root>' + contextRoot '</context-root><description>Test NextJS</description></weblogic-web-app>'
archive.append(weblogicXML,{ name: 'WEB-INF/weblogic.xml' });
const manifestMF = 'Manifest-Version: 1.0\nBuild-Tag: 0.0.1-SNAPSHOT\nWeblogic-Application-Version: 0.0.1-SNAPSHOT';
archive.append(manifestMF,{ name: 'META-INF/MANIFEST.MF' });
archive.finalize();
} else {
console.log('Failed to delete "' + distFolder + '" folder.') ;
process.exit(1);
};
});
Installed the required packages for webpack.ts
npm install fs --save-dev
npm install rimraf --save-dev
npm install archiver --save-dev
Added the script "warpack": "next build && next export && node warpack/warpack.ts" to build, export and pack the static app to an war.
After deployment of the war-file the page can be loaded on http://something/test but shows an empty page.
Network development tools indicate that the requests are made to the root of the application server, not to the configured basepath.
GET http://host:8001/static/css/main.09371e9d.chunk.css net::ERR_ABORTED 404 (Not Found)
GET http://host/static/js/2.0850eeb7.chunk.js net::ERR_ABORTED 404 (Not Found)
GET http://host/static/js/main.dc0c945b.chunk.js net::ERR_ABORTED 404 (Not Found)
Too much focus on basePath value instead on correct syntax of next.config.js.
Second module export in next.config.js overwrote first.
Wrong
module.exports = {
basePath: '/test'
}
//https://github.com/vercel/next.js/issues/21079
module.exports = {
images: {
loader: "imgix",
path: "",
}
}
Correct
module.exports = {
basePath: '/test',
assetPrefix: "/test/",
//https://github.com/vercel/next.js/issues/21079
images: {
loader: "imgix",
path: ""
}
}
You can use env check to invoke only for prod environment if you wish to like:
module.exports = {
basePath: "/test"
assetPrefix: process.env.NODE_ENV === "production" ? "/test/" : undefined,
}
I am getting the following error:
internal/modules/cjs/loader.js:797
throw err;
^
Error: Cannot find module 'events/chat'
I have the following folder/file structure:
└─┬ chat
├─+─ events
| +── chat.js
| +── moderation.js
├─ index.js
But if I only call the chat.js or moderation.js file in my server.js file it works just fine.
index.js:
const chatEvent = require('events/chat');
const moderation = require('events/moderation');
module.exports = {
chatEvent,
moderation,
};
I've been playing with some code I found in a book about Node.js. It is a simple app which uploads images.
It shows the EXDEV error (500 Error: EXDEV, rename).
Could someone give me a hint? Here's my code:
exports.submit = function(dir) {
return function(req, res, next) {
var img = req.files.photo.image;
var name = req.body.photo.name || img.name;
var path = join(dir, img.name);
fs.rename(img.path, path, function (err) {
if(err) return next(err);
Photo.create({
name: name,
path: img.name
}, function (err) {
if(err) return next(err);
res.redirect('/');
});
});
};
};
Renaming files cannot be done cross-device. My guess is that your upload directory (which by default is /tmp) is on another partition/drive as your target directory (contained in the dir variable).
Some solutions:
configure the upload directory to be on the same partition/drive as your target directory; this depends on which module you're using to handle file uploads, express.bodyParser (and the module it uses, connect.multipart) accepts an uploadDir option that you can use;
before starting your Node app, set the TMPDIR environment variable to point to a temporary directory on the same partition/drive as your target directory. If you're using a Unix-type OS:
env TMPDIR=/path/to/directory node app.js
instead of setting the environment variable from your shell, set it at the top of your Node app:
process.env.TMPDIR = '/path/to/directory';
instead of renaming, use a module like mv that can work cross-device;
Using Windows XP, I added to app.js:
process.env.TMPDIR = '.'; //new
I don't know how to execute an exe file in node.js. Here is the code I am using. It is not working and doesn't print anything. Is there any possible way to execute an exe file using the command line?
var fun = function() {
console.log("rrrr");
exec('CALL hai.exe', function(err, data) {
console.log(err)
console.log(data.toString());
});
}
fun();
you can try execFile function of child process modules in node.js
Refer:
http://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback
You code should look something like:
var exec = require('child_process').execFile;
var fun =function(){
console.log("fun() start");
exec('HelloJithin.exe', function(err, data) {
console.log(err)
console.log(data.toString());
});
}
fun();
If the exe that you want to execute is in some other directory, and your exe has some dependencies to the folder it resides then, try setting the cwd parameter in options
var exec = require('child_process').execFile;
/**
* Function to execute exe
* #param {string} fileName The name of the executable file to run.
* #param {string[]} params List of string arguments.
* #param {string} path Current working directory of the child process.
*/
function execute(fileName, params, path) {
let promise = new Promise((resolve, reject) => {
exec(fileName, params, { cwd: path }, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
return promise;
}
Docs
If you are using Node Js or any front end framework that supports Node JS (React or Vue)
const { execFile } = require('child_process');
const child = execFile('chrome.exe', [], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
If the .exe file is located somewhere in the machine, replace chrome.exe with the path to the application you want to execute
e.g "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
const child = execFile('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', [], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
Did you ever think about using Batch file in this process? I mean start a .bat file using node.js which will start an .exe file in the same time?
just using answers in the top i got this:
Creating a .bat file in exe file's directory
Type in bat file
START <full file name like E:\\Your folder\\Your file.exe>
Type in your .js file:
const shell = require('shelljs')
shell.exec('E:\\Your folder\\Your bat file.bat')