Managing client side scripts with npm - node.js

Is it possible with NPM to manage the same dependencies for backend and the client-side scripts? I'm building a node.js application with express. When installing all dependencies, those scripts are installed into the node_modules folder. Is it possible to somehow tell express that it should look in those folder for javascript files for the client? E.g. when the client requests the file underscore.js, it should return the file from the installed module. Or is it possible to hook in to the npm installation procedure so that some files can be automatically copied to the public folder of the application?

You can hook into NPM after it installs the packages required by your application using postinstall in the scripts field in package.json:
install, postinstall: Run AFTER the package is installed.
For more read here: http://npmjs.org/doc/scripts.html

Related

How do Node.js and Laravel fit together

I'm doing the courses at Laracasts and the command npm install && npm run dev is used. This didnt work from my htdocs/projectname directory so I installed the windows 10 node.js installer, which installed it to a program files directory.
Somehow, it seems node.js ended up in my packages.json
Node.js seems to be all javascript on the server side? How does this work with Laravel?
Thanks!
NodeJS can be used for many things, which include server-side code, as well as provides tools to be used in a development environment, which are available through the NPM Repository. A quote from the NodeJS Wikipedia page states the following:
Node.js is an open-source, cross-platform, back-end, JavaScript
runtime environment that executes JavaScript code outside a web
browser.
A standard Laravel installation includes a package.json file as standard, which includes the commands and requirements for various packages, one of which includes laravel-mix, which is a webpack based tool for optimizing and compiling your assets (Typically Stylesheets and Javascript).
NPM ist the Node package manager. It's installed with Node.js.
You can use npm -v in the CLI to check if it's installed correctly.
In your project directory must be a package.json to use npm install && npm run dev.
In case of Laravel a package.json should be in the directory.

How much damage can I do by installing a module in the wrong place with npm?

I've got an iOS app, using google cloud functions and want to install the Request module/package https://www.npmjs.com/package/request in the node_modules folder.
My folder structure is:
- Desktop
- myApp
- firebase
- functions
- node_modules
I was reading up on npm about npm install and there was some stuff about installing locally, globally and some other things I didn't quite get.
Is there a standard way to install packages with npm?
And if I wanted to install "Request" in the terminal do i "cd" to the node_modules folder and npm install from there or is it from within the functions folder?
Thanks.
In the same directory where you have the node_module directory (i.e. in your functions directory) you should also have package.json file. If you are in that directory and you run npm install request --save then you will install the module and put it in the dependencies in package.json file. That is what installing locally means. For more info see:
https://cloud.google.com/functions/docs/writing/dependencies
(Cloud functions for Firebase work pretty much the same as Google Cloud Functions)
Update
The --save is the default behavior of npm install since v5.0 so you don't need to use the --save flag since the saving is done automatically (there is --no-save to prevent saving).
See the release notes for more info:
https://github.com/npm/npm/releases/tag/v5.0.0

Do I Need "node_modules" Folder on Live Server

This may be a silly question but I have installed node on my computer and have been running tasks for creating my sass which means I have a node_modules folder within my project folder and I'm wondering if I need to upload the node_modules folder to my live server or not?
Tipically you must:
To have installed Node.js in the server (and npm).
You generate packaje.json in local. npm init. You execute this in a folder containing node_modules.
When package.json is generated. You upload it to the server.
When package.json is in the server, specifically in your project folder, you install all dependencies with npm install
You should make sure that your server had installed node.js if you want to run node.js project on your server and you can use 'npm install' to install the modulars of the package.json.

How to do a manual install of Node js package dependencies

I am rather new to Node but am working on a project behind a very restrictive firewall so I cannot use npm to install packages (no proxy either). I am trying to use express and have been able to include it by just storing the files locally and requiring the local file path but I do not know how to structure the project so that node can find and include all of the dependencies for express (which I also have manually downloaded from github and stored locally within the project). Does anyone know how to do a completely manual global or local install of individual node dependency packages?
Simply download the files and place them in a 'node_modules' folder in the root of your app. Then use 'npm init' to create a package.json. Add your dependencies in the dependencies list in that file.
Then run simply 'npm install'.

Where does meteor install npm packages referenced in packages.json?

After meteor installs npm packages:
npm: updating npm dependencies -- winston...
Npm.require results in module not found error, by inspecting the code and debugging via node-inspector, I discovered that putting a standard node_modules folder (installed via npm install) in .meteor folder in the root of my meteor app folder gets it to find it.
However when deploying to meteor.com or using any automated build-pack (like Heroku's) this might not be possible, I would rather have a scenario where I can link the automatically downloaded modules to the paths Npm.require looks in.
Any idea where it downloads the packages?
Thanks
For meteorite packages this would be:
/path/to/your/project/packages/package_name/.npm/package/node_modules/
For core meteor packages I guess this is
~/.meteor/packages/package_name/hash/npm/node_modules/

Resources