【问题标题】:Reference NodeJS libs from locations other than "node_modules" without using relative path从“node_modules”以外的位置引用 NodeJS 库而不使用相对路径
【发布时间】:2017-10-16 17:43:27
【问题描述】:

假设我有 2 个用于 NodeJS 库的文件夹,即“node_modules”和“somename_modules”。后者是存储内部开发的专有库的文件夹。

我知道当我们使用“import”或“require”引用时,默认位置总是指向项目中“node_modules”文件夹所在的路径。

我的问题是,我们如何在“somename_modules”所在的位置添加另一个默认位置,以便当我们在 ES6 源代码中使用“import”或“require”引用时,它总是会检查来自“node_modules”和“somename_modules”?

例子:

import somefunc from 'somelib' //it will search in './node_modules/somelib' and './somename_modules/somelib'

【问题讨论】:

    标签: node.js import npm ecmascript-6 node-modules


    【解决方案1】:

    我不认为,你可以这样做。

    但是您可以在 package.js 中注册您的内部库/模块(指向 somename_modules 文件夹),以便在 npm install 完成时它们可以位于 node_modules 中。然后,import/require语句就可以正常工作了。

    例如 可以说,您在 somename_modules 文件夹中有一个模块名称“mod1”。 IE: "/somename_modules/mod1"

    在 package.json 中添加以下依赖项:

    "dependencies": {
      "mod1": "file:/somename_modules/mod1",
    },
    

    在这种情况下,mod1 需要遵循 npm 结构,比如里面有一个 package.json 文件。

    【讨论】:

    • 我使用了 'file:./my-path' ("." after file:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    相关资源
    最近更新 更多