Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs: path must be a string

Tags:

node.js

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:

The 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' }
like image 340
Ryan Yiada Avatar asked Aug 29 '14 03:08

Ryan Yiada


People also ask

How to give path in Node js?

The best way to work with paths - use path module (API): Or with path module: var path = require('path'); path. join('E:','Thevan','Some File.

What is path module in Nodejs?

Node. js provides you with the path module that allows you to interact with file paths easily. The path module has many useful properties and methods to access and manipulate paths in the file system. The path is a core module in Node.

What does path resolve do?

The path. resolve() method is used to resolve a sequence of path-segments to an absolute path. It works by processing the sequence of paths from right to left, prepending each of the paths until the absolute path is created. The resulting path is normalized and trailing slashes are removed as required.

Is Node js a server?

Node. js is an open source server environment. Node. js uses JavaScript on the server.


1 Answers

Your problem is here:

write = fs.createWriteStream(configFile)

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

I recommend you node-inspector

like image 149
3y3 Avatar answered Oct 05 '22 22:10

3y3