NPM local package install - node.js

This might well be an ill conceived idea, but I have two react projects in version control, the first, let's call it A, contains a component I want to use in B. B therefore has a dependency on A declared in the package.json for B, as a file: ...path to project A.
The problem is that in order to build project B, the user needs to load both A and B onto their disk, then build A (this is a rollup build) and then build B. Because (I think) B depends on A with a file: reference, when installing A, NPM copies the whole directory including the node_modules folder of A under B. So you end up with B/node_modules/A/node_modules
I think the issue is that we are using the file system location of project A as both the source code location and the registry location if that makes sense. Perhaps we need to publish project A somewhere when it gets built and declare that location to be the dependency?
I hope that makes sense.
I looked at the docs and it seems like node_modules should always be ignored on an install if I am understanding the files section at all.

Common pattern for reusing components between projects is to move the component (or any piece of code that needs to be reused) outside of either A and B and create a standalone npm module containing that code/components.
Publish that module to either public or private npm repository depending on the sensitivity of the code and let both projects A and B install its own instance of that npm module. Otherwise these interdependencies will only cause you headaches in a long run.

Related

Use a custom package to replace a part in dependency chain in a nodejs project

I have a NodeJS project and a part of it has the following structure:
Project —-depends on—> Package A —-depends on-> Package B —-depends on—> Package C
As you can see, package C isn’t being used as the dependency directly. In my case, I would like to use a customized version of package C, but I don’t know what’s the best way to do it as I wish to use the custom package
a) without modifying local node_modules or adding a "patch"
b) hardcoding the lockfile
c) trying the best to maintain the compatability checks between packages used in the project
d) this custom package fork will never get a chance to be merged to main branch of the package C
What could be the best solution in this case? Any suggestions are appreciated 👍👍

Project in Nx monorepo to hold a library's type definition

I would like to add a lib in my Nx monorepo which adds type definition (for Typescript) to an existing pure-javascript library (which lives outside the monorepo).
Did anyone already try something like this? Is it possible without a separate project (have the typings outside the monorepo)? Can you point me some examples/litterature?
I naively thought it would have been enough to name the lib #types/scope__the-lib to make everything works (like npm install #types/scope__the-lib adds a folder in node_modules/#types used by typescript as a location of typings files), but I'm missing some parts.

How to update version of an indirectly referenced node module?

I have a library coming up deep in the node module hierarchy which is causing security issues. I am not directly referencing that module in my package.json. One of the module which I reference is loading up another module and that module is loading this module. So it's the third layer in the dependency tree. I can find out the library dependence tree using npm ls.
I tried updating package.json, but that's not correct I think.
How can I update the version of this particular module without touching the top modules? Should I have to use shrinkwrap?
One (horrible way) (to answer your question directly) you could carefully manage all of those dependencies on your own and build that structure outside of NPM. I hate it. There is a ton of dependency management overhead and no guarantee any of the hand assembled components would work together - so testing overhead too. but in "theory" it could work. FWIW I don't think shrinkwrap helps with sub dependencies at all.
I recommend this course (I understand this isn't what you asked for - but it is the best approach IMO):
Fork/Branch the library and make the change there.
Then issue a pull request (Back to the main branch)
Until it is is merged back in, you cab reference it via the GIT url in your package.json
from: https://docs.npmjs.com/files/package.json
git+ssh://git#github.com:npm/npm.git#v1.0.27
git+ssh://git#github.com:npm/npm#semver:^5.0
git+https://isaacs#github.com/npm/npm.git
git://github.com/npm/npm.git#v1.0.27

Define small named packages in your node application without having to publish them to npm

I'm wondering if there is a way with node to make a directory a containing package, like __init__.py does it in python?
If your package is in a directory named "node_modules", and that directory is locatable somewhere shallower in the filesystem tree that the requiring module, require will find it. So you could do:
myapp.js
node_modules/mylib/package.json
node_modules/mylib/index.js
and myapp.js could just require("mylib"). This also works if that node_modules directory is anywhere shallower in the fs.
That's the simple solution but it isn't commonly done because most people want to .gitignore their node_modules directory and only put transient third party stuff in there.
This problem is extensively and exhaustively discussed in the gist better local require paths. I recommend reading through that for other approaches. Ultimately I think things end up as totally separate projects packaged as separate modules, so move toward that end state as soon as it is justified.

Importing Node.js npm modules (node_modules) & declarations in TypeScript

I think it would be great to directly import modules from node_modules directory without need to manually provide a declaration file for it (let us assume that declaration is provided with module itself). I guess that problem are dependencies that come with declarations (file paths could be resolved relative to the module, but that would cause duplicates and compiler can't handle that).
Currently working with node modules is very inconvenient because simple install from npm repository just isn't enough and we have to manually search for declarations and provide them in our project. Let's say that our project is dependent on 10 node modules (all of them have declarations) and after a year we would like to update them. We would have to manually search for new declarations and let's say that we have around 20 projects like this (it would become a nightmare). Maybe there should be an option to directly import .ts file from node module?
Do you have any suggestions?
This is a re-post from CodePlex to hear your opinions ...
If you use grunt-typescript then I've got a pull request which solves this at least for me. See https://github.com/k-maru/grunt-typescript/pull/36
From the pull request README
Working with modules in node_modules (i.e. npm)
The standard way to use npm packages is to provide a definition file
that specifies the package to the typescript and import the module from there.
///<reference path="path/to/mod.d.ts" />
import mod = module('mod')
The typescript compiler will convert the import to a nodejs require.
var mod = require('mod')
This is pretty unwieldy as you need to know the precise path to the
npm installed package and npm can put the package at pretty much any
level when you are working with multiple levels of dependencies.
With the node_modules option in the grunt config you can just
import a npm package without need to know the exact level where the
package has been installed by npm as long as it is installed locally
and not globally.
To import an npm module in your typescript source do
import npmModule = module('node_modules/npmModule/foo')
Mostly due to a lucky chance this works. Typescript compiler will read
the typescript definition file node_modules/npmModule/foo.d.ts if it
is present at some point on the way towards the root and the resulting
javascript file will contain a require for npmModule/foo if needed.
I don't think that node modules will ever contain built-in typescript support. The language still is a 0.x release and officially described as an alpha version.
Nevertheless there are means to ease the configuration process for typescript. Github already contains huge collections of .d.ts files such as:
https://github.com/borisyankov/DefinitelyTyped
or
https://github.com/soywiz/typescript-node-definitions
You might want to take a look at this tool: https://github.com/Diullei/tsd .
I've never used it but it seems like it's almost what you're looking for.
Moreover I've heard that an official database of .d.ts files is planned. Unfortunately I couldn't find the link but it will probably be some time before this is implemented anyways.

Resources