I created an npm package and i would like to install it in another project.
So i go in the root folder of my project and do
npm install /path/to/package/folder
It correctly install it (creating a symlink, according to the documentation) and i can use it, importing from it as a classic node_module.
My problem is with relative import inside package i created. The package has dependencies on other library and a relative node_modules folder.
It happens that files inside of my package import from the relative node_modules folder instead of the project root node_modules one. If i have a dependency installed in my root project folder, i find it also inside the nested node_modules of my package. Duplicating it, and the relative import from my package imports from the nested one. This creates me some problems. How can i correctly configure this?
Here a sketch trying to clarify:
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
imports thied_package files from nested node_modules instead of root.
My package files are compiled with babel into dist/lib
Thank you.
Here package,json of my_package
{
"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",
}
}
After doing more research,
according to npm installing algorithm when I install my package it then will install all the dependencies listed in its package.json file most right to the top of the node_modules tree.
In my case problem arised as third_package nested in my package and third_package in project root node_modules folder hade different versions. So both were obviously installed.
Indeed, the node import algorithm, as described in loading from node_modules folder, search for the imported/required package in the more nested node_modules folder and if not found it proceeds above to the root of the project. Until find it.
In my case third package files imported by my package were found in its local node_modules folder.
To circumvent this behaviour different solution are available:
Is it possible to explore them to define a custom import behaviour.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With