Post install is replacing npm install folder in linux - linux

I am doing npm install ,after that it is creating node modules folder .
then there is post install option is there which is working fine in windows but in Linux box it is deleting node modules and creating again .
package.json format:
{
"someDependency":{
"#fortawesome/ember-fontawesome": "0.1.9",
"#fortawesome/free-regular-svg-icons": "5.1.0",
},
"scripts":{
"postInstall": "npm install --abc download"
}
}
can anyone help me with how to resolve the issue.

Related

npm not installing packages and not creating package-lock.json

I created a new Laravel project with laravel new x
changed into that folder with cd x
tried installing the npm packages with npm install
This didn't work like expected.
I just got an Error: Cannot destructure property 'name' of '.for' as it is undefined.
Then I was like yeah lets just add a name to the package so I did
Ran npm install again
After that it installed something and prompted: Installed 1 Package
Instead of installing the specified devDependencies, creating a package-lock and a node_modules folder, it installed a package with the name that i just specified, globally. I knew that because of npm ls -g. So now i dont have any packages installed locally in my folder but a dependency globally that does nothing.
Output of npm ls and npm ls -g:
C:\Users\johnw\AppData\Roaming\npm
└── theNameIJustSpecified#
My package.json looks like this (and was auto-generated by the laravel installer):
{
"name": "myName",
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build"
},
"devDependencies": {
"axios": "^1.1.2",
"laravel-vite-plugin": "^0.7.2",
"lodash": "^4.17.19",
"postcss": "^8.1.14",
"vite": "^4.0.0"
}
}
npm -v: 9.2.0
node -v: v19.3.0
os: windows 11
Normally this just worked out of the box with the Laravel installer. I just typed npm install and it worked.
I tried reinstalling Node and npm, even used Nvm for Windows without any change.
I tried restarting my pc without any change.
I tried creating the package.json via npm init, didn't change anything other than the output of npm ls -g (added 1.0.0 to the end):
C:\Users\johnw\AppData\Roaming\npm
└── theNameIJustSpecified#1.0.0
Edit: Tried downgrading to npm 8, also didn't work.
Downgrading to 7 works again, but I get warning because of too old version.
I checked other projects (old Laravel projects) and they have the same problem now. I used to work in them for months using npm packages for asset bundling. Now they give me the same error.

NPM 7 workspaces - how to install new package in workspace?

If I have a NPM 7 workspace like this:
root
- submodule0
- submodule1
- submodule2
and I navigate to the submodule0 directory and run npm i somepackage it seems to "break" the workspace by creating a new package-lock.json in the submodule0 directory and installing all the dependencies there. In other words, it just does the old behavior that existed before I created the workspace.
I was hoping for a command similar to lerna where I can install a new package in submodule0 from the root. Something like:
npm i somepackage --scope submodule0
So far, the only workaround I can find is to edit the submodule0 package.json and add the somepackage manually. Then run npm i from the root. Obviously this is not ideal because I need to look up the #latest version, navigate to the subdirectory, open the package.json, etc. etc. as opposed to just typing one line in the root.
Workspace support for npm install and npm uninstall was added in npm v7.14.0. You can now just do:
npm i somepackage --workspace=submodule0
Uninstalling modules has been the biggest pain, so this is really exciting. The npm team seems to be slowly adding support to commands one by one. Follow updates here: https://github.com/npm/cli/blob/latest/CHANGELOG.md.
I'm also baffled with why npm workspaces has been released without this functionality.
My current workaround uses the add-dependencies package, which adds dependencies to a declared package.json file, whilst skipping the installation process.
npm i add-dependencies -g
Then, from top level of the monorepo, you can run:
npx add-dependencies ./submodule0/package.json somepackage && npm i
Hopefully a --workspace argument will be added to npm i soon to avoid this faff.
Please refer to the answer of mattwad above if you have NPM v7.14.0 or above
Original answer
I wasn't quite happy with the suggestions, but combined all of them to use it in a npm script without any dependencies:
{
"add": "npm install --package-lock-only --no-package-lock --prefix",
"postadd": "npm install"
}
This can be used like following: npm run add -- submodule0 somepackage
Add only into package.json
U can use this to install package only into package.json ( you don't need external dependencies )
npm i --prefix packages/test --save --package-lock-only --no-package-lock express
followed by npm i to install specified dependency into mono repository root node_modules
Lerna
Also can use lerna to use workspace name to install dependency into
package.json
{
"name": "mono",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"wsi": "function workspaceinstall() { ( scope=$1; shift; lerna exec --scope \"$scope\" -- npm install --package-lock-only --no-package-lock \"$#\") }; workspaceinstall"
},
"author": "",
"license": "ISC",
"workspaces": {
"packages": [
"packages/**"
]
}
}
lerna.json
{
"version": "1.0.0",
"npmClient": "npm",
"packages": ["packages/**"]
}
npm run wsi [workspace name] [dependency name to install]
npm run wsi #workspace/test express
npm run wsi #workspace/test express --save-prod
npm run wsi #workspace/test #types/express --save-dev
wsi script only modify package.json for provided workspace name, to actually install dependencies u have to run npm i
In my case, which is similar to yours, I deleted all dependencies from all the inner projects, deleted also the package-lock.json, and installed everything in the root.
/
node_modules
package.json >> all dependencies
package-lock.json >> the only lock file that exists in the repo
/packages
/A
package.json >> no dependencies
-- no package-lock.json
/B
package.json >> no dependencies
-- no package-lock.json
/C
package.json >> no dependencies
-- no package-lock.json
This way, the node_modules folder ONLY resides on the root, and also the package-lock.json file is in the root.
If I allowed to have each project it's own package-lock.json I started seeing installation and runtime errors (because each project could have its own node_modules and its own version of a dependency).
This is the best way I see it works.
After trying to use the npm install with the --prefix --save --package-lock-only --no-package-lock options, npm always give the the error E404 - Not Found for my own packages of the monorepo that are not yet published to a registry. So even when trying to install external packages it fails because of my current dependencies in the package.json.
To workaround this issue I ended up with a mix of the previous suggestions:
"scripts": {
"add": "add-dependencies $npm_config_scope/package.json",
"postadd": "npm i",
},
"devDependencies": {
"add-dependencies": "^1.1.0"
},
Then I can do:
npm run add --scope=packages/app express
npm run add --scope=packages/core eslint jest -D
This works fine for installing external packages. To install my own packages that lives inside the monorepo, I still have to manually edit the package.json, otherwise I get the package not found error.

Execute script after package installation

Now, I there is postinstall to run a script after
npm install
However, I am looking for a way to run a script after installing a single package
npm install jquery --save
Is this possible? If so, does this work on Windows and is there a way to get the name of the installed package (jquery in the given example)?
I'm don't see this on the package.json features.
It is therefore possible to do in the prestart for instance but jquery won't be in the devDependencies that way:
{
...
"devDependencies": {
"bower": "1.3.x",
"uglifyjs": "2.4.10",
... your other dependencies
},
"scripts": {
"prestart": "npm install jquery##.#.# ; <yourcommand> ; npm install",
...
}
}

How to run `npm install && bower install` before `sbt compile` for Heroku Deployment?

I am working on a Playframework project which has front-end codes in sub-directory ./ui and managed by Grunt using https://github.com/tuplejump/play-yeoman
Currently I used https://github.com/heroku/heroku-buildpack-multi and set
https://github.com/heroku/heroku-buildpack-nodejs.git
https://github.com/heroku/heroku-buildpack-scala.git
in the .buildpacks file.
And set
{
"name": "scala-grunt",
"dependencies": {
"grunt-cli": "0.1.13"
},
"devDependencies": {
"grunt": "~0.4.5"
},
"version": "0.1.0",
"engines": {
"node": "~0.10.21"
}
}
in the package.json file of root directory.
However, when I pushed the code base to heroku it will throw an exception Fatal error: Unable to find local grunt. I think that is because sbt doesn't to run npm install && bower install in the ./ui directory.
Does anyone have ideas about how to run a command npm install && bower install before sbt compile in heroku?
Check out https://docs.npmjs.com/misc/scripts. There are a couple keywords you can use to run bower and grunt at different times. Check out the preinstall and postinstall keyword.
For an example, here is my script section from my package.json file that I use a lot.
"scripts": {
"start": "node lib/app.js",
"postinstall": "bower install --allow-root",
"test": "grunt"
}
This command solved for me:
heroku buildpacks:set https://github.com/heroku/heroku-buildpack-multi.git

How can I invoke npm on heroku command line (to install bower components)?

Bower is for client side Javascript what npm is for the server side and reads a component.json file to recognize dependencies that should be fetched at deploy time so I'd be happy it heroku would run it at slug compilation time.
Unfortunately I can not invoke npm or bower from a heroku console or one-off command (heroku run "npm help") (heroku run bash -> npm help) as it's possible with ruby's rake. I've put npm and node (latest/x versions) in my package.json but in the engines section, not the dependencies.
I think this could be solved by customizing the node buildpack but I consider this a little too heavy task just for activating something so obvious.
You can also setup a postintall command, something like this in your package.json
"dependencies": {
"bower": "0.6.x"
},
"scripts": {
"postinstall": "./node_modules/bower/bin/bower install"
}
Then npm install will also install bower dependencies.
Pros : one command to rule them all.
Cons : you unnecessarily embed bower as a dependency.
You can use run like this:
heroku run npm install git://github.com/webjay/kaiseki
You should declare NPM dependencies in the package.json file
All you install from shell will be deleted on exit shell. You are in a cloned instance.
You can use bower directly like this
"dependencies": {
"bower": "^1.7.9"
},
"scripts": {
"postinstall": "sudo bower install --allow-root "
}

Resources