Do I need to bundle files when publishing a library to npm? - javascript

My project structure very very roughly looks like this:
dist/
- helper.compiled.js
- entrypoint.compiled.js
src/
- helper.js
- entrypoint.js
I was reading the npm publishing guidelines and it says to provide a single index.js file. But is this really necessary? Afterall, my entrypoint.compiled.js can just require helper.compiled.js. What is the benefit of providing a single index.js file?
What is the recommended procedure for publishing a library to npm in this situation? I tried using npm pack, but I don't quite understand what it is doing.

The best way to bundle just the compiled files is to put the directory in the files section of your package.json. This will then only package the files that npm uses such as package.json, README.md, and other files that your package requires.
An example package.json looks something like this:
{
"name": "my-library",
"version": "1.0.0",
"description": "My Library.",
"main": "dist/entrypoint.compiled.js",
"scripts": {},
"author": "",
"license": "ISC",
"dependencies": {},
"files": [
"dist"
]
}

You need only to publish the compiled files. If you compile them correctly then there is no need to include your src/ folder in the package.
Here is my .npmignore file for my tree in react package: .npmignore You can see what is in the package here.
As you can see, I publish only the dist directory and every file in the root. The bare minimum is the package.json and the dist directory.
npm publish command essentially just creates a package from your files and uploads it to the npm registry. After running the command you will be able to find the npm package and use it as any other package.
After you run npm publish I recommend downloading published the package from the registry and try out if all the required files are there and verify that you can use everything.

Related

Is there a way to get npm install to install "bin" executables in the local node_modules directory (the .bin directory)?

I have a Node project, and I can npm pack it into a tarball and install it in another directory for testing. When I do that, the set of files in the "bin" clause of my package.json are correctly symlinked into the node_modules/.bin directory there. Thus I can use npx something to run the "something" script and it works as I would expect.
From the actual project directory, however, npm install doesn't do that. The scripts are there in my top-level "bin" directory of course, but npx doesn't find them. Obviously I could just run them directly, but it would be nice if I could run the same shell commands to run them in other installation directories and in the project home directory.
I don't want to install the package globally; I want to keep everything local, at least for now. So can I get npm to make those symlinks for me, or should I just bite the bullet and do it myself?
Here's my package.json:
{
"name": "#foo/plazadb",
"version": "0.1.0",
"description": "Basic database setup with upload/download from CSV",
"main": "lib/plazadb.js",
"author": "Pointy",
"license": "ISC",
"dependencies": {
"arg": "^5.0.1",
"cls-hooked": "^4.2.2",
"csv-parser": "^3.0.0",
"csv-stringify": "^6.0.5",
"neat-csv": "^7.0.0",
"pg": "^8.7.*",
"sequelize": "^6.16.*"
},
"bin": {
"conntest": "./bin/conntest.js",
"download": "./bin/download.js",
"upload": "./bin/upload.js"
}
}
The "bin" files all exist (of course, otherwise they would never work). What I'm trying (out of ignorance) is a simple npm install from the project directory.
One way to do is by
specifying a local prefix by using npm config set prefix <path> or one of these methods linked here.
generating symlinks using npm link
As a word of caution, to undo npm link executing npm unlink . may not work. Instead one may have to run npm uninstall -g ..

Custom NPM package not creating it's self node_modules [duplicate]

When I npm install my node application, my the packages list in the dependencies property of my package.json are installed. But, for some reason some of those dependencies are not installing their sub-dependencies. In other words, there is no node_modules folder with the dependencies of my dependencies.
myproject
- node_modules
- my-package
- node_modules (would expect this to be here, but it's not)
The strange thing is that is another project, the sub-dependencies are being installed for the same packages.
Even when I try to manually install a single package via npm install my-package, that packages node_modules are not installed.
Is there a reason why this might be the case? Or a way I can debug this?
Thanks
NPM tries to flatten your dependencies at the root level. If is a version that satisfies all of your dependencies(Either only one package the dependency, or the version satisfies all package requirements as defined in package.json) it will roll it up to the root of your node_modules. This is done intentionally, so that you do not installed the same dependency multiple times.
The exception to this rule occurs if there are conflicting versions of a module e.g. package1 has dependency a version 1.0.1 and package2 has dependency a version 2.
I opened a similar question and was brought back to this one where I see a lack of an actual solution.
I've currently managed to find it and the correct one to get your package to install its own node_modules is to add the following to your package.json:
"bundledDependencies": [
"npm-package"
]
To that array, add the packages you want to be installed in the node_modules folder of my-package:
myproject
- node_modules
- my-package
- node_modules <-- This folder will contain the packages of the array
So, an example of package json could be:
{
"name": "my-package",
"version": "1.0.0",
"dependencies": {
"cheerio": "^1.0.0-rc.10",
"jsdom": "^19.0.0",
"yargs": "13.2"
},
"bundledDependencies": [
"yargs",
"jsdom"
]
}
When you will install my-package inside one other project it will have it's own node_modules with the packages specified in bundledDependencies.

Where is json.config file found in the root of the project?

I am new in development field.
I have already made an app in create-react-app.
But now I have been asked to submit json.config file..
It is mentioned that -
The file must be called config.json and must be stored in the root level of your project.
Here is the format of the configuration file:
{
"install": "npm install",
"run": "npm start",
"port": 3000
}
I am unable to find it in my project folders and dependency folders and files.
Create-React-App creates this file for you under the hood. So you don't have to setup it. You can open the terminal and write npm install and after npm start. That will open your web app in http://localhost:3000.
More details https://create-react-app.dev/docs/available-scripts
Edit: Dependencies should be inside the package.json in the root directory.

Installing local Node.js project globally

I'm trying to install my custom made project globally across my system so I can call it anywhere in my computer from the cmd, but it doesn't quite work.
I tried:
npm install -g .
and more but I can't remember. This is my package.json:
...
"name": "nodejs-insta-reddit-cli",
"version": "1.0.0",
"description": "",
"main": "./build/index.js",
"bin": {
"instapostwouter": "./build/index.js"
},
...
The reason the main path is in ./build/index.js is because I'm using typescript.
So my question is: How would I install my project globally so that I can use it on my workstation in any directory?
Use npm link in the directory of local module you want to access globally.
Then you add it using npm link package_name.
OR you can publish the module (Private or Public) and use install it globally and use it.
More information on Npm Link
NOTE: if you are working with cli package then make sure you have a bin folder and entry inside the package.json file.

node.js minimal setup and for what is the package.json?

So i start off with writing my own node.js app and the only thing i want is to include a package for saml.
So i was wondering what is the minimal requirement for my app.
I was just creating a node.js file and then i installed the package by:
node install some-saml-passports-package.
I got this warning after installation:
npm WARN enoent ENOENT: no such file or directory, open '.../mynodeapp/package.json'
I removed the package and created a package.json file. Which results in a parsing error because there was no content inside.
I have read that I only need a package.json when I plan to create my own package. Also I read something about local and global installation of package files in npm. Well, i need a bit of clarification for my doing.
What is the use of package.json?
Do I need it when I only want to create a standalone app in node with one dependency to an existing package?
Is it recommended even if I don't need it at all?
There is no minimum requirement for node.js application. The package.json is only required to manage your application for dependencies, module descriptions , handle npm scripts etc. You can install packages without package.json, just don't use the --save flag with npm install. E.g.:
npm install <package_name> - you can still run your node application without any problem. But I will suggest you to add a package.json file and add following:
{
"name": "<package_name>",
"version": "<version>", //0.0.1 for starters
"description": "",
"main": "<entry point of application>", // example: app.js , index.js etc
,
"author": "<your name>",
"license": "ISC",
"dependencies": {}
}
You can create this by adding it manually or just execute npm init in your project root directory and answer some basic questions in console.
Here are some useful links:
npm init
how to use package.json
The minimal file is:
{
}
Now you can start using commands like npm install web3 --save and they will save onto this file.
Create package.json via npm init command.
Package.json contains data about your project and what is more important for standalone app - it contains dependencies list. You can install all dependencies from package.json with npm install.
If you want to install package and save it in package.json type npm install package-name --save.

Categories