If I install http-server locally (without -g flag) how can I run it with local directory as root directory?
In my root project directory I have a node_modules folder, If I want to execute http-server I need to execute $node ./node_modules/http-server/bin/http-server.
I wish to launch http-server with a command like $npm http-server.... If I use $npm start http-server, the server start without ./ as root.
You can call the executable that sits in the module's bin folder like so:
> ./node_modules/http-server/bin/http-server
If you want to do it with an npm command, you can put that in your package.json's scripts collection:
{
"name": "tmp",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start":"./node_modules/http-server/bin/http-server",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Then you can just run
> npm start
Related
I want to write my own npm package to analyse the structure of a vue project (vueanalyser). So I created a new package with npm init --scope=#my-username and set the "main" property to index.js.
// package.json of the custom package
{
"name": "#my-username/vueanalyser",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"repository": {
"type": "git",
"url": "..."
},
"bugs": {
"url": "..."
},
"homepage": "...",
"description": ""
}
I published the package and added it to my vue project (.node_modules/#my-username/vueanalyser). Now I want to add a command like "analyse": "vueanalyser start" to the script property of the package.json of the vue project. If I do so I obviously get an error, that the command is unknown.
So I realized, that I can call the index.js with "analyse": "node node_modules/#my-username/vueanalyser/index.js, but I have seen packages where scripts can be called with a much shorter way e.g. "styleguide:build": "vue-styleguidist build". What do I have to change in order to call my script this way ("analyse": "vueanalyser start")?
the npm bin property
This specifies executables to copy into node_modules/.bin.
Add the executable header to your index.js
Add this to the first line: #!/usr/bin/env bash
Make the file executable
chmod +x index.js
Edit package.json
Add the bin property:
{
...
"bin": { "vueanalyser": "index.js" }
Republish package
Install package
Find node_modules/.bin/vueanalyser is a symlink to ../<package_name>/index.js!
(having node version 12 and npm version 6)
Backend: Node.js
Front: React.js
i cloned repository
cd into the directory
ran
npm install (installing dependencies etc...)
and when i ran
npm start
and i get
[Ben#Mac:~/Desktop/test]$ npm start
> answers-entry-level-exam#1.0.0 start /Users/Ben/Desktop/test
> lerna run start --parallel
lerna notice cli v3.22.1
lerna info Executing command in 1 package: "npm run start"
#ans-exam/server: > #ans-exam/server#1.0.0 start
/Users/Ben/Desktop/test/server
#ans-exam/server: > ts-node-dev index.ts
#ans-exam/server: Using ts-node version 8.5.2, typescript version 3.7.2
#ans-exam/server: server running 3232
which seems like the server is running okay
but localhost:3000 cant be reached
not opening anything
the first time it did open and there was a MacOS popup on the right side of the screen related to node ( i think that is the issue but cant figure out how to fix)
my package.json:
{
"name": "answers-entry-level-exam",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"b": "npm run bootstrap",
"bootstrap": "lerna bootstrap",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "lerna run start --parallel",
"postinstall": "npm run bootstrap"
},
"author": "",
"license": "ISC",
"devDependencies": {
"lerna": "^3.22.1"
}
}
Thanks!
I cloned again the repo and it fixed that
Cheers to all those who tried to help :)
Try localhost:3232. I see in your output server running 3232
I am setting up my local module to be installed via npm install --save ../path/to/my/project.
When running the npm install, the node_modules/my_project directory gets populated with all source files - not just the dist/ folder as configured in package.json. I'm not sure why.
Here's my setup:
Test Project (main project)
Files:
index.js
package.json
dist/
index.js
package.json
{
"name": "test",
"version": "1.0.0",
"description": "a test",
"main": "dist/index.js",
"files": [
"dist"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"test"
],
"author": "Me",
"license": "MIT"
}
-
Test Project2 (to import main project as a dependency)
package.json
{
"name": "test2",
"version": "1.0.0",
"description": "test2",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Me",
"license": "MIT",
"dependencies": {
"test": "file:../test"
}
}
Once I run npm install, my node_modules/ directory looks like this:
test/
index.js
package.json
dist/
index.js
Is there something I'm overlooking? I'm under the assumption that the files: [] field is used to specify which files are included when the package is installed.
Additionally, I've tried running npm cache clean -f and npm cache verify, neither of which solve my issue.
It seems that this only occurs when attempting to install a local module. To test, I made a test repo on github and installed from there, only the dist/ file was included. Not sure why.
I built simple command line application using commander.js for Node.js platform.
Now I want to compile it to simple exe file, Which I can execute directly.
Means I want single executable file for complete application
This is my application structure
APP_ROOT
| - package.json
| - node_modules
| - node_modules/.bin/myapp.bat
| - node_modules/myapp/bin/myapp
| - node_modules/myapp/bin/myapp-action1
| - node_modules/myapp/bin/myapp-action2
Thanks
This is, How i packages my node.js command line app to single executable
Install pkg module using npm i -g pkg
This is my package.json File
json
{
"name": "my-app-exe",
"version": "1.0.0",
"description": "Myapp-Cli tool as executable",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"myapp",
"cli",
"exe"
],
"author": "Shisht",
"license": "MIT",
"devDependencies": {
"myapp": "1.0.0"
},
"bin": "node_modules/myapp-cli/bin/cli",
"pkg": {
"assets": "node_modules/**/*"
},
"help": "pkg . --target host --output myapp-1.0.0-x64.exe --debug"
}
Command used to package myapp to myapp.exe
pkg . --target host --output myapp-1.0.0-x64.exe --debug
It's impossible to run a Node application without some kind of Node runtime to run it on - therefore, if you wish to distribute your program as a standalone .exe, you will have to bundle Node itself into said executable as well as your code. There are various tools that will do this for you, such as EncloseJS.
I'm building a project which I have multiple instances of running, with the exact same code, using forever.
With forever, it's possible to set an id, using --id "id".
{
"name": "project",
"version": "1.0.0",
"description": "Test",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"forever": "forever start --id $1 -c \"npm start\" ./"
},
"author": "Renzo <renzo#geex.company>",
"license": "ISC"
}
This way, I'm trying to make a standard for different app instances like:
npm run forever cms
The idea is that this would start the app with id cms.
But instead, it lists the app with id true.
Can anyone help me?
Try changing this:
npm run forever cms
to this:
npm run forever -- cms
but you'd also have to change the order of arguments in package.json.
Also, you can put this in a script, like forever.sh:
#!/bin/sh
forever start --id $1 -c "npm start" ./
and this in package.json:
"scripts": {
"forever": "sh forever.sh"
},
and then use this from the command line:
npm run forever -- cms
To make it work on Windows see these answers:
Why does `DEBUG=foo node index.js` fails in `scripts` section of `package.json`
'bash' is not recognized as an internal or external command
NPM script under cygwin/windows: the syntax of the command is incorrect
This is more of a workaround than an answer, but for now I'm doing it like this:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"forever": "forever start --id $ncmsfid -c \"npm start\" ./",
},
And starting it with ncmsfid=cms npm run forever