The package.json example at this link includes the following start commands:
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
What explicitly does the above command do?
I think that the "concurrently \"npm run tsc:w\" \"npm run lite\" " means to start both tsc and lite-server simultaneously, and to also place a watch on the tsc so that changes are reloaded into lite-server immediately. Is this correct? And also, why call tsc twice? What is an explicit explanation of the entire line of code including all of its constituent parts put together?
You can break it down the command into parts (with quotes removed):
tsc
concurrently
npm run tsc:w
npm run lite
The first part calls the TypeScript compiler CLI and compiles your TypeScript files.
Next, there is an && which means "cmd1 then/and cmd2". The next section:
concurrently npm run tsc:w npm run lite
Uses the concurrently package CLI to run the given commands, which are npm run tsc:w and npm run lite. The part:
npm run tsc:w
This runs the script in your package.json:
"tsc:w": "tsc -w"
Then npm run lite runs the corresponding script in package.json:
"lite": "lite-server"
So you're technically calling tsc twice but tsc:w starts watching your TypeScript files. Using -w does not do an initial build, so the first tsc is needed to build your files initially, then -w watches your files and rebuilds subsequent changed files. The concurrent script then runs the watch script and the server.
Related
I have a package.json file at the root of my project folder which contains client and server folders.
in the root package.json I have the following scripts:
"scripts": {
"server": "npm run watch --prefix server",
"client": "npm start --prefix client",
"watch": "npm run server & npm run client"
}
but when i try to run npm run watch only the server script runs, what I don't understand is if I paste the contents of the watch script into the terminal it works just fine.
npm run <script> uses cmd.exe by default if you are on windows, and cmd does not support & to run commands in parallel, so it is better to use a package like npm-run-all.
I've always used just npm and never yarn/webpack explicitly. I need to run the code from this repo:
https://github.com/looker-open-source/custom_visualizations_v2
Like a dev server or something to ensure it's serving the files properly but I don't see a "run" like npm run start. Does this just not exist with yarn? It feels like this code should work as is and I shouldn't have to add anything.
EDIT: I've now tried yarn run watch but it just seems to build the code again and not actually host anywhere
npm run somecommand just looks up in the "scripts" field of package.json for the key
somecommand and executes the value in the terminal.
So npm run start basically runs the start script from package.json
The same thing is done using yarn via simply yarn start
In the linked repo, there isn't a start script in the package.json, rather a watch script, so you should be able to run it with the below steps:
yarn to install dependencies after cloning the repo to local (similar to npm install)
yarn watch to start the webpack server (analogous to npm run watch)
Edit:
Turns out the watch command is just setting up webpack to watch for changes and recompile the project every time there is a change.
To run a development server, you will need to add another script preferably with name start and use webpack-dev-server
So the package.json has entries like:
...
"watch": "webpack --config webpack.config.js --watch --progress",
"start": "webpack-dev-server --config webpack.config.js",
...
Then running yarn start should open a dev server at localhost:8080
I want to run these pm2 tasks:
"pm2-frontend": "pm2 start --name frontend npm -- start",
"pm2-storybook": "pm2 start --name storybook npm -- storybook"
Which should run these two package.json scripts:
"storybook": "start-storybook -p 6006",
"start": "next start"
But, this only seems to start the start script. Is there a way to target scripts with another name than start?
Use the word run before you run scripts other than start.
Start is a default script name so you can run it with npm start but storybook is not a default script so you gotta use npm run storybook
I have a Node.js app using TypeScript and now I want Jasmine to run tests automatically each time I make changes in .ts files. So I'm just trying to find an appropriate command to be run as npm test in command line or a package that can watch my .ts files compile them on changes and run jasmine. Does anybody know a solution for it?
The easiest way I found is
installing dependencies: npm install --save-dev jasmine-ts nodemon
initializing jasmine: node_modules/.bin/jasmine-ts init
In the package.json:
"scripts": {
"test": "nodemon --ext ts --exec 'jasmine-ts \"src/**/*.spec.ts\"'"
}
Edit: the above solution doesn't work as of the 11th of Apr, 2019. I published a modified working example at https://github.com/erosb/ts-node-jasmine-example
This may be done with two commands launched in separate terminals. Assuming packages are installed in global mode.
First command launches TypeScript compiler in watch mode:
tsc --watch
The second starts nodemon that watches .js files and restarts on changes. Each time it executes jasmine test runner:
nodemon --ext js --exec 'jasmine JASMINE_CONFIG_PATH=jasmine.json'
This solution is fast enough though it also has a drawback of running in two terminals. So it is not ideal but the best I've found so far.
As a result scripts section in package.json looks like:
"scripts": {
/* ... */
"watch": "tsc --watch",
"test": "nodemon --ext js --exec 'jasmine JASMINE_CONFIG_PATH=jasmine.json'",
"devstart": "nodemon ./bin/www"
},
devstart also works in couple with watch restarting server each time .ts files are changed (after they are compiled to .js).
You might consider using jasmine-node. I don't think that jasmine itself has a watch option.
npm i -g jasmine-node
Assuming that your test command in your package.json scripts block is something like this:
"scripts": {
...
"test": "jasmine some-directory-or-glob-pattern"
...
}
Use jasmine-node and add the --autotest and --watch flags to that command:
"scripts": {
...
"test": "jasmine-node --autotest --watch some-directory-or-glob-pattern"
...
}
Previously described methods either did not work, or were slow to compile code. Here is my attempt to solve this, both fast and convenient, works great for me. The only downside is that jasmine would not know which tests are affected by TS recompilation and would run all the tests.
yarn add tsc-watch --dev
yarn run tsc-watch --onSuccess "yarn run jasmine --config=jasmine.json"
NPM version:
npm -i tsc-watch
npm run tsc-watch --onSuccess "npm run jasmine --config=jasmine.json"
In my case I needed to correctly map TS paths. The full command looks like this:
yarn run tsc-watch --onSuccess \
"node -r tsconfig-paths/register node_modules/jasmine/bin/jasmine \
--config=jest/jasmine.json --require=dist/jest/setup.js $targetFile"
jasmine.json
{
"spec_dir": "dist/src",
"spec_files": ["**/*.e2e.js", "**/*.unit.js", "**/*.spec.js", "**/*.test.js"],
"env": {
"random": false
}
}
Just an example, please adjust to your needs.
tsc-watch starts a TypeScript compiler with --watch parameter, with the ability to react to successful compilation and start tests.
I'm experimenting with setting up a dev environment to use NPM only, without the use of grunt.js or bower.js.
I followed this tutorial: http://beletsky.net/2015/04/npm-for-everything.html
I'm using nodemon to watch my .js and .scss files for changes which restarts the node server. So in my package.json file, under scripts I have
scripts:
"watch-js": "nodemon -e js --watch public/js -x \"npm run build-js\"",
"watch-sass": "nodemon -e scss --watch public/sass -x \"npm run build-sass\"",
"watch": "npm run watch-js & npm run watch-sass"
But when I run npm run watch it only watches for the public/js files to change. And it triggers a build accordingly.
But it won't watch for the sass files.
Versions:
node v0.10.36 nodemon v1.4.1
I also include a build script which if I run compiles the sass to css, so my build-sass script should be ok
"build": "npm run build-js & npm run build-sass",
"watch": "npm run watch-js & npm run watch-sass"
If you are using windows, you might be encountering the windows problem
Because npm is reliant on the operating systems shell to run scripts commands, they can quickly become unportable. While Linux, Solaris, BSD and Mac OSX come preinstalled with Bash as the default shell, Windows does not. On Windows, npm will resort to using Windows command prompt for these things
If so, you can try using parallelshell or npm-run-all for better cross platform support.
This works for me in Mac OS
Install nodemon
npm install --save-dev nodemon
Install npm-run-all:
npm install --save-dev npm-run-all
In package.json add two scripts:
"startn": "nodemon ./app.js",
"watch-jscss": "gulp watch"
Where 'app.js' is your server file
"gulp watch" can be replace by whatever task is doing a sass watch
In the console:
npm-run-all --parallel watch-jscss startn