I have some javascript utility functions that I would like to be able put in a central folder and reference from different projects.
It seems I cant import functions from outside the src file in of my project.
Do I have to publish an NPM package? Do I have to duplicate the code in each project?
Am using javascript/node + vscode.
thanks
I have some javascript utility functions that I would like to be able put in a central folder and reference from different projects.
It seems I cant import functions from outside the src file in of my project.
Do I have to publish an NPM package? Do I have to duplicate the code in each project?
Am using javascript/node + vscode.
thanks
Share edited Apr 23, 2024 at 16:43 Code learner 3041 silver badge12 bronze badges asked Jan 3, 2021 at 6:55 xabraxabra 2391 gold badge5 silver badges12 bronze badges 4- 3 No, you absolutely do not have to publish to npm to use a library module in multiple projects. Read the documentation of your dependency management tool. If you're not using one, you're not using npm anyway. – Bergi Commented Jan 3, 2021 at 6:58
- Hi Bergi, I do certainly use NPM to install various public NPM packages. And I do have a package.json file as part of my project. I dont have another dependency management tool. What do you remend? – xabra Commented Jan 3, 2021 at 7:04
-
2
If you're using
npm
(which you had not stated in your question), see stackoverflow./q/15806241/1048572 stackoverflow./q/8088795/1048572 stackoverflow./q/14381898/1048572 etc – Bergi Commented Jan 3, 2021 at 7:11 - Thank you, Bergi. Sorry if I wasnt clear. The references you provided stackoverflow./q/15806241/1048572 answered my question. I'll add a little more detail below in an answer – xabra Commented Jan 3, 2021 at 9:21
2 Answers
Reset to default 8To create a local (unpublished) library package
Create a 'my-library' folder. Include source code, exporting any desired functions. Folder must include the 'package.json' file generated by
npm init
cd
into the folder of the project that needs to use your library. Runnpm install --save local/path/to/my-library
.
The--save
will add the package to your dependencies in the project's package.json file, as it does with 3rd party published packages. It will also add a copy of the source code to the node modules folder of the project, as always.import/require the package as you would normally, from any project. For example
import { myFunction } from "my-library"
You can use Lerna for dividing projects into multiple packages locally. It uses the same node_modules and can be deployed to NPM anytime.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744881260a4598853.html
评论列表(0条)