frogLuan
frogLuan

Reputation: 191

Unresolved function or method for pipe()

I'm trying to write a simple file web file server. I'm using PhpStorm.

var http = require('http');

var fs = require('fs');

function send404Request(response) {
    response.writeHead("404", {"Content-Type": "text/html"});
    response.write("404 Page Not Found");
    response.end();
}

function onRequest(request, response) {
    if (request.method === 'GET' && request.url === '/') {
        response.writeHead("200", {"Content-Type": "text/plain"});
        fs.createReadStream("./index.html").pipe(response);
    } else {
        send404Request(response);
    }
}


http.createServer(onRequest).listen(8888);
console.log("file server is now running...");

However, PhpStorm says "unresolved function or method pipe()"

Here is my setting for JavaScript libraries in PhpStorm:

PhpStorm JS Libraries

Any idea where goes wrong?

Upvotes: 0

Views: 3132

Answers (4)

cofirazak
cofirazak

Reputation: 661

In the end of 2019 faced the same issue but in my gulpfile.js.

  • i got the "Unresolved function or method pipe()" and
  • the "Unresolved function or method require()" error messages in PHPStorm.

Downloading the @types/gulp library in PHPStorm resolved both issues. enter image description here

Upvotes: 1

user2576117
user2576117

Reputation: 1

Here is a workaround I am using. Instead of fs.createReadStream("./index.html").pipe(response);

I am using: response.write(fs.readFileSync("./index.html"));

This works well for intelliJ IDEA.

Upvotes: -1

Chun-Hao Huang
Chun-Hao Huang

Reputation: 26

In Settings/Project Settings/JavaScript/Libraries, Download "node-DefinitelyTyped". This work for me. I had the same issue as your.

Upvotes: 1

Sam
Sam

Reputation: 960

Perhaps you need to wait for the stream to open:

var readStream = fs.createReadStream("./index.html");

readStream.on('open', function () {
    readStream.pipe(response);
});

Additionally, add this to catch any errors that might be going on:

readStream.on('error', function(err) {
    console.log(err.message);
});

Upvotes: 0

Related Questions