package.json for global module installation - node.js

I have a package.json in which some modules have to be installed globally,below is the part that i added below dependencies
"scripts": {
"preinstall": "npm i -g supervisor",
"preinstall": "npm i -g forever"
}
But when i ran npm install i got the error as below
npm WARN package.json aaa#0.0.0 No README.md file found!
npm WARN cannot run in wd aaa#0.0.0 npm i -g forever (wd=/home/administrator/AAA)
All the modules to be installed locally are getting installed,but only gloal installation is throwing an error.I am stuck here any help will be much appreciated.

Put this in your package.json :
"config":{
"unsafe-perm":true
}
And install your module as root.
Also I think that the switch preferGlobal : Documentation, is created for modules that prefer to be installed globally. You might want to change your program logic to use programatically forever module.

Install your modules locally and then execute them via the bin folder in the local node_modules directory
npm install -S forever
ls node_modules/.bin/
To start your app execute
node_modules/.bin/forever start app.js

Related

BrowserSync: command not found after installing locally

I ran the following command for my node app:
$ npm install browser-sync --save-dev
Installation was successful, browser-sync appears in my package.json file as well as my node_modules directory.
However, when I run $ browser-sync --version to check that it's working, I get the following error:
bash: browser-sync: command not found
Why isn't this working?
Note: this question is similar, but I don't want to have to install it globally as in this question.
Any help would be greatly appreciated!
This is because you're trying to use a module locally which is normally installed globally. Modules installed globally end up on your PATH environment variable, which is why you can run them from the terminal as you're trying to do:
$ browser-sync --version
If you want to use the browser-sync module from a local install you will have to prepend the full path to the browser-sync binary from within your .bin directory since all locally installed modules are placed within your current working directory node_modules directory. i.e. Node modules go in ./node_modules, executables go in ./node_modules/.bin/. So in order to run the browser-sync binary from a local install do the following:
./node_modules/.bin/browser-sync --version
Hopefully that helps!
If you installed browser-sync using npm --save or npm --save-dev you can run it by writing a script in your package.json. Here's an example of a script I added:
{
...
"scripts": {
"dev-server": "browser-sync start --server 'public' --files 'public'"
},
...
}
You can run the scripts from you project's root directory like so
npm run dev-server
This will run whatever command is set to dev-server in your script. In this case it will run browser-sync for the app/site in a folder called /public and watch for any file changes in the /public folder. I know this question is a bit old but it was unanswered and hopefully I can save someone time in the future.
The other answers still work, but a newer approach has emerged since npm added the npx command: npx <package-name>.
This command allows you to run an arbitrary command from an npm
package (either one installed locally, or fetched remotely), in a
similar context as running it via npm run.
Source: https://docs.npmjs.com/cli/v8/commands/npx
In this case, you would run npx browser-sync.

Nodemon for development environment

I wanted to know how to use nodemon, and push it to a git repo, and have other developers on the project be able to use nodemon without having to run the command npm install -g nodemon. Ideally, I would like all developers on the project to be able to just run npm start and nodemon is called whether or not it's installed globally. I've already run npm install --save-dev nodemon, and I'm mostly curious if there is a way to get nodemon to be run from within node_modules, in my start command in the scripts section of the package.json file.
If you install it locally, i.e. without the -g flag, it's available in ./node_modules/.bin/nodemon. So just configure that path in your npm start script.
For example:
"start" : "./node_modules/.bin/nodemon app.js"

npm packages not available when installed locally

I am working with npm on a web app and I found an issue when using some packages that requires terminal commands to run such like nodemon and concurrently
I installed it via
sudo npm install --save-dev nodemon
and when I try to use it via:
nodemon ./server.js
I get an error
nodemon command not found
and the same when I used concurrently
I tried also with
sudo npm install --save nodemon
and it doesn't work.
it only work if I installed it globally
sudo npm install -g nodemon
Why I can't use it when install locally?
Note: I can found the executable file at node_modules/.bin
but this following not working as well
node_modules/.bin/nodemon ./server.js
Global packages can be launched directly because they are saved in your PATH directory by default. If you saved a package locally you can see it on node_modules/.bin/ as you mentioned. So there are 2 ways to achieve what you want if you want to run an executable package if installed locally:
You can run it via terminal as ./node_modules/.bin/nodemon yourscript.js
Or via npm scripts in your package.json file, you do this:
{
"scripts": {
"nodemon": "nodemon yourscript.js"
}
}
and execute npm run nodemon.
The 2nd approach works for both packages installed globally or locally.
I prefer installing packages locally, so my other apps won't get affected especially if I'm using different package versions per project.
UPDATE
On npm#5.2.0 onwards, it comes with a binary called npx. So you can run specific packages on the terminal just by npx [package] and it executes either your local or global npm package. In your case it should be something like npx nodemon server.js.
Because it's in your node_modules/.bin folder, not your PATH.
You can either use ./node_modules/.bin/nodemon or $(npm bin)/nodemon to call nodemon.
To run any locally installed npm module (Mocha, Eslint, Nodemon, etc.), you can now use npx. Try npx nodemon server.js.
I also recommend setting main within your package.json to point to the script you want to run (index.js by default), so you could just run npx nodemon or nodemon (if globally installed) and it will know which script to run.
This is because the local node_modules folder is not in your PATH. See the link to the duplicate question for more details.

Node-sass is not recognized by command line

I'm trying to set up node-sass, following the instructions on CSS-Tricks. Node and npm are installed correctly, and the node-sass installation worked too. When I go to run node-sass --output-style compressed -o dist/css src/scss, though, I get an error message stating
'node-sass' is not recognized as an internal or external command,
operable program or batch file.
I've done a fair bit of Googling and searched Stack Overflow directly. My question isn't about "node" not being recognised as a command. I know node is working as I can run node -v and npm -v, and node-sass was successfully installed after running npm install --save-dev node-sass (there's a folder in node_modules) and no errors appeared in the command line.
Other information: I am running Windows 10 and just did a clean install of node and npm before trying to use node-sass.
EDIT: I uninstalled and reinstalled with -g thanks to #Bhavik's suggestion, and it's now working
You need to install it globally
npm install -g node-sass
Or add it in package.json
"devDependencies": {
"node-sass": "4.5.0"
},
"scripts" : {
"node-sass": "node-sass --output-style compressed -o dist/css src/scss"
}
And then do
1. npm i, which in this case would be similar to npm install --save-dev node-sass
2. npm run node-sass
Reference: npm scripts, npm-run-scripts
You can simply run this code
npm install -g sass
sass --watch sass:css
Hopefully work
npm commands check "node_package" folder and try to run things there. You can try
npx run scss
to install scss and then run it, even if it is not installed before.
The below solves the problem
yarn global add node-sass-chokidar
node-sass v4.13+
Install node-sass in your project locally
cd <root path of your project>
yarn add -D node-sass
// or
npm install -D node-sass
Add a script to your package.json
"scripts" : {
...
"compile:sass": "node-sass --recursive --watch <sass directory> --output <css directory>",
...
}
Run the script from the command line
yarn compile:sass
// or
npm run compile:sass
This is a simple problem don't worry too much. Just go to package.json file and add this code
"devDependencies": {
"node-sass": "4.9.2"
},
"scripts" : {
"node-sass": "node-sass --output-style compressed -o dist/css/ scss --recursive"
}
and just save the file.
And run this command,
npm run node-sass
That's all
First, run npm install -g node-sass as others have pointed out.
Now, the target of the command (sass.cmd) is not located in the current working directory. For it to still be able to run, its location must be in your PATH (or Path) environment variable.
For me, the path is: C:\Users\Guy\AppData\Roaming\npm
Make sure to restart any terminal/IDE you were trying to run it in before trying again. Otherwise it won't recognize the new environment variable.

webpack command not working

I am new to Node Js and Webpack. I tried to start a project with module-loaders.
Firstly, I installed nodeJs and NPM and created a new directory called tutorial. I used the command prompt to cd into this directory and then ran the following command npm init and then installed webpack via npm using the command below :
npm install -S webpack
The 1st command installed webpack locally into the project under the 'node-modules' directory and I can run my project by doing this:
nodejs node-modules/webpack/bin/webpack.js
The problem with this is that I have to place my webpack.config.js file inside of this directory which I want to place in my project root.
One solution to this problem was to install webpack globally on my machine which I did using the command below :
npm install -g webpack
This installed Webpack and now I do have a Webpack command. However, this command does not seem to be working or doing anything at all. When I try to run this from my project's root directroy it does not do anything at all (See Screenshot)
Please tell me what I am doing wrong!!
webpack is not only in your node-modules/webpack/bin/ directory, it's also linked in node_modules/.bin.
You have the npm bin command to get the folder where npm will install executables.
You can use the scripts property of your package.json to use webpack from this directory which will be exported.
"scripts": {
"scriptName": "webpack --config etc..."
}
For example:
"scripts": {
"build": "webpack --config webpack.config.js"
}
You can then run it with:
npm run build
Or even with arguments:
npm run build -- <args>
This allow you to have you webpack.config.js in the root folder of your project without having webpack globally installed or having your webpack configuration in the node_modules folder.
You can run npx webpack. The npx command, which ships with Node 8.2/npm 5.2.0 or higher, runs the webpack binary (./node_modules/.bin/webpack) of the webpack package.
Source of info: https://webpack.js.org/guides/getting-started/
I had to reinstall webpack to get it working with my local version of webpack, e.g:
$ npm uninstall webpack
$ npm i -D webpack
npm i webpack -g
installs webpack globally on your system, that makes it available in terminal window.
The problem with my setup was webpack was installed but webpack-cli was missing
npm i -g webpack webpack-cli
If you prefer to install locally then install without -g flag
The quickest way, just to get this working is to use the web pack from another location, this will stop you having to install it globally or if npm run webpack fails.
When you install webpack with npm it goes inside the "node_modules\.bin" folder of your project.
in command prompt (as administrator)
go to the location of the project where your webpack.config.js is located.
in command prompt write the following
"C:\Users\..\ProjectName\node_modules\.bin\webpack" --config webpack.config.vendor.js
Installing webpack with -g option installs webpack in a folder in
C:\Users\<.profileusername.>\AppData\Roaming\npm\node_modules
same with webpack-cli and webpack-dev-server
Outside the global node_modules a link is created for webpack to be run from commandline
C:\Users\<.profileusername.>\AppData\Roaming\npm
to make this work locally, I did the following
renamed the webpack folder in global node_modules to _old
installed webpack locally within project
edited the command link webpack.cmd and pointed the webpack.js to look into my local node_modules folder within my application
Problem with this approach is you'd have to maintain links for each project you have. Theres no other way since you are using the command line editor to run webpack command when installing with a -g option.
So if you had proj1, proj2 and proj3 all with their local node_modules and local webpack installed( not using -g when installing), then you'd have to create non-generic link names instead of just webpack.
example here would be to create webpack_proj1.cmd, webpack_proj2.cmd and webpack_proj3.cmd
and in each cmd follow point 2 and 3 above
PS: dont forget to update your package.json with these changes or else you'll get errors as it won't find webpack command
Actually, I have got this error a while ago. There are two ways to make this to work, as per my knowledge.
Server wont update the changes made in the index.js because of some webpack bugs. So, restart your server.
Updating your node.js will be helpful to avoid such problems.

Resources