【发布时间】: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_package 文件。
我的包文件是用 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