node.js 尝试捆绑多个 js 文件时,Browserify 找不到模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27652556/
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
Browserify cannot find module when trying to bundle many js files
提问by Ricardo Casta?eda
This is my first day doing node, I'm having some problems trying to bundle some js files.
这是我做 node 的第一天,我在尝试捆绑一些 js 文件时遇到了一些问题。
MyFolder
|-- app (folder)
| |-- Collections (contains: movies.js)
| |-- Models (contains: movie.js)
| |-- node_modules
|-- main.js
|-- node_modules (folder)
|-- static (folder)
This is the content of js files I want to compress into static/bundle.js
这是我想压缩成static/bundle.js的js文件的内容
// app/models/movie.js
var Backbone = require("backbone");
var Movie = Backbone.Model.extend({
defaults: {
title: "default",
year: 0,
description: "empty",
selected: false
}
});
module.exports = Movie;
// app/collections/movies.js
var Backbone = require("backbone");
var Movie = require('models/movie');
var Movies = Backbone.Collection.extend({
model: Movie
});
module.exports = Movies;
When I run browserify -r ./app/main:app > static/bundle.jsthe file bundle.js is created with the scripts from app/main.js. It works as expected.
当我运行browserify -r ./app/main:app > static/bundle.js文件 bundle.js 时,它是使用 app/main.js 中的脚本创建的。它按预期工作。
But when I run browserify -r ./app/collections/movies.js:movies \ -r ./app/models/movie.js:movie > static/bundle.js, it creates an empty bundle.js and shows this:
但是当我运行时browserify -r ./app/collections/movies.js:movies \ -r ./app/models/movie.js:movie > static/bundle.js,它会创建一个空的 bundle.js 并显示:
Error: Cannot find module '/Users/MyFolder/app/models/movie.js:movie' from '/Users/MyFolder'
My folder app/node_modulesis sync with ln -sf ../models .and ln -sf ../collections .
我的文件夹app/node_modules与ln -sf ../models .和同步ln -sf ../collections .
Question 1:Any hint what I'm doing wrong?
Question 2:If static/bundle.jsexists. Does running browserify again overwrites the file or not? On my local tests it doesn't overwrite, so am I supposed to delete this file each time for update?
问题 1:任何提示我做错了什么?
问题2:如果static/bundle.js存在。再次运行 browserify 是否会覆盖文件?在我的本地测试中它不会覆盖,所以我应该每次都删除这个文件进行更新吗?
回答by cheshireoctopus
Might consider adding ./to your path:
可能会考虑添加./到您的路径中:
var Movie = require('./models/movie');
回答by Sumit Kushwaha
For people coming from search engines:
对于来自搜索引擎的人:
It might be that you are using a mac and you have not used proper case while requiring the file.
可能是您使用的是 mac,并且在需要文件时没有使用正确的大小写。
This is equivalent in mac:
这在 mac 中是等价的:
require('./someFile');
require('./somefile');
But not in centOs for example.
但例如在 centOs 中则不然。
回答by Henry
If you use your debugger and step into the requirecall you'll find yourself inside some minified code (typically).
如果您使用调试器并进入require调用,您会发现自己处于一些缩小的代码中(通常)。
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require== ...
Go to your console and inspect t[o][1]
转到您的控制台并检查 t[o][1]
This will show you a list of the correct paths for your modules.
这将向您显示模块的正确路径列表。
Object {
'<module name>' : <id>
...
}
If this becomes too confusing temporarily un-minify the first line in your compiled bundle.jsfile (I use alt-cmd-lin PhpStorm) and try again.
如果这变得太混乱,请暂时取消缩小编译bundle.js文件中的第一行(我alt-cmd-l在 PhpStorm 中使用),然后重试。

