package.json add "npm install -g" to scripts preinstall - node.js

When writing package.json:
"scripts": {
"preinstall": "npm install -g grunt-cli"
},
"devDependencies": {
"async": "^0.9.0",
"grunt": "^0.4.5",
"grunt-contrib-connect": "^0.9.0",
"grunt-contrib-cssmin": "^0.10.0",
"grunt-contrib-jshint": "^0.10.0",
"grunt-contrib-uglify": "^0.6.0",
"grunt-contrib-watch": "^0.6.1",
"jit-grunt": "^0.9.0",
"marked": "^0.3.2",
"time-grunt": "^1.0.0",
"underscore": "^1.7.0"
}
And then running the command npm install
It works on some computers but on some I get this error:
http://pastebin.com/rYUwhV5k
Is it proper use of the package.json?
Is there any other way to do it?
How can I fix the problem that occurs on the other computers?

According to the npm documentation "NOTE: INSTALL SCRIPTS ARE AN ANTIPATTERN"
https://docs.npmjs.com/misc/scripts
In your case, probably best to just add in the readme.md that you need to have grunt installed to run the tests (and let the user decides how to install it)

Related

NPM is giving me an error using npm install

A friend shared with me a theme he is developing for WordPress, and I cloned his repo in my /themes folder, first thing I tried was to run "npm install" in the theme folder in order to get gulp and other npm cool features.
This is what it returns:
$ npm install
npm ERR! code EINTEGRITY
npm ERR! sha1-fjL3W0e4epHQRhHxvxQQmsAGUdc= integrity checksum failed when using sha1: wanted sha1-fjL3W0e4epHQRhHxvxQQmsAGUdc= but got sha512-kV/CThkXo6xyFEZUugw/+pIOywXcDbFYgSct5cT3gqlbkBE1SJdwy6UQoZvodiWF/ckQLZyDE/Bu1M6gVu5lVw== sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc=. (36808 bytes)
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\rinor\AppData\Roaming\npm-cache\_logs\2020-04-04T17_30_54_859Z-debug.log
After trying installing it globally it gave me back the same error so I came up with uninstalling node.js from my pc and then download and install the LTS version (12.16.1). Rebooted, tested and it seemed to work as I tried typing "node -v" && "npm -v". But when I return to my theme and go for "npm install" it gives me the same error.
Here is my package.json content
{
"name": "ruce-theme",
"version": "1.0.0",
"description": "A starter theme",
"scripts": {
"watch": "gulp watch"
},
"dependencies": {
"bootstrap": "^4.0.0-beta.3",
"jquery": "^3.2.1",
"npm": "^6.4.1",
"slick-carousel": "^1.8.1"
},
"devDependencies": {
"#babel/core": "^7.3.3",
"#babel/preset-env": "^7.3.1",
"#babel/preset-react": "^7.0.0",
"babel-core": "^6.25.0",
"babel-loader": "^8.0.5",
"babel-plugin-transform-react-jsx": "^6.24.1",
"babel-preset-env": "^1.6.0",
"cross-env": "^5.0.1",
"css-loader": "^2.1.0",
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"file-loader": "^3.0.1",
"gulp": "^4.0.0",
"gulp-babel": "^8.0.0-beta.2",
"gulp-clean-css": "^3.9.1",
"gulp-clone": "^1.1.4",
"gulp-concat": "^2.6.1",
"gulp-cssnano": "^2.1.2",
"gulp-ignore": "^2.0.2",
"gulp-imagemin": "^4.0.0",
"gulp-merge": "^0.1.1",
"gulp-plumber": "^1.2.0",
"gulp-rename": "^1.2.2",
"gulp-rimraf": "^0.2.2",
"gulp-sass": "^3.1.0",
"gulp-sequence": "^0.4.6",
"gulp-sourcemaps": "2.6.2",
"gulp-uglify": "^3.0.0",
"gulp-watch": "^4.3.11",
"node-sass": "^4.11.0",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1"
},
"license": "ISC"
}
These are the erros that the debug shows
20111 error code EINTEGRITY
20112 error sha1-fjL3W0e4epHQRhHxvxQQmsAGUdc= integrity checksum failed when using sha1: wanted sha1-fjL3W0e4epHQRhHxvxQQmsAGUdc= but got sha512-kV/CThkXo6xyFEZUugw/+pIOywXcDbFYgSct5cT3gqlbkBE1SJdwy6UQoZvodiWF/ckQLZyDE/Bu1M6gVu5lVw== sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc=. (36808 bytes)
20113 verbose exit [ 1, true ]
You don't mention whether you had package-lock.json or not. In some cases the following might help. But first make sure you understand what's going on. npm tells you that the checksum from https://registry.npm.org doesn't match the one from package-lock.json. Either it changed in the registry, or...
Solution Consider the line from the output:
npm ERR!
sha1-fjL3W0e4epHQRhHxvxQQmsAGUdc=
integrity checksum failed when using sha1: wanted
sha1-fjL3W0e4epHQRhHxvxQQmsAGUdc=
but got
sha512-kV/CThkXo6xyFEZUugw/+pIOywXcDbFYgSct5cT3gqlbkBE1SJdwy6UQoZvodiWF/ckQLZyDE/Bu1M6gVu5lVw== sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc=
. (36808 bytes)
Find the package in package-lock.json by the first two integrity checksums (sha1-...), and put the third one (sha512-...) into its "integrity" field.
More on it here.

npm WARN The package is included as both a dev and production dependency

When running npm install
I got these warning:
npm WARN The package babel-core is included as both a dev and production dependency.
npm WARN The package babel-loader is included as both a dev and production dependency.
npm WARN The package babel-preset-react is included as both a dev and production dependency.
npm WARN The package redux-thunk is included as both a dev and production dependency.
npm WARN The package uglifyjs is included as both a dev and production dependency.
up to date in 7.183s
npm WARN The package babel-core is included as both a dev and production dependency.
npm WARN The package babel-loader is included as both a dev and production dependency.
npm WARN The package babel-preset-react is included as both a dev and production dependency.
npm WARN The package redux-thunk is included as both a dev and production dependency.
npm WARN The package uglifyjs is included as both a dev and production dependency.
package.json file:
{
...
"dependencies": {
"axios": "^0.15.3",
"babel-core": "^6.10.4",
"babel-loader": "^6.2.4",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-2": "^6.24.1",
"babel-register": "^6.9.0",
"bluebird": "^3.5.0",
"bootstrap-sass": "^3.3.7",
"classnames": "^2.2.5",
"console-polyfill": "^0.2.3",
"cross-env": "^1.0.8",
"css-loader": "^0.23.1",
"deepmerge": "^1.3.2",
"dom-helpers": "^3.0.0",
"expect": "^1.20.1",
"fuzzy": "^0.1.3",
"moment": "^2.18.1",
"node-libs-browser": "^1.0.0",
"node-sass": "^3.8.0",
"react": "^15.1.0",
"react-addons-shallow-compare": "15.4.0",
"react-addons-test-utils": "^15.1.0",
"react-axios": "0.0.9",
"react-bootstrap-daterangepicker": "^3.2.2",
"react-daterange-picker": "^1.1.0",
"react-dom": "^15.1.0",
"react-draggable": "^2.2.3",
"react-redux": "^4.4.8",
"react-router": "^3.0.2",
"react-virtualized": "^8.5.2",
"redux": "^3.6.0",
"redux-logger": "^2.6.1",
"redux-promise": "^0.5.3",
"redux-promise-middleware": "^4.2.0",
"redux-thunk": "^2.1.0",
"sass-loader": "^4.0.0",
"style-loader": "^0.13.1",
"uglifyjs": "=2.4.10",
"webpack-dev-middleware": "^1.6.1",
"webpack-dev-server": "^1.14.1",
"webpack-hot-middleware": "^2.11.0"
},
"devDependencies": {
"assets-webpack-plugin": "^3.5.1",
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-react-transform": "^2.0.2",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2016": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-3": "^6.24.1",
"didyoumean": "^1.2.1",
"extract-text-webpack-plugin": "^1.0.1",
"glob": "^7.1.1",
"postcss-loader": "^1.3.0",
"purifycss-webpack-plugin": "^2.0.3",
"react-transform-hmr": "^1.0.4",
"redux-thunk": "^2.2.0",
"uglifyjs": "=2.4.10",
"webpack": "^1.15.0",
"webpack-cleanup-plugin": "^0.5.1",
"webpack-split-chunks": "^0.1.1"
}
}
Noticed that there were different versions installed. What exactly happens when there are two versions of the same package installed for dev and prod? Is this normal behavior? Should I get rid of the dupe references in the package.json file?
You have the package referred to in both sections of your dependencies; you should totally not do this because it means that your production install will have a different version to your development install.
If you do npm install you will get all dependencies & devDependencies installed; however if you do npm install --production you only get dependencies installed.
You should remove things you don't need for your app to run from dependencies and place them in devDependencies. Things in dependencies should be seen as requirements to run the application (after any code transformation has occurred).
There is zero case where a dependency should be in both.
My use-case is exactly what #Jim pointed out in the comment of the accepted answer, In development I wanted to use my local module files as I was working on it the same time I worked on my other projects using it.
In production I would use the module from VCS, and I don't want to manually change the package.json file every time.
This is how I set up my package.json:
"dependencies": {
"module-name": "git+ssh://git#bitbucket.org/XXX/XXX.git#master"
},
"devDependencies": {
"module-name-dev": "file:../XXX"
}
With this setup, npm doesn't give me any errors, because the modules name are different, now what left to do is to require the dev package in development instead the main one.
I found the module-alias package, it allows you to use alias names for paths you want to require.
In your app.js file at the very beginning you need to add this code:
if (process.env.NODE_ENV === 'development') {
const moduleAlias = require('module-alias');
moduleAlias.addAlias('module-name', 'module-name-dev');
}
From now on, every time you require the module-name module, you will actually get the module-name-dev in development.
In production you shouldn't install the devDependencies, and the alias will not work, so no extra steps needed to change between the 2.
Working with webpack
If you are using webpack, you do not need the module-alias, you can add alias to the webpack config using webpack-chain like this:
chainWebpack: (config) => {
if (process.env.NODE_ENV === 'development') {
config.resolve.alias
.set('module-name', 'module-name-dev');
}
},

Npm dependencies not installed through chef

We are trying to resolve node dependencies using 'npm install' in chef recipe through execute resource.
This my chef recipe :
execute "download dependancies from package.json" do
command "npm install --silent"
user "#{node['non_root_user']}"
end
This is my package json :
"dependencies": {
"classnames": "^2.2.5",
"keymirror": "^0.1.1",
"moment": "^2.10.2",
"moment-timezone": "^0.4.0",
"node-sass": "^3.2.0",
"parse": "^1.4.2",
"react": "^0.13.2",
"react-swipeable": "^3.0.2"
},
"devDependencies": {
"babel-eslint": "^4.0.10",
"brfs": "^1.4.0",
"browser-sync": "^2.6.7",
"browserify": "^9.0.7",
"browserify-shim": "^3.8.5",
"classnames": "^2.2.5",
"dotenv": "^1.2.0",
"eslint": "^1.1.0",
"eslint-plugin-react": "^3.2.3",
npm install only installs dev-dependencies but not dependencies. is there any automated way to install all dependencies using npm install? Am I missing anything additional configuration with npm.
What you have is fine so there is probably some other issue. If you want something simpler check out the poise-javascript and application_javascript cookbooks though.

"npm install" insall nothing to node_modules folder but a .staging folder centos 7

I have this package.json
{
"name": "jangkoo",
"version": "0.0.0",
"private": true,
"dependencies": {
"keystone": "^0.3.19",
"async": "^1.5.0",
"lodash": "^4.13.1",
"node-sass": "^3.3.2",
"node-sass-middleware": "^0.9.7",
"dotenv": "^2.0.0"
},
"devDependencies": {
"eslint": "^2.12.0",
"eslint-config-keystone": "^2.3.1",
"eslint-plugin-react": "^5.1.1",
"gulp": "^3.7.0",
"gulp-shell": "^0.5.0",
"gulp-watch": "^4.3.5",
"gulp-sass": "^2.0.4"
},
"scripts": {
"lint": "eslint .",
"start": "node keystone.js"
}
}
When i run npm install, it created a node_modules with a .staging folder inside. No other modules installed. What's the problem here? The OS is Centos 7 .
npm install temporarily adds all node_modules into a .staging folder during install. If something goes wrong during install, like you run out of memory, you could be stuck with this weird node_modules install containing the .staging folder.
https://github.com/npm/npm/issues/12540
for install npm for centos
yum update
yum install npm
I found it out. It failed to install keystone then it stopped. That's why nothing has been installed.
How about remove the node_modules folder and run npm install again

How to define scripts for npm command?

I am using npm+webpack to manage a react web application. And I want to define a few command for npm in order to run some webpack command. Below is the two command scripts I defined in package.json file.
"scripts": {
"start": "webpack-dev-server --host 0.0.0.0",
"build": "NODE_ENV=production webpack --config ./webpack.config.js --progress --profile --colors"
},
As you can see, there are two commands 'start' and 'build'. When I run npm start it will run webpack-dev-server --host 0.0.0.0 to launch a web server.
But when I run npm build, it doesn't run the configured command and simply returns without any output. I wander how to define a script command for npm to use. Below is my whole package.json file:
{
"name": "demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --host 0.0.0.0",
"build": "NODE_ENV=production webpack --config ./webpack.config.js --progress --profile --colors"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.9.0",
"babel-loader": "^6.2.4",
"compression-webpack-plugin": "^0.3.1",
"css-loader": "^0.23.1",
"less": "^2.7.1",
"less-loader": "^2.2.3",
"npm-install-webpack-plugin": "^3.1.3",
"style-loader": "^0.13.1",
"svg-sprite-loader": "0.0.26",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1",
"webpack-shell-plugin": "^0.4.2"
},
"dependencies": {
"actions": "^1.3.0",
"axios": "^0.12.0",
"babel-preset-stage-2": "^6.11.0",
"cdnjs": "^0.3.2",
"components": "^0.1.0",
"containers": "0.0.1",
"extract-text-webpack-plugin": "^1.0.1",
"features": "^0.1.0",
"file-loader": "^0.8.5",
"fo": "^0.1.1",
"jshint": "^2.9.2",
"jshint-loader": "^0.8.3",
"leaflet": "^0.7.7",
"material-ui": "^0.15.2",
"moment": "^2.13.0",
"normalize.css": "^3.0.2",
"nuka-carousel": "^2.0.0",
"public": "^0.1.2",
"query-string": "^4.2.2",
"react": "^15.1.0",
"react-addons-css-transition-group": "^15.1.0",
"react-addons-shallow-compare": "^15.1.0",
"react-alert": "^1.0.14",
"react-button": "^1.2.1",
"react-cookie": "^0.4.7",
"react-date-picker": "^5.3.9",
"react-datepicker": "^0.27.0",
"react-dom": "^15.0.2",
"react-infinite-calendar": "^1.1.14",
"react-redux": "^4.4.5",
"react-router": "^2.4.1",
"react-router-redux": "^4.0.5",
"react-select": "^1.0.0-beta13",
"react-spinkit": "^1.1.8",
"react-tap-event-plugin": "^1.0.0",
"react-tappable": "^0.8.1",
"redux": "^3.5.2",
"redux-thunk": "^2.1.0",
"sha1": "^1.1.1",
"source-map-loader": "^0.1.5",
"src": "^1.1.2",
"style": "0.0.3",
"url-loader": "^0.5.7",
"utils": "^0.3.1"
}
}
You can use npm run-script <script-name> to run arbitrary scripts. In your case, npm run-script build.
From the npm documentation, The npm <script-name> syntax is only supported for a specific number of predetermined scripts such as start.
npm supports the "scripts" property of the package.json script, for
the following scripts:
prepublish: Run BEFORE the package is published. (Also run on local npm install without any arguments.)
publish, postpublish: Run AFTER the package is published.
preinstall: Run BEFORE the package is installed
install, postinstall: Run AFTER the package is installed.
preuninstall, uninstall: Run BEFORE the package is uninstalled.
postuninstall: Run AFTER the package is uninstalled.
preversion, version: Run BEFORE bump the package version.
postversion: Run AFTER bump the package version.
pretest, test, posttest: Run by the npm test command.
prestop, stop, poststop: Run by the npm stop command.
prestart, start, poststart: Run by the npm start command.
prerestart, restart, postrestart: Run by the npm restart command. Note: npm restart will run the stop and start scripts if no restart script is provided.
Additionally, arbitrary scripts can be executed by running npm run-script <pkg> <stage>. Pre and post commands with matching names
will be run for those as well (e.g. premyscript, myscript,
postmyscript).
As well, you can use npm run <script-name> as a shorthand.
You can define an npm script just like how you have it here:
"scripts": {
"start": "webpack-dev-server --host 0.0.0.0",
"build": "NODE_ENV=production webpack --config ./webpack.config.js --progress --profile --colors"
},
To run the build command enter:
$ npm run build
you need to use command npm run build

Resources