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

标签 node.js

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

我尝试了各种各样的东西。我在我的主目录、我的 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)

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

> var express=require('express');

成功

undefined

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

最佳答案

您的 require 语句运行良好。忽略undefined,这只是 Node REPL。 undefined 解释here ,并查看下面的评论以获取有关此内容的其他 Material 的链接。

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

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

关于node.js - 如何在不全局安装的情况下在 Node REPL 中要求 Node 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18260605/

相关文章:

node.js - 如果对等点仍然使用 Nodejs WebRTC 连接,我们将如何在服务器端知道

javascript - Gulp 构建全栈应用程序的输出/路径/结构策略

javascript - UnhandledPromiseRejectionWarning,但我正在处理错误?

node.js - Express 路由器中间件中的请求正文为空

node.js - 如果用户取消登录,NPM 通行证 linkedin 登录显示 500

javascript - nodemailer 在 Aws Ec2 上的生产环境中导致错误

node.js - Node Jest-必须指定配置路径

node.js - 尝试构建遗留 Node/Angular/ ionic 应用程序,在node-gyp命令上失败

javascript - 如何使用 Node.js 从给定路径中选择多个随机文件?

javascript - TypeError : The first argument must be one of type string, 缓冲区、ArrayBuffer、数组或类似数组的对象。在cryptoJS中接收到类型对象