“错误:找不到模块‘less’”Node.js 模块加载首选项/顺序/缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9321061/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:12:29  来源:igfitidea点击:

"Error: Cannot find module 'less'" Node.js module loading preference/order/cache?

node.jsmodulerequirecommonjs

提问by Lance Pollard

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 dependenciesblock). Design.io watches files for changes.

情况是这样的……所以我创建了一个 Node.js 模块,作为一些 Node.js 模板引擎Shift.js的接口。它包含在另一个 Node.js 模块Design.io 中(它被指定为 Design.io 的 package.jsondependencies块)。Design.io 监视文件的更改。

Then you build an app and require('design.io'). You also install, say, Less and Stylus into your project.

然后你构建一个应用程序和require('design.io'). 您还可以将 Less 和 Stylus 安装到您的项目中。

This way, when you save a .lessor .stylfile, a chain of methods are called:

这样,当您保存一个.less.styl文件时,会调用一系列方法:

  1. require("design.io")gets notified of the filesystem event. Design.io then calls
  2. require('shift').render(string, extension, callback). Shift.js then calls
  3. require(moduleFor(extension)) # require("less"). The string is compiled.
  1. require("design.io")得到文件系统事件的通知。Design.io 然后调用
  2. require('shift').render(string, extension, callback). Shift.js 然后调用
  3. require(moduleFor(extension)) # require("less"). 字符串被编译。

In my app (current working directory) I have installed less and stylus:

在我的应用程序(当前工作目录)中,我安装了 less 和 stylus:

npm install less stylus

The problem I'm having is, in step 3which is called from within the Shift.js module, I get errors like this:

我遇到的问题是,在从 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)

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.

目标是像 Shift.js 这样的库不必定义每个依赖项package.json......对于“模板引擎接口”库,这将需要太多应用程序可能永远不会使用的依赖项。

Thanks for the help, hope that was somewhat clear.

感谢您的帮助,希望这有点清楚。

回答by

When you npm install foo, the foomodule gets installed in a directory named node_modulesin the current working directory.

当您npm install foofoo模块安装在node_modules当前工作目录中命名的目录中。

When you install this shiftlibrary, it only looks for modules to require within its ownnode_modulesdirectory, or in one of the global directories require.resolve()searches.

当您安装此shift库时,它只会在其自己的node_modules目录中或在全局目录require.resolve()搜索之一中查找需要的模块。

And that's the simple solution to your problem:

这就是您问题的简单解决方案:

npm install -g less

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.

然后该库对您计算机上的所有 Node.js 代码全局可见,而不仅仅是对当前工作目录中的代码可见。

Alternatively, if you only want shiftto see it, then do something like this:

或者,如果您只想shift查看它,请执行以下操作:

npm install shift
cd node_modules/shift
npm install less