0

I know that the normal way to include a file in nodejs is by using

var myFile= require("./file");.

However, if file contains symbols such as '#' or TRUE or FALSE, nodejs fails with the following error:

SyntaxError: Unexpected token ILLEGAL
    at Module._compile (module.js:437:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)


(function (exports, require, module, __filename, __dirname) { # 

Is there any way I can include this kind of file in nodejs.

NREZ
  • 942
  • 9
  • 13
Anonymous
  • 4,133
  • 10
  • 31
  • 38
  • 1
    `require` is the normal way to *load a JavaScript module*. I don't know what you mean by "include a file". – Quentin Aug 14 '13 at 05:44
  • The file I'm trying to load is a cookie named test.cookie. I need to use this file in a curl command `curl ... -b test.cookie ....` – Anonymous Aug 14 '13 at 05:52
  • Define "include". I can't think of any definition of it that would be a prerequisite for using the path to it in a system call. Why are you thinking about making a system call to curl when Node has HTTP libraries of its own? – Quentin Aug 14 '13 at 05:55
  • The problem is that I see the same error when I use https.request. The problem is in the post header. `var postheaders = { 'Content-Type' : 'application/json', 'Content-Length' : Buffer.byteLength(jsonObject, 'utf8'), 'Cookie' : test.cookie };` – Anonymous Aug 14 '13 at 06:02
  • Since that code doesn't call `require` that doesn't seem very likely. – Quentin Aug 14 '13 at 06:12
  • I think he is trying to send a cookie along with his request on the web, and wants to know where or how to access, in node (or connect/express), the cookie that was sent by the browser request . Or, maybe he wants to set a cookie. In any event some clarification is needed. I am doubting this is about 'files'. See the comment with the `curl` command you will see he is messing with cookies. – Paul Aug 14 '13 at 07:23

1 Answers1

1

As quentin has mentioned, "require" is used to load a module. What you probably need to do is to read the file.

Please refer to the readFile method of fs api. The filecontents is then available in the callback that you specify.

You might also want to refer to the following SO thread

Example of readFile

var fs = require('fs');
var cookie;
fs.readFile("/path/to/data", (error, data) =>{
  //do stuff with data which is of type Buffer
  cookie = data.toString();
})
Community
  • 1
  • 1
vaeroon
  • 11
  • 3
  • On using this API, I see the following error. `if (err) throw err; Error: ENOENT, open 'test.cookie'` – Anonymous Aug 14 '13 at 06:14
  • You have to get the part to your file correct. – Quentin Aug 14 '13 at 06:31
  • Thanks everybody. I figured out what the problem was. I needed to specify the full path of the cookie in my curl command being executed in NodeJS using exec. – Anonymous Aug 14 '13 at 15:01