My package.json
looks like this (name/description/etc. omitted).
我的package.json看起来像这样(省略了name / description / etc.)。
{
"dependencies": {
"express": "3.3.4",
"jade": "0.34.x",
"mongoose": "3.6.x"
},
"devDependencies": {
"vows": "0.7.x"
}
}
I used express
on the repository and ran the auto-generated node app.js
. This worked, but when I used curl http://localhost:port
I got the error "Cannot find module character-parser." I ran npm install character-parser
and then I got "Cannot find module transformers." This happened a few more times, but after I installed all of them the app started working.
我在存储库上使用了express并运行了自动生成的节点app.js.这工作,但当我使用curl http:// localhost:port时,我收到错误“无法找到模块字符解析器”。我运行了npm install character-parser然后我得到了“找不到模块转换器”。这发生了几次,但在我安装了所有这些后,应用程序开始工作了。
I thought that npm install
was supposed to install dependencies recursively. This also worries me because I obviously want the package.json
to be usable when the app is deployed.
我认为npm install应该以递归方式安装依赖项。这也让我担心,因为我显然希望在部署应用程序时可以使用package.json。
3 个解决方案
#1
2
Here's a simple script to collect the dependencies in ./node_modules:
这是一个收集./node_modules中依赖项的简单脚本:
var fs = require("fs");
function main() {
fs.readdir("./node_modules", function (err, dirs) {
if (err) {
console.log(err);
return;
}
dirs.forEach(function(dir){
if (dir.indexOf(".") !== 0) {
var packageJsonFile = "./node_modules/" + dir + "/package.json";
if (fs.existsSync(packageJsonFile)) {
fs.readFile(packageJsonFile, function (err, data) {
if (err) {
console.log(err);
}
else {
var json = JSON.parse(data);
console.log('"'+json.name+'": "' + json.version + '",');
}
});
}
}
});
});
}
For one project I'm working on, the output looks like this:
对于我正在处理的一个项目,输出如下所示:
"progress": "0.1.0",
"request": "2.11.4",
If you remember to remove the comma from the last entry, you can copy and paste the output.
如果您记得从最后一个条目中删除逗号,则可以复制并粘贴输出。
#2
1
I got this exact error while I was npm installing for https://github.com/HenrikJoreteg/humanjs-sample-app/
当我在安装https://github.com/HenrikJoreteg/humanjs-sample-app/时,我得到了这个确切的错误
I'm on a Windows machine, so I suspected that it was an issue with the odd restrictions that Windows has on the number of characters in a file path.
我在Windows机器上,所以我怀疑这是一个问题,Windows对文件路径中的字符数有奇怪的限制。
I resolved this by shorting my base path to a three character folder name in my root (in this case going from C:\projects\humanjs-sample-app to C:\hjs). When I re-ran npm install everything worked. I'm not happy with that resolution. I shouldn't have to worry about my base path name being too long. This is one of the reasons that people often don't do node development on Windows machines.
我通过将我的基本路径缩短为根目录中的三字符文件夹名称来解决这个问题(在这种情况下从C:\ projects \ humanjs-sample-app转到C:\ hjs)。当我重新运行npm安装时,一切正常。我对这个决议不满意。我不应该担心我的基本路径名称太长。这是人们经常不在Windows机器上进行节点开发的原因之一。
An alternate resolution is to develop on Linux or Mac, if you aren't already. That's probably my long term strategy.
另一种解决方案是在Linux或Mac上开发,如果你还没有。这可能是我的长期战略。
#3
-1
When you run npm install <name-of-package>
it will install the package to your node_modules
folder, but will not add it as a dependency. In order to install the package and save it as a dependency in your package.json
, you must use the --save
flag like so:
运行npm install
npm install <name-of-package> --save
npm install
The npm documentation provides further information on additional flags that can be used such as the --save-dev
flag for saving development dependencies and the --save-optional
flag for saving optional dependencies to your package.json
.
npm文档提供了有关可以使用的其他标志的更多信息,例如用于保存开发依赖项的--save-dev标志和用于将可选依赖项保存到package.json的--save-optional标志。
#1
2
Here's a simple script to collect the dependencies in ./node_modules:
这是一个收集./node_modules中依赖项的简单脚本:
var fs = require("fs");
function main() {
fs.readdir("./node_modules", function (err, dirs) {
if (err) {
console.log(err);
return;
}
dirs.forEach(function(dir){
if (dir.indexOf(".") !== 0) {
var packageJsonFile = "./node_modules/" + dir + "/package.json";
if (fs.existsSync(packageJsonFile)) {
fs.readFile(packageJsonFile, function (err, data) {
if (err) {
console.log(err);
}
else {
var json = JSON.parse(data);
console.log('"'+json.name+'": "' + json.version + '",');
}
});
}
}
});
});
}
For one project I'm working on, the output looks like this:
对于我正在处理的一个项目,输出如下所示:
"progress": "0.1.0",
"request": "2.11.4",
If you remember to remove the comma from the last entry, you can copy and paste the output.
如果您记得从最后一个条目中删除逗号,则可以复制并粘贴输出。
#2
1
I got this exact error while I was npm installing for https://github.com/HenrikJoreteg/humanjs-sample-app/
当我在安装https://github.com/HenrikJoreteg/humanjs-sample-app/时,我得到了这个确切的错误
I'm on a Windows machine, so I suspected that it was an issue with the odd restrictions that Windows has on the number of characters in a file path.
我在Windows机器上,所以我怀疑这是一个问题,Windows对文件路径中的字符数有奇怪的限制。
I resolved this by shorting my base path to a three character folder name in my root (in this case going from C:\projects\humanjs-sample-app to C:\hjs). When I re-ran npm install everything worked. I'm not happy with that resolution. I shouldn't have to worry about my base path name being too long. This is one of the reasons that people often don't do node development on Windows machines.
我通过将我的基本路径缩短为根目录中的三字符文件夹名称来解决这个问题(在这种情况下从C:\ projects \ humanjs-sample-app转到C:\ hjs)。当我重新运行npm安装时,一切正常。我对这个决议不满意。我不应该担心我的基本路径名称太长。这是人们经常不在Windows机器上进行节点开发的原因之一。
An alternate resolution is to develop on Linux or Mac, if you aren't already. That's probably my long term strategy.
另一种解决方案是在Linux或Mac上开发,如果你还没有。这可能是我的长期战略。
#3
-1
When you run npm install <name-of-package>
it will install the package to your node_modules
folder, but will not add it as a dependency. In order to install the package and save it as a dependency in your package.json
, you must use the --save
flag like so:
运行npm install
npm install <name-of-package> --save
npm install
The npm documentation provides further information on additional flags that can be used such as the --save-dev
flag for saving development dependencies and the --save-optional
flag for saving optional dependencies to your package.json
.
npm文档提供了有关可以使用的其他标志的更多信息,例如用于保存开发依赖项的--save-dev标志和用于将可选依赖项保存到package.json的--save-optional标志。