I want to try using npm to run my various build tasks for a web application. I know I can do this by adding a scripts field to my package.json like so:
"scripts": {
"build": "some build command"
},
This gets unwieldy when you have more complex commands with a bunch of options. Is it possible to move these commands to a bash script or something along those lines? Something like:
"scripts": {
"build": "build.sh"
},
where npm run build would execute the commands in the build.sh file?
Reading through this post it seems like it is, but I'm not clear on exactly where I'm supposed to drop my build.sh file or if I'm missing something.
Its totally possible...
"scripts": {
"build": "./build.sh"
},
also, make sure you put a hash bang at the top of your bash file #!/usr/bin/env bash
also make sure you have permissions to execute the file
chmod +x ./build.sh
Finally, the command to run build in npm would be
npm run build
If you don't want to bother with giving permissions and the env you execute the script has for example sh, you could just do
"scripts": {
"build": "sh ./build.sh"
}
Even Simpler:
I routinely do this for one-offs and PoC's not involving a VCS
package.json
{
"scripts": {
"ship": "rsync -avz deployable/* <some-server>:/var/www/some-site/sub-dir/"
},
}
...
Just add 'bash' keyword before the file_name.sh
"scripts": { "build": "bash ./file_name.sh" }
then run "npm run build" on terminal
In case someone sees this and is using Docker for creating an image of this application.
Regarding:
but I'm not clear on exactly where I'm supposed to drop my build.sh file
Wherever you do decide to drop your build.sh file, make sure it is included in the Dockerfile image output, i.e. the file's path is in one of the COPY commands in the Dockerfile.
For example, if you have something like
scripts {
"build": "./scripts/build.sh"
}
Then your Dockerfile should have a COPY command similar to:
COPY scripts/build.sh scripts/
Related
I am having a Monorepo and using NX build system.
I am trying to figure out a way to inject the project name in my package.json
package.json
"scripts": {
...
"sampleCmd": "env-cmd -f apps/${projectName}/.env.local"
},
I am looking for a way to inject the projectName/appName.
Example:
If I execute yarn sampleCmd user-service
The sampleCmd should be "env-cmd -f apps/user-service/.env.local"
Yarn run command just add arguments to end of script command defined in package.json. It's not possible to get a specific argument, neither to change the order.
But as a workaround, we can create a dedicated script in a scripts folder for example, and reference it from package.json.
Then inside this script, we can get arguments.
for example, in /scripts/sample-cmd.sh
#!/usr/bin/env bash
cd $(dirname ${0})/..
PROJECT_NAME="${1}"
env-cmd -f apps/$PROJECT_NAME/.env.local
Then in package.json :
"scripts": {
...
"sampleCmd": "./scripts/sample-cmd.sh"
},
Of course, this need to be adapted to target environment, system... (bash, linux/mac/windows...). But the main idea is here.
Note this in not related to NX monorepo, and will work for every NPM/YARN project.
I'm a beginner in the field of frontend development and I'm starting to work on building small projects and assignments.
I was wondering how I could add a default script line to every package.json file I create using the npm init command. (I'm on MacOS)
I found out how to set the default author name (which created the .npmrc file in my home directory), however I can't seem to figure out to add the following as a default to all package files:
"scripts": { "start": "live-server --browser=firefox" },
It seems trivial, but as we'll be building out our set-up, there will be certain scripts defaults we'll need in every projects, such as "deploy" settings etc.
Is there an easy way to add this as a default so that this is pre-added with every npm init command?
Or: a classmate of mine created an automator workflow to set-up a new project folder including some starting files and a command that already runs the npm init on that folder. Is there a terminal command to add the above line to the scripts section of the package.json file which we could add to the automator workflow?
You can add custom scripts using the following command in the CLI:
npm set-script dev "nodemon server.js"
Result in package.json:
"scripts": {
"dev": "nodemon server.js"
}
Consult the documentation here.
You can also write it in a shell script file to automate your projects.
I want to put custom script commands while creating package.json using npm init.
npm init only offers to set test command but i want to set build, publish, release etc custom commands.
Basically i want to create below file using npm init without any manual intervention
{
"name": "some name",
"version": "1.0.0",
"scripts": {
"release": "node release.js",
"build": "node index.js"
}
}
Note : Guys i am aware than i can create package.json and then inside that i can edit manually script section to put my commands.
Referring to official doc, you can't do it directly.
But maybe, with using custom questions, it can works.
Have a look here https://docs.npmjs.com/creating-a-package-json-file
It appears as though TypeScript is transpiling target files that are not executable.
I have to run chmod u+x <file> after transpilation to get the files to become executable.
This is the case, even if they have a hashbang:
#!/usr/bin/env node
How can I tell TypeScript / tsc to create files which are executable?
Changing file's permissions isn't typescript responsibility.
Solution 1.
Use a separate step in the build process in your package.json. For example:
{
"name": "temp",
"version": "1.0.0",
"scripts": {
"build": "tsc && chmod +x build/index.js"
},
"dependencies": {
"typescript": "^2.3.4"
}
}
Solution 2.
Write TypeScript Language Service Plugin. I think, in your case this is overengineering.
I'm using the scripts section inside the package.json file to store some commands I have to run regularly.
"scripts": {
"test": "./test/phantomjs ./test/js/run_jasmine_test.coffee ./test/index.html",
"rjs": "r.js -o ./js/app.build.js",
"less": "lessc -x ./css/app.less > ./css/app.css"
}
in every command I have got a ./ at the beginning of the path - this is why I can only call npm run-script rjs from the project's root directory.
is there a way to reference the project's root directory inside the package.json so that I can run e.g. npm test from anywhere in my project?
I think it depends on the way you organize your project.
I created a sample project just to demonstrate. This is my sample project directory tree:
.
├── dir1
│ └── dir2
├── package.json
├── src
│ └── index.js
└── test
└── test.js
NOTE: ./dir1/dir2 are just two empty directories for demonstration.
My package.json file looks like this:
{
"name": "sample",
"version": "1.0.0",
"description": "sample",
"main": "index.js",
"scripts": {
"test": "node test/test.js",
"start": "node src/index.js"
},
"author": "yaniv",
"license": "ISC"
}
Focus on the scripts section, those are two simple line, without considerations about my location.
If I change directory to /dir1/dir2 and execute npm test and the script I wrote on test property is executed properly without the use of ./ (which defines the current directory...)
Bottom line - I suppose if you re-organize your project and remove ./ will do the job in your case.
INIT_CWD
We can reference the root directory of the project throught the environment variable INIT_CWD that npm set for us!
From the doc :
Scripts are run from the root of the module, regardless of what your current working directory is when you call npm run. If you want your script to use different behavior based on what subdirectory you’re in, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run.
Example:
"tas:adminApp:build": "cd src/KidoService/AdminApp && npm run build && cd $INIT_CWD && gulp build_copyAdminApp",
And just because i love to think too much!
What if we didn't have such a variable set for us!
We can execute an env variable set command at all start! export ROOT_DIR=$PDW !
And then when we need it ! We can use it!
The above example will become
"tas:adminApp:build": "export ROOT_DIR=$PDW && cd src/KidoService/AdminApp && npm run build && cd $ROOT_DIR && gulp build_copyAdminApp",
You can see it's a nice technique ! We can use that to reference some directories that we want to go back to!
For windows we use set VAR=VALUE
considered creating a shell file that runs?
for example create a .sh file make it executable (chmod 777 file.sh)
and then cd to your nodejs project root, run the npm command, and then cd back into the directory you have just left?
adding the script to your environment will make it executable from anywhere...
alternatively will hard coding the full path be ok? (so rather than using ./ - put /home/user/username/yourproject/phantomjs)