3

I can use querystring in node 4.2.6 from a script, but I can from a node prompt. Here's some proof.

I have the following script:

$ cat test.js
console.log(process.versions)
console.log(querystring)

And I get an error when I run it:

$ node test.js
{ http_parser: '2.5.0',
  node: '4.2.6',
  v8: '4.5.103.35',
  uv: '1.8.0',
  zlib: '1.2.8',
  ares: '1.10.1-DEV',
  icu: '56.1',
  modules: '46',
  openssl: '1.0.2e' }
/path/to/file/test.js:2
console.log(querystring)
            ^
ReferenceError: querystring is not defined
    at Object.<anonymous> (/path/to/file/test.js:2:13)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:442:10)
    at startup (node.js:136:18)
    at node.js:966:3

But if I go into node on the command line, I don't get the error.

$ node
> console.log(process.versions)
{ http_parser: '2.5.0',
  node: '4.2.6',
  v8: '4.5.103.35',
  uv: '1.8.0',
  zlib: '1.2.8',
  ares: '1.10.1-DEV',
  icu: '56.1',
  modules: '46',
  openssl: '1.0.2e' }
undefined
> console.log(querystring)
{ unescapeBuffer: [Function],
  unescape: [Function],
  escape: [Function],
  encode: [Function],
  stringify: [Function],
  decode: [Function],
  parse: [Function] }
undefined

The console.log() is just for proof--I can't use querystring at all in a script. What could be wrong?

0

2 Answers 2

15

The node REPL (what you get when just typing the executable name (node)) automatically loads built-in modules as globals as they are accessed by name as a convenience. This is mentioned in the documentation here.

For scripts that you load with node foo.js you will need to require() modules manually to pull in what you actually need. For the querystring module, you would just need to do:

var querystring = require('querystring');

There is no need to npm install querystring as the module is built-in to node.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the reference to the docs that explains the pain point
0

This worked for me:

$ npm install querystring

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.