简体   繁体   English

使用NodeJS随附的外壳

[英]Using the shell provided with NodeJS

Once you've installed NodeJS, you'll have a executable in your computer named NodeJS which is a shell. 安装NodeJS之后,您的计算机中将有一个名为NodeJS的可执行文件,它是一个外壳程序。 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. 我想知道我该怎么办...在这里,您可以运行JS代码,例如在浏览器的控制台中,很好。

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. 这样做,我将能够使用那些模块提供的功能来运行一些JS代码,而IMO将会非常非常出色。

I've tried installing the modules with the -g option (for example npm install -g express ); 我试过使用-g选项安装模块(例如npm install -g express ); then (in that shell) I want to run require('express') but it doesen't work, it says: 然后(在该shell中)我要运行require('express')但它不起作用,它说:

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

Ideas? 想法?

选择一个目录,然后运行npm install express --save ,然后运行node ,最后运行var express = require('express');

As per issue #5431 , looks like the Node.JS REPL doesn't find globally-installed modules and this is expected behaviour. 根据问题#5431 ,看起来Node.JS REPL找不到全局安装的模块,这是预期的行为。

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. 如果要安装要在程序中使用的东西,请使用require('whatever'),然后在项目的根目录本地安装。

This is your case, sou need to install express locally : 这是您的情况,所以需要在本地安装express

$ 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. 请注意,由于使用了var语句,因此结果undefined ,但它确实起作用。

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 只需添加一点:转到可执行文件所在的文件夹,您将在其中找到目录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. 如果将模块的任何文件夹粘贴到node_modules则在nodeJS shell中将需要它。

Anyway, I prefer the other solution instead to CTR-C + CTRL-V . 无论如何,我更喜欢其他解决方案,而不是CTR-C + CTRL-V

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM