Nodejs: path must be a string

I tried to implement some code that uses promise , and I copied some source code from Ghost. But when I ran it, I got an error message:

Code:

 var Promise = require('bluebird') var fs = require('fs') var path = require('path') var configPath = path.join(__dirname, '/config-example.js') var configFile function writeConfigFile(){ return new Promise(function(resolve,reject){ var read, write, error; console.log('path->', configPath) read = fs.createReadStream(configPath); read.on('error', function(err){ console.log('Error->', err); reject(err) }) write = fs.createWriteStream(configFile) write.on('error', function(err){ console.log('Error->',err) reject(err) }) write.on('finish', resolve) read.pipe(write) }); } var p = writeConfigFile(); p.then(function(data){ console.log(data) },function(data){ console.log('data->',data) }); 

Error output

 path-> /mnt/share/Learn/config-example.js data-> [TypeError: path must be a string] Error-> { [Error: ENOENT, open '/mnt/share/Learn/config-example.js'] errno: 34, code: 'ENOENT', path: '/mnt/share/Learn/config-example.js' } 
+6
source share
1 answer

Your problem is here:

 write = fs.createWriteStream(configFile) 

configFile is the uninitialized variable here. You can avoid the same problem in the future by using some debugger.

I recommend you node-inspector

+3
source

Source: https://habr.com/ru/post/974528/


All Articles