【问题标题】:Tell npm package to import from project root node_modules where it is installed告诉 npm 包从安装它的项目根 node_modules 导入
【发布时间】:2020-12-22 10:19:24
【问题描述】:

我创建了一个 npm 包,我想将它安装到另一个项目中。

所以我进入我项目的根文件夹并执行 npm install /path/to/package/folder

它正确安装它(根据文档创建符号链接),我可以使用它,从它作为经典 node_module 导入。

我的问题是我创建的包内的相对导入。该包依赖于其他库和一个相对的 node_modules 文件夹。

碰巧我的包中的文件是从相对的 node_modules 文件夹而不是项目根 node_modules 中导入的。 如果我在我的根项目文件夹中安装了一个依赖项,我也会在我的包的嵌套 node_modules 中找到它。复制它,我的包中的相对导入从嵌套的包中导入。这给我带来了一些问题。如何正确配置?

这是一个试图澄清的草图:

my_project
    |
    |
     __ node_modules (root)
             |
             |
             my_package (in this case symlink to my package)
             |        |
             |        |
             |        |__node_modules  (nested) <-------
             |        |        |
             |        |        |
             |        |        |__ third_package
             |        |
             |        | 
             |        |__ dist
             |             |
             |             |
             |              __ lib
             |                  |
             |                  |
             |                  |__ index.js
             |
             |
             |___ third_package

index.js 从嵌套的 node_modules 而不是 root 导入 thied_pa​​ckage 文件。 我的包文件是用 babel 编译到 dist/lib 中的

谢谢。

这里是包,my_package的json

{
  "name": "my_package",
  "version": "1.0.0",
  "description": "",
  "main": "lib/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "./node_modules/.bin/babel src --out-dir lib --copy-files",
  },
  "keywords": [
  ],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@babel/cli": "^7.10.5",
    "@babel/core": "^7.11.4",
    "@babel/preset-env": "^7.11.0",
    "@babel/preset-react": "^7.10.4",
    "@fortawesome/fontawesome-free": "^5.14.0",
    "@fortawesome/fontawesome-svg-core": "^1.2.30",
    "@fortawesome/free-brands-svg-icons": "^5.14.0",
    "@fortawesome/free-regular-svg-icons": "^5.14.0",
    "@fortawesome/free-solid-svg-icons": "^5.14.0",
    "@fortawesome/react-fontawesome": "^0.1.11",
    "leaflet": "^1.6.0",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-leaflet": "^2.7.0"
  },
  "peerDependencies": {
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-leaflet": "^2.7.0",
    "leaflet": "^1.6.0"
  },
  "devDependencies": {
    "babel-eslint": "^10.1.0",
  }
}

【问题讨论】:

    标签: node.js npm installation package


    【解决方案1】:

    经过更多研究,

    根据npm installing algorithm,当我安装我的包时,它会将其 package.json 文件中列出的所有依赖项安装到 node_modules 树的最右侧。

    在我的情况下,问题出现在我的包中嵌套的 third_package 和项目根 node_modules 文件夹中的 third_package 具有不同的版本。所以显然两者都安装了。

    确实,节点导入算法,如loading from node_modules folder 中所述,在嵌套更多的 node_modules 文件夹中搜索导入/必需的包,如果没有找到,它会继续到项目的根目录。直到找到为止。

    在我的情况下,我的包导入的第三个包文件位于其本地 node_modules 文件夹中。

    为了规避这种行为,可以使用不同的解决方案:

    1. module alias
    2. webpack resolve
    3. babel plugin resolver

    是否可以探索它们以定义自定义导入行为。

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 2014-02-08
      • 2015-08-29
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多