Npm pack to include local modules - node.js

I have some local modules that I want to include in my package to be sent to a server. Since these local packages are not on the npm registry, they need to be part of the package.
I have tried some ways to get the node_modules folder included in the package using npm pack, however it seems this is not possible at all?
Secondly I tried to list the local modules in the bundledDepencies in the package.json file and use npm pack, however this also does not include the local modules, no matter what;
{
"name": "dev",
"version": "1.0.0",
"main": "main.js",
"dependencies": {
"local-module": "file:../../local-module"
},
"bundledDependencies": [
"local-module"
]
}
How can I get these local modules included in the dev package?
The local module does contain dependencies itself, not sure if that makes things more complicated?

I had a similar issue a while back, and a good and simple solution, is just to put your local modules into private git repos (which are now free on GitHub, thanks Microsoft 🙌)
Then, in your package.json, just add:
"dependencies" : {
"name1" : "git://github.com/user/project.git#commit-ish",
"name2" : "git://github.com/user/project.git#commit-ish"
}
Source, npm docs

I believe Alicia's approach is the cleanest one. However for someone that runs into the same issue as I did, whereby a server requires a tarball, but does not have git installed, I added my local packages to a seperate folder in the project called repo and referenced them in the package.json as;
"dependencies": {
"my-local-package": "file:./repo/my-local-package"
}

There's also yalc, which creates a local store for local packages. After adding a local package to the store, you can use yalc to copy the local package into another package. You can also use it to do updates of local packages.

Related

How can I make package-lock, integrity SHAs, and locally built modules as tgz all work together?

Consider the following scenario:
We have some modules that we install as tgz.
In package.json, they are referred to as:
"somePackage": "file:../modules/somePackage.tgz",
We'd like to use package-lock, and so we create package-lock files.
The package-lock file has:
"node_modules/somePackage": {
"version": "1.0.97",
"resolved": "file:../modules/somePackage.tgz",
"integrity": "sha512-PXp1Q75feN1P+PHhqZmPF/J/5q5oVnALOVtdDq69Bn29sqFdwHkNL1k4DjOBYbh1C8y8beETOc7T77hMUhrWmQ==",
"dev": true,
The problem has to do with the integrity checksum. When rebuilding the package, and then running npm install, it fails integrity check.
I am not sure what the best way to go about this would be. Ultimately it would be nice to have a way to ignore checksum for tarballs like that, but I am not sure there is a way.
Currently using Npm 7 and Node 14.
Though I disagree with the fact that npm checks integrity checksum of locally built tarballs, the real issue is that locally you shouldn't need to build tarballs. Local builds should rely on workspaces.
The example in my question should be setup like this:
Remove "somePackage": "file:../modules/somePackage.tgz",
Setup workspaces section that looks like this:
"workspaces" : [
"../modules/somePackage"
]

How to specify an npm workspace as a dependency

I can’t find the answer to this seemingly obvious question anywhere.
I have some npm workspaces setup in a project. It’s basically a main project with several workspaces within it. They are in a top level folder called “packages”, each in its own folder with its own package.json.
I need to add the workspaces as dependencies of the main project. I’ve added them to package.json of the main project but npm keeps trying to install them from npm.org, and so it fails.
I’m using this syntax:
“workspaces”:[
“packages/*”
],
“dependencies”: {
“workspace-a”: “^0.0.1”
…
}
How do I specify the workspaces as dependencies in package.json?
[Update: the eventual way the main project is used is that it is a dependency of a totally separate project]
I am using explicit workspaces attribute, in the root package.json
"workspaces": [
"foo-bar",
"another-package"
]
Dependency from another workspace package is referenced using file: prefix.
"name": "#project/foo-bar",
"dependencies": {
"#project/another-package": "file:another-package"
},
were #project is the package scope, name it according to your needs.

Project with npm workspaces doesn’t work as dependency

I have a library that uses workspaces to house a bunch of plugin modules inside the library repo.
The dir structures looks something like:
package.json
src/app
src/plugins/package-a
src/plugins/package-a/package.json
The workspace modules are specified in the library’s package.json using local paths:
"workspaces": [
"src/plugins/package-a",
…
],
"dependencies": {
"package-a": "src/plugins/package-a"
…
}
It appears to work fine, when I npm install everything installs.
However since this is a library, the way it gets used is by being a dependency of other projects.
When I setup a separate project and add the library as a dependency, and npm install the separate project it throws an error. The error says the plugin doesn’t have a package.json, but it clearly does have a package.json.
Why can’t the project be used inside another project as a dependency?

Can I override the url npm pulls a package from?

I have a legacy project which depends on a specific version of an npm package which through a dependency chain depends on a specific npm package of phantomjs, the binaries of which are no longer online at the registered location. This breaks the project's build.
Updating package versions is not an option.
I can fool npm by manually fetching the required version of phantomjs and dropping it into my system's download folder, but this is a workaround, I want a cleaner fix, ideally something that sits in a config script.
Can I, in my project's package.json or .npmrc files, force npm to use another url for a package # some version, every time "npm install" is run for that project?
A possible solution is uploading the package to github and proceed as in NPM docs https://docs.npmjs.com/files/package.json#urls-as-dependencies
I figured out that this is possible using npm-shrinkwrap (https://docs.npmjs.com/files/package-locks). Basically, you add an npm-shrinkwrap.json file to your project, alongside package.json. In the shrinkwrap file, you can override any specific package listed in package.json - anywhere in that package's dependency chain, you specify a URL to fetch a dependency package from.
{
"name": "MyProject",
"dependencies": {
"A" : {
"version" :"1.2.1",
"dependencies": {
"B": {
"version": "https://myBForkUrl.git"
}
}
}
}
If MyProject depeneds on package A, which in turn depends on B, but B is unmaintained and has a critical bug, I can roll my own fork of B, fix the bug and host it at https://myBForkUrl.git, without also having to fork A. Based on NPM's documentation it looks like this overriding can be nested as deep as necessary.

How are npm packages managed in nodester?

I don't understand how node packages are managed in nodester. When I run nodester npm install <package-name> from CLI I don't see any packages in my app's source folder. Without these packages visible in my folder, can I use them in usual way (as if I had I installed them in my apps folder directly).
I am advised against storing packages directly in the folder since Nodester offers Node PaaS for free and it would be unkind to not optimize my app and make it use minimal space.
Secondly is there a way through which I can run the app both locally and on nodester. How can I tell git not to push the locally installed git modules. I have heard something like git ignore. How do I manage git ignore so that my local packages are not pushed on nodester?
I might not have been eloquent in framing the question as I am a newbie to node so anyone who can put my question in a better way, feel free to Edit this.
Generally the best way is to add the node_modules dir to your .gitignore file. My .gitignore looks like this for my node projects:
*.sw*
.DS_Store
node_modules
The first line ignores any Vim temp files, the second to ignore OSX .DS_Store files and the last one ignores my node_modules dir. You will need to delete your node_modules dir from your repo first using git rm if its already committed.
More explination re. gitignore files is here from GitHub.
So that will make Git disregard your node_modules, awesome. Secondly, you will need to create a package.json file. This is what tells npm (and Nodester) what your app depends on.
{
"author": "Mr Awesome", // This is your name :)
"name": "my_awesome_app", // This is your apps name
"description": "More awesome than most other apps.", // What your app does
"version" : "0.0.1", // Your apps version (increment this when you deploy)
"node": "0.6.12", // The version of node you want Nodester to run your app on
"dependencies": {
"connect" : "2.0.3", // depend on version 2.0.3 of connect
"express" : "*" // depend on the latest version of express
}
}
More information about package.json formats can be found here:
When you push to nodester should read the package.json and install your dependencies.
Hope that helps!

Resources