Running a custom node module in scripts from packege.json - node.js

I have build a node module that works, module name my-module.
When I install this module to a bigger project I want to run it in the packege.json.
This works like this:
"scripts": {
"myModule" : "node ./node_modules/my-module"
}
Is there a way to avoid the reference to node_modules?
Having the scripts like the following:
"scripts": {
"myModule" : "my-module"
}
Thank you all for the help
Moshe S.

Add to your "my-module" package.json a "bin" field:
"bin": {
"my-script": "./path-to-your-executable/my-module-script.js",
"other-script": "./index.js"
}
And in your bigger project you can do
"scripts": {
"start-my-module": "my-script", //Will look for "my-script" at ./node_modules/.bin/
"start": "other-script"
}
And you can call your scripts from console in the regular way:
npm run start
This will execute index.js from "my-module".

Related

Single npm package with two commands

I have a node.js package described by this JSON :
"name": "mycommand",
"main": "index.js",
"bin": {
"mycommand": "./index.js",
The index.js file contains this code :
#!/usr/bin/env node
const app = require('./src/app.js')
const { Logger } = require('./utils/Logger')
app.init()
And the app.js contains the code of a command line tool based on yargs.
Now, I would like to add a 2nd command in this package, but I don't know how should I proceed since there can be only one "main".
Anybody has an example somewhere ?
Maybe you are looking for scripts instead of bin?
Now, I would like to add a 2nd command in this package, but I don't know how should I proceed since there can be only one "main".
The bin mapping has nothing to do with your main file.
Your command is getting symlinked, you can simply add more:
"bin": {
"mycommand": "./index.js",
"my-other-command": "./other-command.js",
Or you can use your mycommand and parse the arguments in the script app.js using process.argv:
https://nodejs.org/en/knowledge/command-line/how-to-parse-command-line-arguments/

Is there any way to open nw.js app from web browser?

I have a desktop application, packaged using node-webkit JS. Is there any way to open this app with IP address from other computer by browser? I just set node-remote to http://localhost:3000 in package.json but is not working when I use chrome and open the IP. There are some errors like nw is not defiend and etc. Please tell me if this way can work or not. Thanks
I don't know nwjs but if I understand correctly, you want to access to localhost in your computer from another computer.
you can not use ip to access because of NAT. but fortunaly you can do that by a third computer (that is not behind NAT). to do that use localtunel.
for more info see this.
Do you think you could paste what your package.json looks like? I've done what you are talking about. Here is what my file structure looks like:
!(https://i.imgur.com/L3M6lvx.png)
The package.json that is in my project folder:
!(https://i.imgur.com/uZV7mzr.png)
The 1st thing that I did was install my dependencies into my project folder so that I don't get the command not recognized error. I did that by going to my project folder and typing:
npm init -y
npm install nw#0.50.1-sdk nwjs-builder -D
This creates a fresh package.json and adds the modules to the file as dependencies. Then I went into my src folder and created another package.json. I set the "main" tag to my index.html
Going back to the .json in my root project folder, we add to the "script" tag:
"script": {
"dev": "nw src/ --remote-debugging-port=9222"
}
(you can make dev whatever you want)
Once you have that setup, all you need to do is run npm run dev and your app will open up. Head over to chrome and type localhost:9222 and you should be set.
It is possible to create an app that can run in a regular browser, and also in NW.js with added features when it runs inside NW.js. You would need to basically wrap anything in if statements, like
if (window.nw) {
let fs = window.nw.require('fs');
let file = fs.readFileSync('./whatever.txt');
console.log(String(file));
}
You could then create two different npm scripts. One to just run a local web server and one to run it and launch NW.js.
{
"main": "http://localhost:4467",
"node-remote": "http://localhost:4467",
"node-main": "server.js",
"scripts": {
"start": "concurrently \"npm run serve\" \"wait-on http://localhost:4467 && nw .\"",
"serve": "node server.js"
},
"dependencies": {
"express": "latest"
},
"devDependencies": {
"concurrently": "latest",
"wait-on": "latest"
}
}
Example: https://github.com/nwutils/nw-local-server-example

How to run full-icu with nodemon?

When I try to run full-icu with nodemon, it doesn't add the localization support as expected.
I've tried to include full-icu via an environment variable, which I load with dotenv, then via CLI, none had worked yet. I'm running Node.js 12.3.1, nodemon 1.19.1, full-icu 1.3.0.
CLI:
const gulp = require('gulp');
const {spawn} = require('child_process');
gulp.task('nodemon', () =>
{
const {stdout, stderr} = spawn('nodemon.cmd', ['--icu-data-dir=/node_modules/full-icu']);
//...
});
.env:
NODE_ICU_DATA=/node_modules/full-icu
Also, I've tried to remove the first slash or include the absolute path without any success.
When I run:
console.log(Intl.NumberFormat.supportedLocalesOf('sk');
The output should be ['sk'], but is actually [] for any language other than English.
I've solved it by changing "start" in "scripts" in package.json from:
"scripts": {
"start": "node ./app"
}
to:
"scripts": {
"start": "node --icu-data-dir=node_modules/full-icu ./app"
}
The problem with the CLI was probably a wrong order of the variables because --icu-data-dir was preceded by ./app. And I guess the problem with the environment variable was that I'd added it after the initialization of the process and Node didn't check it afterwards. If that's not the case, I'd like to be corrected.

node.js Command Line Interface app on windows

I am creating a simple CLI (Command line interface) application using NodeJs, involving two files:
package.json:
index.js
I want to print "hello world" to STDOUT and it is working when running command $ node index.js
But I want to use it globally via command test. So, I put a bin entry in package.json. Then build the application using npm link .. But then when I run "test" command, Windows shows me the following error:
How can I use console.log in separate app?
Thank you!
In package.json file, you need to write the code as follows:
"name" : "test",
"version": "1.0.0",
"scripts": {
"start": "node index.js",
"build": "webpack",
},
...
...
after this, use the command npm start to run the application.
1- Create bin folder in root folder and place your index.js inside the bin.
type the shebang code on the first line of index.js:
bin/index.js
#!/usr/bin/env node
console.log("hello world");
2- Add below code into the package.json file.
"bin": {
"test": "./bin/index.js" //this is relative path
}
3-finally run this code on command line
npm link
now you run "test" in your command line, it will log "hello world"
note:pay attention to the relative path that i mentioned above.

How to run basic hello world app using electron framework , node.js on windows platform

I have created the package.json, main.js and index.html files, stored them all in the folder, but when I try to run the application the following errors are displayed.
There could be two solutions if you want to use npm start (fixing either them should be enough):
No start script in package.json
No server.js file
Number 1 solution:
Do you have a start script in your package.json file? - as it looks like you may be missing it.
To fix this, go into your project directory, and open up your package.json file, and add the following script (replacing your-file with your actual file name):
"scripts": {
"start": "node your-file.js"
}
Number 2 solution:
Change your application name (e.g. myapp.js) to server.js, and try running npm start again.
OR SIMPLY... just run the node myapp.js (replacing myapp.js with your actual file name that you want to run) command rather than npm start
Replace your package.json with this one
{
"name" : "arun_electron",
"version" : "0.1.0",
"main" : "main.js",
"devDependencies": {
"electron": "^0.4.1"
},
"scripts": {
"start": "electron main.js"
}
}
when you run npm start, the command electron main.js will be executed.

Resources