Is it possible to run a script on a node.js dependency? - javascript

I'm working on a node package which has an installation script to set up a simple application structure. It's not doing anything too strenuous, just creating a few folders and creating an "admin" user if one doesn't exist.
At the moment it's doing a bunch of checks every time the application starts and does the setup process if required. Is there any way of doing it through the command line? Something along the lines of the user just typing my-package install or npm run my-package-install to call the script?

yes, with npm, you can build a simple commandline tool.
And you can define/parse the args from terminal.
You need to define the script you want to run in your package.json.
"bin": {
"your-command": "bin/commit.js"
}
And run npm link, this would make the command available.
For more detail, check Building a simple command line to with npm
Hope this can answer your question. : )

Related

Is it possible to run React project without npm start?

I'm in a big trouble. Working in a part time in a company they're looking for a new web technology to build "web-component" in their website.
They have started to use AngularJS (first version) and I told them that, with the recent evolution of this framework, it's not the right period of time to deal with it.
That's why I began to be interested in ReactJS. However, they don't have a node.js server infrastructure (and that's why AngularJS suits to them, only one browser is sufficient) so it's impossible to run it with something like "npm start".
SO ! My question is (as my post's title says...) :
Is it possible to run ReactJS without server side ?
I've tried with the following line in my header
<script src="https://unpkg.com/react#15/dist/react.js"></script>
<script src="https://unpkg.com/react-dom#15/dist/react-dom.js"></script>
But it remains a blank page.
Maybe there is something I don't understant in the react structure and that's why I'm looking for some help/explanations from you.
I hope I have been clear enough ! Thank you in advance for answer.
It is absolutely possible to run a React app without a production node server. Facebook provides an easy-to-use project bootstrapper that you can read about here
That being said, developers may need to use a node dev server locally via npm start, as well as using node to perform production builds via npm run build. But one can take the build output from npm run build and serve it from any static server and have a working react application.
For those who are getting 404's after deploying in a sub directory. Make sure to add the path in package.json as homepage.
"homepage": "https://example.com/SUB-DIRECTORY",
You should insert "homepage": "./" into your package.json file, then use building react-script command like npm run build.
I did it by using serve, as part of the build step in Jenkins. To install it, execute the command:
npm install -g serve
Then, to serve it:
serve -s build
Please, refer to project page for more information: https://github.com/zeit/serve

NPM package for NPM scripts

I'm trying to write an npm package that will add a specific npm script to whatever package.json on which it is depended. Nothing in the npm package.json / script docs is bringing me remotely close.
I'm basically trying to do this:
I install an npm package (call it 'cool-thing')
cool-thing, by installing, adds an npm script to my existent package.json
I can call cool-thing on the command line and it will perform whatever action is specified in the package.json
Anyone know how I could do this?
I am assuming you are working on a package cool-parent which depends on package cool-thing because you want to run the executable that cool-thing provides.
Normally, you would manually modify the cool-parent package.json to add a script:
"scripts": {
"cool-thing": "cool-thing"
}
Now for user convenience, you would like 1) to automate this modification. Not only that, you want to 2) make the modification automatically after the package is installed as a dependency.
There are some drawbacks to part 2, the developer of cool-parent
might not want to add a script,
might already have a script called cool-thing,
might not have a package.json,
...
I consider making modification outside of the package itself during install time to be an undesirable side effect.
If you still want to do it, you can using a postinstall script in cool-thing. You would need to figure out the location of the package.json of the parent if there is one through working directories and perhaps environment variables that npm provides.

How to run a Node.JS app that is forked from github?

Before I say anything else, I am a complete noob to node.js and I just want to see what this web application looks like. I noticed this project at a hackathon and I wanted to test it out. They gave the github repo: https://github.com/android-fanatic/Web But I can't run it from my computer. I understand that I would need to use the command prompt and run it from my local server, but can someone give me step by step directions for installing the node.js app?
Again the link is:
https://github.com/android-fanatic/Web
Thanks in advance for any help! :)
Install Node.js
Clone the repository to somewhere on your hard drive
Open a command prompt and go to that directory
Type npm install to install any dependencies
Type npm start
???
PROFIT
The reason you can use npm start is because if you look inside of their package.json file you'll see a "start" option under "scripts". That command will execute when you type npm start.
Assuming you have node and npm installed on your system, you should be able to do:
npm install
npm start
The first command will install the node package dependencies and the second will run the server.
Note: I have never used this project, so my guess is based on looking at the repo alone.
The web application should be available at:
http://localhost:3000

Writing a script to automate npm command

I am working on an angular js project and I would like to automate The following two commands.
./node_modules/protractor/bin/webdriver-manager update
./node_modules/protractor/bin/webdriver-manager start
The issue is that I am working on a small angular project on github. I added all dependencies required to my package.json, However when my friend pulled it from git he was able to install protractor but he could not get webdriver to start unless he ran the above two commands. So i wanted to write some script to automate it and better yet even add protractor ./conf.js to it.
So I did research and I am aware that I can write a npm script but I was not able to find a proper document that showed where to write the script and how to execute it. I would appreciate all suggestions.
You can add a scripts property to your package.json with the command you wish you run.
"scripts": {
"prostart": "./node_modules/protractor/bin/webdriver-manager start",
"proupdate": "./node_modules/protractor/bin/webdriver-manager update"
}
you would then run these by typing npm run prostart or npm run proupdate which would look for those commands in your package.json.
In addition to Josh's answer, the script start could be run as npm start as start is a special keyword, but update should be run as npm run update because npm update is another npm command entirely.
For any other command besides start and test (I think), you have to preface it with npm run ...

Create file with command line in Node

So I'm attempting to do this Node.js tutorial, and it says to create three .js files from the command line.
touch server.js client.js test.js
Except I get the following error:
'touch' is not recognized as an internal or external command, operable
program or batch file.
Not sure what is wrong here. I've installed Node.js as well as npm and browserify. I've created the package.json file correctly.
I suppose I could go into the project directory, right click and make a new file that way, but that defeats the purpose doesn't it?
What is the actual command to create a new file in the command line?
I'm using Windows 7.
That command is for a unix environment. You can use the following to create an empty file with similar functionalities that touch has in windows:
echo $null >> server.js in the desired directory
You can use the following command:
echo> index.js
touch is generally present on *nix platforms but not Windows. You will be able to follow the tutorial without it.
The tutorial author is using it to create empty files. You could achieve the same by simply saving files with the same names using an editor.
Alternatively, if you really want to use it in Windows, try Cygwin.
I know this is an old post, but the question is still relevant and there is a way to integrate the touch command into Windows, using the touch-for-windows npm package. It can be installed globally via node/npm with npm install -g touch-for-windows.
As someone who uses a pretty customized terminal across multiple machines, Cygwin didn't provide some of the other features I often use. The echo command (and accepted answer) provided by ltalhouarne works, but I felt was cumbersome to use. This npm package, though old, operates exactly in Windows as the touch command in *nix.
You can use :
copy nul > Gulpfile.js
You can also use the following command:
copy con [filename.extension]
[Note: Don't include square brackets]
That's it!
Follow the link
For installation in Windows
https://www.npmjs.com/package/touch-for-windows
Installation
In command prompt type:
npm install -g touch-for-windows.
Usage
After installing, you can run this application via the command line, as shown below.
C:\pythonapp>touch index.html
Successfully created 'index.html'
Worked for me
cd > filename.txt works too... if u create txt files, then it will have the file path on them. Just remember to delete them.
If you want a cross-platform solution in an environment where you have Node.js installed, you can as well run javascript code with node:
node -e "require('fs').writeFileSync('server.js', '')"
node -e "require('fs').writeFileSync('client.js', '')"
node -e "require('fs').writeFileSync('test.js', '')"
The commands above will create your 3 files with no content. You can also replace the '' by any content you would like to have in the file.
For more complex logics, you can move your code into a javascript file and execute it like:
node path/to/script.js
If you use git, there is already a bash installed inside
c:\Program Files\Git\bin\bash.exe
You can use it to work without installing cygwin. It contains the touch command.
Use the below command example to create any file in cmd:
type NUL > index.js
(here index.js is the file I want to create)

Categories

Resources