How to deploy a stand-alone application as an npm module? - node.js

Is there a way to load an npm module as the main application?
I'm used to using git to load some application like:
git clone https://github.com/me/myapp.git
cd myapp
npm install
npm install always puts dependency modules under the node_modules path.
How would I deploy a stand-alone application as a node module?
Is there something equivalent to:
npm install myapp
cd myapp
npm start

Create a github new project let say myapp
Clone the project
git clone https://github.com/me/myapp.git
Generate the package.json
Update the your code logic
let create file index.js add some code
module.exports = (name) =>name;
Create a npm account
https://www.npmjs.com/signup
login npm
go to the terminal type below the command
npm login
Publish the npm package
npm publish https://github.com/me/myapp.git
Install the package
npm install myapp
Start import the the module
let name =require('myapp')
console.log(name("hello world"));

Related

Running npm build before/after npm install

I'm not familiar with npm so I might be holding the wrong end of the shovel here...
There is a package on npm that I would like to modify and use in my own project. The package is angular-crumbs. I forked the source repo (https://github.com/emilol/angular-crumbs) into my own account (https://github.com/capesean/angular-crumbs) and then run npm install capesean/angular-crumbs -force. However, this produces a node_modules folder in my project that hasn't been built (and whatever else - as I understand it) with the commands in the source repo's package.json file:
"build": "npm run clean && npm run transpile && npm run package && npm run minify && npm run copy"
i.e. it doesn't have the types, the correct package.json file, etc.
So my question is, how do I get the properly-built files (including type definitions, etc.) from my own repo to install or build-after-installing in my target project?
I am not sure about what you're trying to do, are trying to work on the
angular-crumbs source code, or are you trying to use it in your own project as a dependencuy ?
Anyway, running npm install will install all your dependencies so that you can directly use them in your project, those dependencies don't need to be built after they are installed.
In your case you seem to have an angular application (which is completely different from node.js), usually to start an angular app you can run ng serve which will build your source code and run an angular server so you can access it on localhost.

Define custom npm install procedure for azure/webapps-deploy#v2

My project currently contains 2 apps. The first is the application Backend (NestJS) and the second is the client (VueJS).
The current folder structure follows:
Root (NestJS)
./client/ (VueJS)
When I am deploying my app to Azure App Service I am using the azure/webapps-deploy#v2 action. It's procedure is to run npm install in the root of the project but I need it to also run in the sub project containing the client packages. How can this be done? Are there any arguments to provide the webapps deploy action to include that addition npm install command?
You could try add the commands directly in your workflow:
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm run test --if-present
cd ./client/mern_azure_example # go to your client package
npm install # run npm install under your client package
But it takes very long time.

npm - set project specific default prefix/path

I would like npm ... to always run in the context of a subfolder of my project: "./assets".
When I run this command from the project root, it behaves as expected:
$ npm --prefix ./assets install
However, it does not read this from the .npmrc in the root folder.
$ echo "prefix=./assets" > .npmrc
$ npm i
# creates an empty ./node_modules folder
How can I set a set project specific default prefix for npm commands?
If you have install npm globally you can simply configure prefix to set for specific folder so that you can install other node modules to that folder. Following is the command to set prefix.
npm config set prefix /Programming/node-v0.10.31-linux-x64/
here i have configure Node in node-v0.10.31-linux-x64 this folder and node modules will be install there.
or
if you want to install node modules for specific folder individually you can user following command
npm install bower -g --prefix /Programming/node-v0.10.31-linux-x64/

npm install -g is copying all my source code to the prefix folder (Windows)

I'm building a .NET Web Application and using NodeJS for a bunch of build tasks (minification, bundling, unit tests, etc).
My problem is that when I run the following from my source code folder:
npm install -g
It not only checks and installs all of the node packages (great!) .. but it also copies the ENTIRE source code folder over to my global node_modules folder (.cs, .sln, bin folders .. the lot ... not so great!)
Is there any way of stopping it from doing this?
Note - I've redirected the npm and cache folders to a separate path using:
npm config set prefix e:\npm-repo\npm --global
npm config set cache e:\npm-repo\cache --global
This is how npm install -g works.
You can try npm install if you want to install only the dependencies from package.json locally.

Install NodeJS package locally

When i try to install package on my local directory using npm install connect,but it just keep pop up some warning about
no such file or directory, open '/Users/felixfong/package.json'
But i don't not want to install package at my computer directory,i want to install at my local web app directory
Are you sure you are inside your local web app directory when you run the npm install connect command?
cd app-directory/
npm install connect
Also ensure that a package.json file is also present in the app-directory.
If it isn't present, you can use npm init command to create the package.json file interactively.
You have to go inside your project directory using
Then you can check package.json.
If package.json file is not there then initialize npm using the following command:
npm init
Then your can install the package using the following command:
npm install connect
'npm install connect' does not save the connect npm package in package.json file.
For saving the package into package.json file you have to givt --save option like:
npm install connect --save
Make sure that you are in web app's directory. Current path can be checked via command pwd in Linux and cd in windows. Navigate to your web app directory if you are somewhere else. Check existence of package.json by listing the content of the folder. ls and dir can be used for ubuntu and windows respectively for listing content. Commands for ubuntu are as below:
pwd
cd your-path/
ls
Now Initialize npm in your web app directory if package.json is not already existing there.
npm init
This will ask some information like:
name of the app,
its version,
description,
entry point,
test command,
git repo,
keywords,
author and
license (if any)
You can select default values by leaving the fields empty if you aren't sure or confused about any field. Combining this information as json, npm will create a file named package.json
Just run the desired command now after initialization of npm or if its already initialized:
npm install connect

Resources