javascript - 允许用户管理 npm 模块的依赖版本

标签 javascript node.js npm three.js node-modules

我正在构建一个模块,该模块将具有 THREE.js 作为依赖项。我正在尝试找出最好的方法,将“三个”作为依赖项包含在内,并使其可供模块和模块的用户访问(用户将需要访问其项目中的“三个”库)。我想出了两种方法来完成这项工作:

一:

添加三个作为模块的依赖项,在模块中导入整个库并将其添加为全局变量,如下所示:

import * as THREE from 'three'; 
window.THREE = THREE;

这里的缺点是我负责使用 THREE 的版本,并且导入整个库(THREE 最近已切换到模块化构建,因此不再需要包含整个库)。

两个:

添加三个作为模块的依赖项并且要求用户将其添加为其项目的依赖项。这里的想法是,我现在只能从项目中导入我需要的内容:

import { Scene, Camera } from 'three'; 

用户可以导入项目中所需的内容,从而减小文件大小。

这里的问题是,现在安装了三个副本的两个副本,它们可能是不同且不兼容的版本。有什么方法可以将我的模块指向顶级模块,以便他们可以管理已安装的三个版本?

最佳答案

我看到它的完成方式与此类似,但我从未在生产中做过类似的事情,所以我个人不知道它的强度:

main.js:

'use strict';
const three = require('three'),
    mod = require('yourModule');

mod.init(three);
//...

你的Module.js:

'use strict';
let three = {};
function init(t) {
    three = t;
}
//...
modules.export = {
    init: init
    //...
};

或者

main.js:

'use strict';
const three = require('three'),
    mod = require('yourModule')(three);

你的Module.js:

'use strict';
module.exports = (three) => {
    //...
};

关于javascript - 允许用户管理 npm 模块的依赖版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39209180/

相关文章:

javascript - 无法读取未定义的属性

javascript - 路由中的 Ember.js 数据未显示在模板中

node.js - React Native Ios 错误 - ENOENT : no such file or directory, uv_cwd (null))

javascript - 如何在 visual studio code launch.json 文件中运行 npm 脚本

javascript - 从 chromeApp 中的 webview 中删除 CSP header

node.js - 为什么当我更改该文件的数据时,该文件的哈希值保持不变?

javascript - Handlebars : Access has been denied to resolve the property "from" because it is not an "own property" of its parent

使用 Docker 通过 Elastic Beanstalk 部署 Node.js

javascript - Node npm package throw use strict : command not found after publish and install globaly

javascript - 为 JavaScript 中的每个列表项遍历数组