How do you use "yarn create <pkg-name>"? - javascript

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

Related

Difference between npx and npm?

I have just started learning React, and Facebook helps in simplifying the initial setup by providing the following ready-made project.
If I have to install the skeleton project I have to type npx create-react-app my-app in command-line.
I was wondering why does the Facebook in Github have npx create-react-app my-app rather than npm create-react-app my-app?
Introducing npx: an npm package runner
NPM - Manages packages but doesn't make life easy executing any.NPX - A tool for executing Node packages.
NPX comes bundled with NPM version 5.2+
NPM by itself does not simply run any package. It doesn't run any package as a matter of fact. If you want to run a package using NPM, you must specify that package in your package.json file.
When executables are installed via NPM packages, NPM links to them:
local installs have "links" created at ./node_modules/.bin/ directory.
global installs have "links" created from the global bin/ directory (e.g. /usr/local/bin) on Linux or at %AppData%/npm on Windows.
Documentation you should read
NPM:
One might install a package locally on a certain project:
npm install some-package
Now let's say you want NodeJS to execute that package from the command line:
$ some-package
The above will fail. Only globally installed packages can be executed by typing their name only.
To fix this, and have it run, you must type the local path:
$ ./node_modules/.bin/some-package
You can technically run a locally installed package by editing your packages.json file and adding that package in the scripts section:
{
"name": "whatever",
"version": "1.0.0",
"scripts": {
"some-package": "some-package"
}
}
Then run the script using npm run-script (or npm run):
npm run some-package
NPX:
npx will check whether <command> exists in $PATH, or in the local project binaries, and execute it. So, for the above example, if you wish to execute the locally-installed package some-package all you need to do is type:
npx some-package
Another major advantage of npx is the ability to execute a package which wasn't previously installed:
$ npx create-react-app my-app
The above example will generate a react app boilerplate within the path the command had run in, and ensures that you always use the latest version of a generator or build tool without having to upgrade each time you’re about to use it.
Use-Case Example:
npx command may be helpful in the script section of a package.json file,
when it is unwanted to define a dependency which might not be commonly used or any other reason:
"scripts": {
"start": "npx gulp#3.9.1",
"serve": "npx http-server"
}
Call with: npm run serve
Related questions:
How to use package installed locally in node_modules?
NPM: how to source ./node_modules/.bin folder?
How do you run a js file using npm scripts?
npx is a npm package runner (x probably stands for eXecute). One common way to use npx is to download and run a package temporarily or for trials.
create-react-app is an npm package that is expected to be run only once in a project's lifecycle. Hence, it is preferred to use npx to install and run it in a single step.
As mentioned in the main page https://www.npmjs.com/package/npx, npx can run commands in the PATH or from node_modules/.bin by default.
Note:
With some digging, we can find that create-react-app points to a Javascript file (possibly to /usr/lib/node_modules/create-react-app/index.js on Linux systems) that is executed within the node environment. This is simply a global tool that does some checks. The actual setup is done by react-scripts, whose latest version is installed in the project. Refer https://github.com/facebook/create-react-app for more info.
NPM is a package manager, you can install node.js packages using NPM
NPX is a tool to execute node.js packages.
It doesn't matter whether you installed that package globally or locally. NPX will temporarily install it and run it. NPM also can run packages if you configure a package.json file and include it in the script section.
So remember this, if you want to check/run a node package quickly without installing locally or globally use NPX.
npM - Manager
npX - Execute - easy to remember
npm - Package manager for JavaScript, just like: pip (Python), Maven (Java), NuGet (.NET), Composer (PHP), RubyGems (Ruby), ...
npx - runs a command of a package without installing it explicitly.
Use cases:
You don't want to install packages neither globally nor locally.
You don't have permission to install it globally.
Just want to test some commands.
Sometime, you want to have a script command (generate, convert something, ...) in package.json to execute something without installing these packages as project's dependencies.
Syntax:
npx [options] [-p|--package <package>] <command> [command-arg]...
Package is optional:
npx -p uglify-js uglifyjs --output app.min.js app.js common.js
+----------------+ +--------------------------------------------+
package (optional) command, followed by arguments
For example:
Start a HTTP Server : npx http-server
Lint code : npx eslint ./src
# Run uglifyjs command in the package uglify-js
Minify JS : npx -p uglify-js uglifyjs -o app.min.js app.js common.js
Minify CSS : npx clean-css-cli -o style.min.css css/bootstrap.css style.css
Minify HTML : npx html-minifier index-2.html -o index.html --remove-comments --collapse-whitespace
Scan for open ports : npx evilscan 192.168.1.10 --port=10-9999
Cast video to Chromecast : npx castnow http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4
More about command:
https://docs.npmjs.com/files/package.json#bin
https://github.com/mishoo/UglifyJS2/blob/master/package.json#L17
NPX:
From https://www.futurehosting.com/blog/npx-makes-life-easier-for-node-developers-plus-node-vulnerability-news/:
Web developers can have dozens of projects on their development
machines, and each project has its own particular set of npm-installed
dependencies. A few years back, the usual advice for dealing with CLI
applications like Grunt or Gulp was to install them locally in each
project and also globally so they could easily be run from the command
line.
But installing globally caused as many problems as it solved. Projects
may depend on different versions of command line tools, and polluting
the operating system with lots of development-specific CLI tools isn’t
great either. Today, most developers prefer to install tools locally
and leave it at that.
Local versions of tools allow developers to pull projects from GitHub
without worrying about incompatibilities with globally installed
versions of tools. NPM can just install local versions and you’re good
to go. But project specific installations aren’t without their
problems: how do you run the right version of the tool without
specifying its exact location in the project or playing around with
aliases?
That’s the problem npx solves. A new tool included in NPM 5.2, npx is
a small utility that’s smart enough to run the right application when
it’s called from within a project.
If you wanted to run the project-local version of mocha, for example,
you can run npx mocha inside the project and it will do what you
expect.
A useful side benefit of npx is that it will automatically install npm
packages that aren’t already installed. So, as the tool’s creator Kat
Marchán points out, you can run npx benny-hill without having to deal
with Benny Hill polluting the global environment.
If you want to take npx for a spin, update to the most recent version
of npm.
Simple Definition:
npm - Javascript package manager
npx - Execute npm package binaries
Here's an example of NPX in action: npx cowsay hello
If you type that into your bash terminal you'll see the result. The benefit of this is that npx has temporarily installed cowsay. There is no package pollution since cowsay is not permanently installed. This is great for one off packages where you want to avoid package pollution.
As mentioned in other answers, npx is also very useful in cases where (with npm) the package needs to be installed then configured before running. E.g. instead of using npm to install and then configure the json.package file and then call the configured run command just use npx instead. A real example:
npx create-react-app my-app
NPM => Is a JS package manager.
NPX => Is a tool for executing Node packages and execute npm package binaries.
It is easy to remember:
-npm stands for MANAGER
-npx stands for EXECUTE
NPM: NPM stands for Node Package Manager and is the default package manager for Node.js. It was developed by Isaac Z. Schlueter and was originally released on January 12, 2010. It is entirely written in JavaScript. It consists of a command-line client npm which manages all node.js packages and modules. When node.js is installed, it is included in the installation.
npm run your-package-name
NPX is a tool that use to execute packages.
NPX is an acronym for Node Package Execute The NPX package comes with npm, so when you install npm above 5.2.0, NPX will be installed automatically.
It is an npm package runner that can execute any package that you want from the npm registry without even installing that package. The npx is useful during a single time use package. If you have installed npm below 5.2.0 then npx is not installed in your system.
Run the following command to determine if npx is installed:
npx -v
The following command can be run if npx is not installed.
npm install -g npx
Use npx to execute the package:
npx your-package-name
Simplest Definition:
NPX
The npx stands for Node Package Execute and it comes with the npm,
when you installed npm above 5.2.0 version then automatically npx will
installed. It is an npm package runner that can execute any package
that you want from the npm registry without even installing that
package.
NPM
npm is a package manager for the JavaScript programming language
maintained by npm, Inc. npm is the default package manager for the
JavaScript runtime environment Node.js. It consists of a command line
client, also called npm, and an online database of public and paid-for
private packages
NPM vs. NPX
NPM stands for the Node Package Manager. A text based program for Nodejs package management.
While NPX is a Node Package Runner. Its function is to execute the Nodejs package
NPX will execute binary files from the Nodejs package, both installed and not.
Even NPX can also help us use certain versions of Nodejs without having to use nvm (node.js version management), nave (node.js virtual environment), and nvm (node.js version management).
NPM stands for Node Package Manager. NPM is Node.JS's default package manager. It's written in Javascript. The role of NPM is to manage the package and modules of node.js.
NPX stands for Node Package Execute. NPX comes with npm, when npm is installed above the 5.2.0 version, it gets installed automatically. NPX is an npm package runner and its role is to execute the package from the registry without even installing that package.
Now, the differences between NPM and NPX are as below:
i) NPM is used to install the packages while NPX is used to execute the packages.
ii) Due to npm the packages installed have to be taken care of since it's installed globally while the packages which are used by npx don't need to be taken care of as they are not installed globally.
Simple answer is like
NPX: is used to execute any node package without installing the package on our machine.
NPM: is used to install any node js package in our machine. We can use "require("package-name')" when we install any package using NPM. but we can not import the package when we use NPX.
Example: You should run npm i axios
in this case you are installing axios package in your local machine
and npx create-react-app 'app-name'
here you are executing the create-react-app package directly on your machine without installing it's files.
NPM stands for Node Package Manager.
It comes pre-installed with Node.js. NPM helps to manage packages in your projects as dependencies.
When using NPM, there are two ways to install a package into your local computer.
Locally: When a package is installed locally, it is installed in
./node_modules/.bin/ of the local project directory.
Globally: A global package is installed in the user environment
path. /usr/local/bin for Linux and AppData%/npm for Windows.
To execute a locally installed package, it should be specified in the package.json scripts block as shown below.
"scripts": {
"your-package": "your-package-name"
}
Then, you can execute the package with:
npm run your-package-name
NPX is an NPM package executor.
Currently, NPX is bundled with NPM when you install the NPM version 5.2.0 or higher.
Why NPX over NPM?
No need to edit the package.json file with node_modules paths.
You can directly execute the tool from the command line.
The differences between NPM and NPX are as below:
i) NPM is used to install the packages while NPX is used to execute the packages.
ii) Due to npm the packages installed have to be taken care of since it's installed globally while the packages used by npx don't need to be taken care of as they are not installed globally.
NPX is a tool for creating and executing some features in a new project
NPM is the package manager that contains all of libraries
Here is the simple definition.
NPM is a package manager, you can install node.js packages using NPM
NPX is a tool to execute node.js packages.
Here's an example of what your app creation might look like using npx
npx create-react-app project-name --template all
Simply npm is the Node Package Manager and
npx is the executeable version that run npm packages
npm is a tool that use to install packages and npx is a tool that use to execute packages.
npm-If you wish to run package through npm then you have to specify that package in your package.json and install it locally.
npx-A package can be executable without installing the package. It is an npm package runner so if any packages aren’t already installed it will install them automatically.
npm - package manager
npx - Execute npm package
This is a difference with it.
npm is package manager or installer on the other hand Packages used by npx are not installed globally so you have to carefree for the pollution for the long term.
Actually, I tried many ways to solve this and failed but finally removing/deleting yarn globally solves the problem
just type this command in your commandline terminal:
npm uninstall -g yarn
And then run the command below to install the react starter project
npx create-react-app

How can I update the package.json using meteor add?

How can I get my package.json updated by meteor add command as it can be achieved by npm install --save?
I just read there that meteor writes in the .meteor/package. Does that mean package.json can only be updated via npm install --save whereas meteor add only effects .meteor/package?
I'll highly appreciate detailed information on that.
There are two ways of adding packages to a meteor project.
With meteor add
With npm install
These are two separate ways of managing packages, and one doesn't affect the other.
If you install, for example, jquery with meteor add, you'd use import {$} 'meteor/jquery' to use in a file.
If you use npm install to install jquery, then you might use import $ from 'jquery'.
Don't confuse npm packages with meteor packages.

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.

If I do an NPM install with "npm install --save-dev" how can I check for updates

If I install a number of different packages from what I understand using --save-dev will create a local package.json file listing these.
Is there some way using npm (or Webstorm IDE) that I can check these packages to see if any of them have been updated and then download later versions?
all --save-dev will do is augment your existing package.json file with a key of
"devDependencies": {
}
This will keep the dependencies at the project level, instead of global requirement, when you npm install.
If you want to update your dependency list you can simply use 'npm update', if you want to update a specfic package use 'npm update' with the module name. If you wish to see which packages have an update use 'npm outdated'
https://www.npmjs.org/doc/cli/npm-update.html
https://www.npmjs.org/doc/cli/npm-outdated.html

GRUNT - Install Grunt Packages?

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>

Categories