node.js 如何在不全局安装的情况下在 Node REPL 中要求 Node 模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18260605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:19:27  来源:igfitidea点击:

How To Require Node Module In Node REPL Without Installing Globally?

node.js

提问by user2316667

As per the node js instruction manual, it is highly discouraged to install any node module globally.

根据 node js 说明手册,强烈建议不要全局安装任何 node 模块。

I tried a variety of things. I executed the following commands in my home directory, in my git file (with the node_modules) folder, and the actual node_modules folder.

我尝试了各种各样的东西。我在我的主目录、我的 git 文件(带有 node_modules)文件夹和实际的 node_modules 文件夹中执行了以下命令。

> var express=require('express');
undefined
> var express=require('node_modules/express');
Error: Cannot find module 'node_modules/express'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at repl:1:13
    at REPLServer.self.eval (repl.js:110:21)
    at repl.js:249:20
    at REPLServer.self.eval (repl.js:122:7)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)

Note that in the above, I expected them in my git folder (with node_modules). I found it curious that the line

请注意,在上面,我希望它们在我的 git 文件夹中(带有 node_modules)。我觉得奇怪的是这条线

> var express=require('express');

yielded

屈服了

undefined

In the home directory, I just get the same "cannot find module" error.

在主目录中,我只是收到相同的“找不到模块”错误。

采纳答案by Peter Lyons

Your requirestatement is working fine. Ignore the undefined, that's just the node REPL. The undefinedis explained here, and see the comments below for links to additional material about that.

你的require陈述工作正常。忽略undefined,那只是节点 REPL。在这里undefined进行了解释,有关其他材料的链接,请参阅下面的评论。

You can verify with:

您可以通过以下方式验证:

mkdir /tmp/test-repl
cd /tmp/test-repl
npm install express
node
> var express = require('express');
undefined
> express
//long object which is the express module gets printed

回答by user3738936

Depending on the terminal/shell it may want you to specify the current directory. I am using gitbash at the moment.

根据终端/外壳,它可能希望您指定当前目录。我目前正在使用 gitbash。

 _u = require('./node_modules/underscore/underscore');

When I do this the object is returned.

当我这样做时,对象被返回。

node repl require example

节点 repl 需要示例

I am curious if this works for others, it worked for me.

我很好奇这对其他人是否有效,对我有效。

回答by dc5

Printing undefinedis normal behavior for both the browser console and the node repl.

打印undefined是浏览器控制台和节点 repl 的正常行为。

Try typing: express. (tab key) - you should get something like this:

尝试输入:express。(tab 键) - 你应该得到这样的东西:

> var express = require('express');
undefined
> express.
express.__defineGetter__      express.__defineSetter__      express.__lookupGetter__      express.__lookupSetter__      express.constructor           express.hasOwnProperty
express.isPrototypeOf         express.propertyIsEnumerable  express.toLocaleString        express.toString              express.valueOf               

express.apply                 express.arguments             express.bind                  express.call                  express.caller                express.constructor
express.length                express.name                  express.toString              

express.Route                 express.Router                express.application           express.arguments             express.basicAuth             express.bodyParser
express.caller                express.compress              express.cookieParser          express.cookieSession         express.createServer          express.csrf
express.directory             express.errorHandler          express.favicon               express.json                  express.length                express.limit
express.logger                express.methodOverride        express.mime                  express.multipart             express.name                  express.prototype
express.query                 express.request               express.response              express.responseTime          express.session               express.static
express.staticCache           express.timeout               express.urlencoded            express.version               express.vhost