Include package json dependencies for npm install global - node.js

I only know npm install locally particular plugin into the dependencies object in the package.json. Been doing npm install -g on many packages manually, but how do I include it inside the package.json?
eg. my current package:
{
"name": "mypackage",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"axios": "^0.19.0",
"lodash": "^4.17.15",
"react": "16.8.3",
}
}
To include my global install modules into mypackage
├── expo-cli#3.0.8
├── npm#6.9.0
├── npm-check-updates#3.1.20
├── react-devtools#4.1.0
├── react-native-cli#2.0.1
└── typescript#3.6.3

Go to your project root directory and execute,
npm install your-package-name
Or you can put the package-name manually in your package.json file and run,
npm install
But your package is already globally installed on your machine.
When you install an npm package, globally, using npm install -g my-package, then the package is globally available in your machine. You can use it anywhere in your machine.
But without global flag -g when you install a package, it only available inside the project scope.
So when a package is available globally, you do not need to install it in your project scope. It's a code redundant.

just type
$ npm install name-of-the-dependency
This is going to install the dependency in the package.json

The package will be added to your package.json when you install it from the project directory without -g
npm install --save package
you can also save the developer dependencies by
npm install --save-dev package
when you want to download a package for developers, such as grunt, gulp, then use this option
when you are distributing your code to production, these dependencies will not be available.
If you want to include packages like angular-cli you can install it as normal package and access it by referring the path inside the node_modules. like node_modules/.bin/ng build --prod

Appreciate with the guidance guys, just updated my question. So to add my global modules into mypackage gonna be like as per below?
{
"name": "mypackage",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"axios": "^0.19.0",
"lodash": "^4.17.15",
"react": "16.8.3",
"expo-cli": "3.0.8",
"npm": "6.9.0",
"npm-check-updates": "3.1.20",
"react-devtools": "4.1.0",
"react-native-cli": "2.0.1",
"typescript": "3.6.3"
}
}

Related

Install custom npm package in another custom npm package

I have create a npm package to install "gulp" & "nunjucks" and some other features of gulp. and published it in NPM repository named (package name: xdnunjucks-test).
Now I have created new project and trying to install "xdnunjucks-test" then it is added to my new project but all the dependencies in xdnunjucks-test are not added to new project.
command: npm install xdnunjucks-test
-- I should create a custom NPM package to install all dependencies in my new projects. This will helps me to avoid run all the commands in xdnunjucks-test everytime.
single command to run multiple commands.
here are my package.json file content.
Published Custom NPM command - package.json:
{
"name": "xdnunjucks-test",
"version": "1.0.5",
"description": "Custom npm command creation",
"main": "index.js",
"scripts": {
"test": "XDNunjucks",
"start": "npm install gulp",
"bootstrap-gulp": "npm i bootstrap-gulp",
"gulp-uglify": "npm install gulp-uglify",
"gulp-sass": "npm install node-sass gulp-sass",
"gulp-imagemin": "npm install gulp-imagemin",
"gulp-minify": "npm install gulp-minify",
"gulp-livereload": "npm install gulp-livereload",
"gulp-data": "npm install gulp-data",
"browser-sync": "npm install browser-sync"
},
"keywords": [
"npm",
"test"
],
"author": "Anil",
"license": "ISC",
"devDependencies": {
"browser-sync": "^2.26.7",
"gulp": "^4.0.2",
"gulp-data": "^1.3.1",
"gulp-imagemin": "^7.1.0",
"gulp-livereload": "^4.0.2",
"gulp-minify": "^3.1.0",
"gulp-nunjucks-render": "^2.2.3",
"gulp-sass": "^4.0.2",
"gulp-uglify": "^3.0.2",
"node-sass": "^4.13.1"
},
"dependencies": {
"bootstrap-gulp": "^2.2.5"
}
}
new project package.json:
{
"name": "n-test-2",
"version": "1.0.0",
"description": "",
"main": "gulpfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"xdnunjucks-test": "^1.0.5"
}
}
After installing the "xdnunjucks-test" command. I didn't get the features of "xdnunjucks-test" in new project.
Please help in in this.
Thank you.
All the dependencies in you package xdnunjucks-test is only "bootstrap-gulp" "^2.2.5"
all other dependency from devDependency will not be installer in package.
For example if you need to use gulp-uglify in installed package xdnunjucks-test - you should move it to dependency
devDependencies are:
installed on npm install on a directory that contains package.json, unless you pass the --production flag (go upvote Gayan Charith's answer).
not installed on npm install "$package" on any other directory, unless you give it the --dev option.
are not installed transitively.
You can find more about npm dependency on stackoverflow and npm documentation

Package installs fine as dependency from NPM registry but not from GitHub URL?

PREFACE: The repository I forked from does install fine via NPM when I install it as a package from the NPM registry. My current guess at this time there is some variance between how directories are set when a package is installed from a registry, as opposed to being installed directly from a GitHub URL in the package.json file, and this is causing the require() statement that tries to find the babel-register module to fail during the prepack script execution. If someone can point me to a document that clearly explains the steps NPM takes when installing a package from the NPM registry, it might help me figure this out.
I have a GitHub repo that I forked:
https://github.com/roschler/zos-cli
I am trying to install the package as a dependency in my Node.JS package using what I think is the proper format for referencing a GitHub repo and branch, as shown below in my package.json file:
{
"name": "basil",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"openzeppelin-zos": "^1.9.1"
},
"devDependencies": {
"ajv": "^6.5.1",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-register": "^6.26.0",
"webpack": "^4.12.0",
"webpack-cli": "^3.0.3",
"zos": "roschler/zos-cli.git#development"
}
}
However, when I execute npm install after making that change, I get the following error output:
$ npm install
> zos#1.0.0 prepack /home/robert/.npm/_cacache/tmp/git-clone-d4bae66a
> truffle compile && rm -rf lib && babel src --out-dir lib
Error: Cannot find module 'babel-register'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15)
at Function.Module._load (internal/modules/cjs/loader.js:520:25)
at Module.require (internal/modules/cjs/loader.js:650:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/home/robert/.npm/_cacache/tmp/git-clone-d4bae66a/truffle.js:1:63)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
I double-checked and the necessary babel elements are in the node_modules sub-directory of my project:
$ find . -regex .*?babel-register
./node_modules/babel-register
I then tried installing the Babel command line version globally and if you look at my package.json file I tried installing the Node API version into my project, as per their docs:
https://babeljs.io/blog/2015/10/31/setting-up-babel-6
$ npm install --global babel-cli
$ npm install --save-dev babel-core
And I still get the exact same error output. Why is it still complaining about being unable to find babel-register and how can I fix this?
UPDATE: I tried a full global install like the following and I still have the exact same error:
npm install -g ajv
npm install -g webpack
npm install -g webpack-cli
npm install -g babel-loader babel-core babel-preset-env babel-preset-react babel-register
# Created a .babelrc file in my project directory with this content:
{
"presets": [
["env", {
"targets": {
"node": "8.9"
}
}],
"react"
],
"plugins": ["transform-object-rest-spread"]
}
UPDATE 2: Found this page on Babel register:
https://new.babeljs.io/docs/en/next/babel-register.html
Tried the suggested npm install:
npm install #babel/core #babel/register --save-dev
Still no luck.
What's happening is zos-cli has a prepack script that needs the package's devDependencies to be installed. prepack gets run when installing a git dependency (https://docs.npmjs.com/misc/scripts), however the devDependencies only get installed when a package is the root package (https://docs.npmjs.com/files/package.json#devdependencies). So you're effectively trying to build zos-cli without the build tools.
Simply put, installing this module as-is as a git dependency won't work.
Some possible solutions:
(preferred) Actually publish the module and use that (you can fork and publish under your own NPM namespace, license permitting)
Fork the project on github and setup a branch in which you have already run npm i; npm run prepack; and then removed prepack from the package.json.

npm install doesn't save dependency to package.json

It does add only when I execute: npm install <package_name> --save
In the documentation though: https://docs.npmjs.com/cli/install is written this:
By default, npm install will install all modules listed as dependencies in package.json.
Which is misleading.
npm install without arguments installs all dependencies (and dev dependencies) listed in the package.json file.
npm install --production installs all the dependencies (but no dev dependency)
npm install <package> installs a package and its dependencies.
npm install <package> --save installs a package and its dependencies, and adds it in the package.json file.
Edit: Since npm 5, --save is implied.
No, it's not. I think you are misreading the sentence.
npm install without any package name (as in your quote) will install all dependencies mentioned in the package.json.
Crete package.json file in your application folder.
Exp:- var/www/html/node/rest/package.json
{
"name": "node-api",
"main": "server.js",
"version": "0.0.1",
"dependencies": {
"express": "~4.0.0",
"mongoose": "~3.6.13",
"body-parser": "~1.0.1",
"mysql": "^2.5.4"
}
}
Then run npm install command your application folder
Exp:- var/www/html/node/rest npm install
You could run npm init again to update the dependencies info.

Azure "Web apps" service: auto-install npm modules from package.json

I deployed a node.js app in an azure "web apps" container. I added the "node_modules" folder to .gitignore and let azure install modules from package.json.
However, most modules do not get automatically installed and i have to open the instance's command line and install them manually using "npm install".
I am using node v0.12.0 and here is my package.json:
{
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"devDependencies": {
"body-parser": "^1.12.4",
"cors": "^2.6.1",
"express": "*",
"gulp": "^3.8.11",
"gulp-jshint": "^1.11.0",
"gulp-nodemon": "^2.0.3",
"moment": "^2.10.3",
"mongoose": "^4.0.3"
},
"engines": {
"node": "0.12.0"
}
}
How can I enable automatic installation of modules through azure web apps and not be obliged to push the "node_modules" folder with each commit?
Azure does not install the devDependencies.
If you need them on production, it is not devDependencies but dependencies
In your package.json, devDependencies should be renamed to dependencies
This kind of thing appens when you npm install --save-dev.
Production dependency (needed to run): npm install --save
Otherwise (build tools, jshint, etc.): npm install --save-dev

Grunt error: Cannot find module 'time-grunt'

I try to install time-grunt local and global, clear npm cache, update npm, but nothing helps.
I get:
Loading "Gruntfile.js" tasks...ERROR
Error: Cannot find module 'time-grunt'
Warning: Task "default" not found. Use --force to continue.
My version of packages:
node: '0.10.31',
npm: '1.4.23'
After run:
npm install --save-dev time-grunt
in package.json state:
"devDependencies": {
"grunt": "^0.4.5",
...
"time-grunt": "^1.0.0"
}
This is part of my grunfile.js:
module.exports = function( grunt ) {
require('time-grunt')(grunt);
grunt.initConfig({
// grunt tasks here
});
// load tasks here
// register task here
}
Other grunt tasks running without error.
I'm not understand what is wrong.
How I can test correctly installation of time-grunt by CLI?
You need to add it inside dependencies and not within devDependencies. This way you don't need to separately run $ npm install --save-dev time-grunt
{
"name": "grunt-build",
"version": "0.1.0",
"private": true,
"dependencies": {
"time-grunt": "^1.3.0"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-clean": "~0.7.0",
"grunt-contrib-compress": "~0.5.0",
"grunt-contrib-concat": "~0.5.0",
"grunt-contrib-copy": "^0.8.0",
"grunt-contrib-uglify": "~0.5.0",
"grunt-remove-logging": "~0.2.0"
}
}
use npm install or sudo npm install in the folder with package.json
I ended up installing all grunt dependencies manually.
npm install time-grunt
npm install load-grunt-config
and then all modules listed here:
Note: you can install all of the dependencies in the photo above in one go with npm install.
Finally I ran the grunt command and it worked!

Resources