一旦安装了 NodeJS,您的计算机中就会有一个名为 NodeJS 的可执行文件,它是一个 shell。我想知道我能用它做什么......在这里你可以运行 JS 代码,例如,在浏览器的控制台中,太棒了。
现在,是否可以在该环境中要求模块?这样做,我将能够使用这些模块提供的函数运行一些 JS 代码,IMO 真的非常棒。
我已经尝试使用 -g
选项安装模块(例如 npm install -g express
);然后(在那个 shell 中)我想运行 require('express')
但它不起作用,它说:
错误:找不到模块“express”
在 Function.Module._resolveFileName ...
想法?
最佳答案
根据 issue #5431 ,看起来 Node.JS REPL 没有找到全局安装的模块,这是预期的行为。
在article linked from that issue ,内容如下:
- 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.
这是你的情况,你需要安装 express
本地:
$ npm install express
$ node
> var express = require('express');
undefined
请注意,由于 var
语句,您得到的结果是 undefined
,但它确实有效。
关于javascript - 使用 NodeJS 提供的 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30843806/