1

Once you've installed NodeJS, you'll have a executable in your computer named NodeJS which is a shell. I was wondering what can I do with that... here you're able to run JS code as, for example, in the browser's console, great.

Now, is it possible to require modules in that env? Doing so, I'd be able to run some JS code using functions provided by those modules which IMO would be really, really great.

I've tried installing the modules with the -g option (for example npm install -g express); then (in that shell) I want to run require('express') but it doesen't work, it says:

Error: Cannot find module 'express' at Function.Module._resolveFileName ...

Ideas?

1
  • run in cmd: npm ls and npm root and npm list -g show us ;) Commented Jun 15, 2015 at 11:26

3 Answers 3

1

As per issue #5431, looks like the Node.JS REPL doesn't find globally-installed modules and this is expected behaviour.

In the article linked from that issue, it reads:

  1. If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.

This is your case, sou need to install express locally:

$ npm install express
$ node
> var express = require('express');
undefined

Note that you get undefined as a result because of the var statement, but it did work.

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

1 Comment

should not refer to this issue. It s a normal behavior. global is just another context within what the versions are evaluated in a non conflcting way. As of the local module is another context.
1

Pick a directory, then run npm install express --save, then node, and finally var express = require('express');

Comments

0

Answers above are correct. Just want to add another point: going to the folder where the executable is, you'll find there the directory node_modules

nodejs
|-------node_modules
|        |-----------npm
|-------node
|-------more staff

If you paste any folder of a module inside node_modules it'll be able to be required in the nodeJS shell.

Anyway, I prefer the other solution instead to CTR-C + CTRL-V.

1 Comment

yeah it will work fine between two empty ptoject. But if you paste between existing projects, you may face versions misatch. Which is what npm is made to fight. The good way is to use npm. Note --save to help you to do that right. Also if you want to faster your install you can think about using sinopia, it s local cache.

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.