How to build node.js projects against local versions of the dependencies? - node.js

I've been trying to build a relatively complex node.js project (https://github.com/edrlab/thorium-reader/) against local versions of some dependencies.
I can build and run the project with the non-local dependencies without problems.
I've tried different "routes", such as adding the dependencies using npm install --save <path-to-dependency> or just adding a file:<path-to-dependency> reference to the package.json file. I've checked out the exact versions of each dependency.
npm install doesn't show any errors.
Now when I run npm run start I get type errors that don't quite understand, such as:
Argument of type 'import("~/repositories/thirdparty/thorium/r2-opds-js/dist/es6-es2015/src/opds/opds2/opds2-facet").OPDSFacet'
is not assignable to parameter of type
'import("~/repositories/thirdparty/thorium/thorium-reader/node_modules/r2-opds-js/dist/es6-es2015/src/opds/opds2/opds2-facet").OPDSFacet'.
Types of property 'Links' are incompatible.
Does anyone have a hint for me what I am doing wrong here ?
I'm using node.js version 17.2.0 and npm version 8.2.0.
Best,
N

Ok, after fiddling around for hours I found that using npm link does the trick. Not sure what the differences are, npm install didn't work at all, neither did putting the references in package.json.
That is, go to the dependency's repository, run npm link, build the dependency as a module, THEN go to the main repository and run npm link <dependency> --save.
Now the build process works.
Not sure why there are the two different method, one of which doesn't work a all in this case.

Related

Bundling NPM module for OFFLINE distribution (with all dependencies)

How can I create a tarball package for distribution (with all its dependencies)?
The package needs to contain the actual module + all its dependencies since it will be installed locally/offline due to internet restrictions on the organization.
I tried adding the dependencies to bundledDependencies in package.json then running npm pack. But the generated tarball does not include any dependencies I have listed.
I also tried using a module called npm-pack-all but it does not work as I intended.
Any way I can do this? Preferably without the need for additional npm modules.
Help is really appreciated. Thanks!
npm pack with bundledDependencies worked well for me:
I am using node v10.15.0 and npm v6.4.1
It even bundled the dependencies that my dependencies need/have.

Is npm init needed?

I always thought that you should initialize npm first before installing any packages
npm init --yes
However I found out that I could just go straight to installing packages
npm i example-package
Then the package would be installed and package.json would be created at the same time.
Is there any reason I should be doing npm init first? Is it only required if I want to specify project details?
It is not required. You can install packages without, and everything will work.
npm init can do basically two things:
ask for basic project info to include in packages.json
create a specific type of project (for example React) by using npm init typeofproject
If you just want to use packages and don’t care about naming the project or using a template, just install packages.
npm init is there when you are installing the project very first time.
else you don't need to use npm init for installing any package
Well, kind of a late answer, but as far as I know (correct me if im wrong), one of the features is it gets set up with package.json which includes the dependencies list. That way, NPM can simply install the packages on the list (via the "npm init" if you have a situation that you want to clone the app into another machine), rather than copy pasting the whole project folder.
This isn't a direct answer to the question, but, if sheds some light at some point, why not.

How to create npm/yarn dependency tree from just package.json; without creating node_modules folder

I inherited an application which works fine in node8, but npm install fails in node10, giving an error about fibers package being built using node-gyp
fibers is not a direct dependency of the app, so I want to know which dependency is bringing in fibers as it's dependency.
Unfortunately, npm ls, yarn why only works when node_modules is generated completely through npm install or yarn install.
I did research online but couldn't find a static dependency tree generator just from package.json.
Even though I could just use node8 and run npm install followed by npm ls to figure out whose bringing in fibers; I believe there should be an easier static analysis of package.json.
Is there no way to statically analyze a package.json and create a dependency graph for it in npm/nodejs ?
I come from java and we had maven which can just analyze a file named pom.xml to create a nice graph about whats coming from where.
Execute npm install in the directory and let it fail.
It'll output something like
A log of this can be found at <location>
Open the log file and search for the text saveTree.
Notice a hierarchy of resolved packages
Here you can find the module you're looking for and whose bringing it in.

How to build a node module?

I thought this would be trivial and I realise that it may differ from project to project, but I tried to reproduce the node module build for webdriverio locally and use that in my project.
Here's what I tried:
git clone git#github.com:webdriverio/webdriverio.git
cd webdriverio
git checkout v4.8.0
npm install
npm run build
npm pack
This produces a file named webdriverio-4.8.0.tgz. I change the package.json file of my project to depend on this file rather than webdriverio from npm. Like so:
"webdriverio": "file:../webdriverio/webdriverio-4.8.0.tgz",
Running npm install in my project, updates webdriverio in node_modules as expected, except my version is different from the npm version, despite presumably being based on the same code.
I've never built a node module before, so I appreciate that I might have missed something, but the resources I've found online seem to indicate that the above should be enough. Also if there is a better way to accomplish what I'm trying to accomplish, I'd appreciate the feedback.

Confused in starting a project in node.js with npm install

Hello I am just a noob and still learning. I have already downloaded and tried the chat tutorial of get-started part from socket.io. Now, I am again learning from another source. What's confusing me is that, do I always have to npm install in the beginning of every project after writing the dependencies in the package.json? Or is there any other way? I would be very glad if you could help me understand my confusion. Thank you!
Yes, before running, all dependencies must be installed. So you must run npm install.
When developing, you can use npm install --save <package_name> to install a dependency and automatically add it to package.json.
NPM means Node Package Manager. It is used to manage your dependencies to other node modules dynamically thanks to a configuration file called package.json. This way you can easily define the exact versions you need or a mask in order to always retrieve the stable ones for instance.
The command npm install allows to interpret your configuration file and then download the good versions (and this recursively).

Resources