“错误:找不到模块‘less’” Node.js 模块加载首选项/顺序/缓存?
情况是这样的……所以我创建了一个 Node.js 模块,充当一些 Node.js 模板引擎的接口, Shift.js。它包含在另一个 Node.js 模块 Design.io 中(它是指定的 Design.io 的包)。 json dependency
块)。 Design.io 监视文件的更改。
然后构建一个应用并 require('design.io')
。您还可以将 Less 和 Stylus 安装到您的项目中。
这样,当您保存 .less
或 .styl
文件时,会调用一系列方法:
require("design.io")
收到文件系统事件的通知。 Design.io 然后调用- require('shift').render(string, extension, callback)。然后 Shift.js 调用
require(moduleFor(extension)) # require("less")
。字符串已编译。
在我的应用程序(当前工作目录)中,我安装了 less 和 stylus:
npm install less stylus
我遇到的问题是,在从 Shift.js 模块中调用的第 3 步中,我收到如下错误:
Error: Cannot find module 'less'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Less.engine (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift/less.js:6:14)
at Less.render (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift/less.js:18:21)
at /Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift.js:69:23
at /Users/viatropos/Documents/git/plugins/design.io/node_modules/async/lib/async.js:118:13
at Object.forEachSeries (/Users/viatropos/Documents/git/plugins/design.io/node_modules/async/lib/async.js:134:9)
at Object.render (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift.js:78:31)
我的问题是,为什么会发生这种情况?我认为你可以动态地从模块中获取库,只要它们安装在某个地方......我错过了什么?
目标是像 Shift.js 这样的库不必在 package.json 中定义每个依赖项……对于“模板引擎的接口”库,这将需要太多的依赖项应用程序可能永远不会使用。
感谢您的帮助,希望这有点清楚。
Here's the situation… So I've created a Node.js module that acts as an interface to some Node.js template engines, Shift.js. That is included inside another Node.js module, Design.io (it's specified Design.io's package.json dependencies
block). Design.io watches files for changes.
Then you build an app and require('design.io')
. You also install, say, Less and Stylus into your project.
This way, when you save a .less
or .styl
file, a chain of methods are called:
require("design.io")
gets notified of the filesystem event. Design.io then callsrequire('shift').render(string, extension, callback)
. Shift.js then callsrequire(moduleFor(extension)) # require("less")
. The string is compiled.
In my app (current working directory) I have installed less and stylus:
npm install less stylus
The problem I'm having is, in step 3 which is called from within the Shift.js module, I get errors like this:
Error: Cannot find module 'less'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Less.engine (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift/less.js:6:14)
at Less.render (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift/less.js:18:21)
at /Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift.js:69:23
at /Users/viatropos/Documents/git/plugins/design.io/node_modules/async/lib/async.js:118:13
at Object.forEachSeries (/Users/viatropos/Documents/git/plugins/design.io/node_modules/async/lib/async.js:134:9)
at Object.render (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift.js:78:31)
My question is, why is this happening? I thought that you could dynamically require libraries from a module as long as they were installed somewhere… What am I missing?
The goal would be that libraries like Shift.js wouldn't have to define every single dependency in package.json
… For an "interface to the template engines" library, that would require too many dependencies that the app would probably never be using.
Thanks for the help, hope that was somewhat clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您
npm install foo
时,foo
模块将安装在当前工作目录中名为node_modules
的目录中。当您安装此
shift
库时,它仅在自己的node_modules
目录或全局目录之一中查找所需的模块require.resolve()
搜索。这就是解决问题的简单方法:
然后该库对计算机上的所有 Node.js 代码全局可见,而不仅仅是当前工作目录中的代码可见。
或者,如果您只想
shift
看到它,请执行以下操作:When you
npm install foo
, thefoo
module gets installed in a directory namednode_modules
in the current working directory.When you install this
shift
library, it only looks for modules to require within its ownnode_modules
directory, or in one of the global directoriesrequire.resolve()
searches.And that's the simple solution to your problem:
And then the library is globally visible to all Node.js code on your computer, rather than only being visible to code in the current working directory.
Alternatively, if you only want
shift
to see it, then do something like this: