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.
Related
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.
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.
Is it possible to install nodejs packages (/modules) from files like in ruby's Gemfile as done with bundle install or python's requirements file (eg: requirements.pip) as done using pip install -r commands ?
Say I have a file where three or four package names are listed.
How would I instruct npm to install packages whose names are found in that file (and resolve dependencies which it also does)?
Just create your package JSON, you can use yarn to manage your packages instead of npm, it's faster.
Inside your package you can create a section of scripts accessed by npm run
scripts: {
customBuild: 'your sh code/ruby/script whateve'
}
And after you can run on terminal, npm run customBuild for example
you can use NPM init to create a package.json file which will store the node packages your application is dependent on, then use npm install which will install the node packages indicated in the package.json file
I was reading Yarn blog and found it supports yarn create just like create-react-app.
https://yarnpkg.com/blog/2017/05/12/introducing-yarn/
I tried locally... basically made very simple application with following package.json.
{
"name": "create-test-app",
"version": "1.0.0",
"bin": {
"create-test-app": "./index.js"
},
"dependencies": {
...
}
}
But somehow,,, it is complaining that it can't find the package.
"error Couldn't find package "create-test-app" on the "npm" registry.
In order to use "yarn create", I should upload on "npm" registry? Can't try it in locally? Thanks in advance.
The way to use this is yarn create <starter-kit-package>. Starter kit package must have been installed globally. You can find create-* starter kit packages in the npm registry.
Here is the link to documentation for Yarn create
To add to #Oluwafemi Sule 's answer.
The way to use this is yarn create <starter-kit-package>. Starter kit package must have been installed globally. You can find create-* starter kit packages in the npm registry.
This means that for it to work you need to install it to your yarn global or publish it to npm registry.
Fortunately you can install your local package globally. So start by creating a starter kit package then do this:
yarn global add file:/path/to/create-kit-package
Then after this you can do:
yarn create kit-package
In the above kit-package is the name of the package without create, since you are supposed to create the package with a name that matches the following format:
create-package-name
so on the cli you call the command as :
yarn create package-name
to invoke once it is installed globally.
You can publish this to NPM once you are happy with the package so that someone else or yourself can install it the normal way without first adding the package globally.
Yarn takes from NPM and does not have its own mechanism for creating packages. When you create a package on NPM, Yarn will actually grab its details through the NPM Registry.
More information: http://blog.npmjs.org/post/151660845210/hello-yarn
Can anyone advise what I'm doing wrong here (i'm new to grunt) a colleage has supplied me with a base gruntfile setup
I have installed node,js, and grunt but I cant figure out how to install the various packages (uglify, concat etc) - which command should I use for this?
The first question you must ask yourself is, "what are the names of the grunt dependencies?"
The quick answer:
Simply execute the npm install <package-name> for each package you wish to install. For the most part, I consider the package-names to be equivalent to their github name. ( grunt-contrib-concat for example. )
npm install grunt-contrib-concat will install it, then go on to the next.
Improving Maintainability:
For optimal maintainability and cross developer/environment support you should consider using package.json to list your dependencies.
Once you have all of your dependencies listed in package.json, you can then run npm install and all of them will be installed should the need ever arise. (For development based dependency vs runtime/production based dependency management, read on!)
Best Practices:
The optimal workflow with node js is to use npm init first, so that your package.json file is created for you. then once you have a package.json, you can install each of your dependencies/devDependencies by either adding them to their respective lists in the json file or adding them to their respective lists on the fly by using npm install <package name> --save or --save dev respectivly.
NOTE: IF you are experimenting, and you'd rather NOT save a node module to your dependency list, then a simple npm install <package name> will do.
At this point, I do recommend searching for npm package.json best practices - as you will find some valuable information regarding your workflow for nodejs. Dependency management can be amazing, but only if you know how to really take advantage of the utilities provided.
Consider the following excerpt from a package.json file:
"dependencies": {
"backbone": "*",
"underscore": "~*",
"requirejs": "*"
"jquery": "*"
},
"devDependencies": {
"grunt": "*",
"grunt-contrib-uglify": "*",
"grunt-contrib-concat": "*",
"grunt-contrib-copy": "*"
},
The example we see is that the npm packages under dependencies will install when running npm install --production, the development packages will be installed automatically when running npm install --development,
NOTE: You can change your node environment as well which modifies the usage of a simple npm install with no flags.
For the most part, grunt is used for development, so I believe it would be more common to find anything related to grunt being in devDependencies
A sample package.json:
http://browsenpm.org/package.json
if you have package.json with devDependencies (or dependencies) defined, all you need to run is npm install then grunt
Otherwise run something like this to install and save them to your package.json
npm install --save-dev <package-name>