How to append to a file in Node?

For occasional appends, you can use appendFile, which creates a new file handle each time it’s called: Asynchronously: const fs = require(‘fs’); fs.appendFile(‘message.txt’, ‘data to append’, function (err) { if (err) throw err; console.log(‘Saved!’); }); Synchronously: const fs = require(‘fs’); fs.appendFileSync(‘message.txt’, ‘data to append’); But if you append repeatedly to the same file, it’s much … Read more

How to store Node.js deployment settings/configuration files?

I use a package.json for my packages and a config.js for my configuration, which looks like: var config = {}; config.twitter = {}; config.redis = {}; config.web = {}; config.default_stuff = [‘red’,’green’,’blue’,’apple’,’yellow’,’orange’,’politics’]; config.twitter.user_name = process.env.TWITTER_USER || ‘username’; config.twitter.password= process.env.TWITTER_PASSWORD || ‘password’; config.redis.uri = process.env.DUOSTACK_DB_REDIS; config.redis.host=”hostname”; config.redis.port = 6379; config.web.port = process.env.WEB_PORT || 9980; module.exports = … Read more

Error: request entity too large

I had the same error recently, and all the solutions I’ve found did not work. After some digging, I found that setting app.use(express.bodyParser({limit: ’50mb’})); did set the limit correctly. When adding a console.log(‘Limit file size: ‘+limit); in node_modules/express/node_modules/connect/lib/middleware/json.js:46 and restarting node, I get this output in the console: Limit file size: 1048576 connect.multipart() will be … Read more

How to process POST data in Node.js?

You can use the querystring module: var qs = require(‘querystring’); function (request, response) { if (request.method == ‘POST’) { var body = ”; request.on(‘data’, function (data) { body += data; // Too much POST data, kill the connection! // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB if (body.length > 1e6) … Read more

How do I update Node.js?

I used the following instructions to upgrade from Node.js version 0.10.6 to 0.10.21 on a Mac. Clear NPM’s cache: sudo npm cache clean -f Install a little helper called ‘n’ sudo npm install -g n Install latest stable Node.js version sudo n stable Alternatively pick a specific version and install like this: sudo n 0.8.20 … Read more

Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object

In my case (using Webpack) it was the difference between: import {MyComponent} from ‘../components/xyz.js’; vs import MyComponent from ‘../components/xyz.js’; The second one works while the first is causing the error. Or the opposite.