我最近安装了 Node.js,并被告知 Express 是进行路由和设置 Web 应用程序开发的最佳方式。
我安装了最新版本的节点,它显然与最新的 Express 不兼容。
我查找了一下,发现nave...和RVM一样,nave允许你切换node的版本。所以我运行了 nave.sh install 0.4.11...
它成功运行,我能够运行。
npm install express -g
我想,应该在全球范围内安装express。所以我运行:
express testapp
这创建了
create : testapp
create : testapp/package.json
create : testapp/app.js
create : testapp/public/stylesheets
create : testapp/public/stylesheets/style.css
create : testapp/public/images
create : testapp/public/javascripts
create : testapp/views
create : testapp/views/layout.jade
create : testapp/views/index.jade
然后我
cd testapp/
node app.js
我得到
错误:找不到模块“express”
这是常见行为吗?
由于express位于packages.json中,如果我运行npm install -d
,它将在我的应用程序中创建一个node_modules目录,而不仅仅是指向我的节点路径中的node_modules的符号链接。
I recently installed node.js and was told that express was the way to go for routing and getting set up with web application development.
I installed the latest version of node which apparently is incompatible with the latest express.
I looked up and found nave... Like RVM, nave allows you to switch versions of node. So I ran nave.sh install 0.4.11...
That worked successfully and I was able to run.
npm install express -g
This I thought, should install express globally. So I run:
express testapp
which creates
create : testapp
create : testapp/package.json
create : testapp/app.js
create : testapp/public/stylesheets
create : testapp/public/stylesheets/style.css
create : testapp/public/images
create : testapp/public/javascripts
create : testapp/views
create : testapp/views/layout.jade
create : testapp/views/index.jade
Then I
cd testapp/
node app.js
I get
Error: Cannot find module 'express'
Is this usual behavior?
Since express is in packages.json, if I run npm install -d
, it will create a node_modules directory in my application and not just symlink to the node_modules in my node path.
发布评论
评论(2)
总而言之,是的,这是通常的行为。
当您使用带有 -g 选项的 NPM 安装软件包时,它会全局安装它,这会做一些很好的事情,例如将可执行文件放在您的路径上(即您使用的 Express 脚本)
,但是,它不会将这些软件包放在任何地方该节点可以找到它们。
要安装它以便节点可以找到该包,您还必须
在本地进行安装(安装到应用程序目录根目录中的node_modules文件夹)。
这主要是为了避免任何依赖关系冲突,虽然看起来很愚蠢,但实际上非常有用。
如果您有真正的理由想要使用全局安装(例如您有许多应用程序,您希望确保始终共享相同的版本),您可以使用 npm link 命令。
有关 NPM 以及全局与本地的详细概述,请参阅 此博客文章。
In a word, yes, this is the usual behavior.
When you install packages using NPM with -g option, it installs it globally, which does nice things like putting executeables on your path (i.e. the express script you used)
However, it does NOT put those packages anywhere that node can find them.
To install it so node can find the package, you must also do
which installs locally (to the node_modules folder in the root of your application dir).
This is primarily to avoid any dependencies conflicts, and though it may seem silly, it is in fact really useful.
If you have some real reason to want to use your global install (say for example you have many applications that you want to make sure always share the same version) you can use the npm link command.
For a good rundown of NPM and global vs local see this blog post.
如果您使用的是 Windows,请将位置添加到您的路径中。
将:
IMarek
更改为您的用户名。If you are on Windows, add location to your path.
Change:
IMarek
to your user name.