Does npm has a shell or environment inbuilt, which can be used to run node modules installed locally ?
Ex:
I have nodeunit, eslint and other packages listed in my dependencies. I can run commands provided by these dependencies from npm scripts like:
"lint": "eslint .",
"test": "nodeunit test/*js",
These commands cannot be run outside npm scripts. I know, with a small configuration we can achieve this (by updating the PATH with node_modules/.bin), but I am wondering if there is npm way of doing this (something like npm shell which would enable this).
In other words:
In my project dir I will not be able to run this: nodeunit . because nodeunit is not available in the PATH. When I execute npm test, npm adds node_modules/.bin to PATH, hence nodeunit is available under npm test script. My question is: Is there a npm way which will enables us to execute these commands in the project directory without using them in npm scripts
Related
I ask this question because I have been installing nodemon with npm and I see the results of installing through the suggested command at first sight, at the right side of the screen:
npm i nodemon
It is different from the installation instructions you can read above, on the Installation section. There we see:
global installation:
npm install -g nodemon
install nodemon as a local project dependency:
npm install --save-dev nodemon
The thing is, what is difference between
npm i nodemon
and
npm install -g nodemon
When I use the first command it tells me typical "nodemon is not recognized as internal or external command, operable program or batch file". To solve it I must install globally.
When you run npm i nodemon nodemon is installed as a local project dependency, to run nodemon on the CLI you would have to provide the pull path to it's installation, typically you would want to make this reference in your project's package.json file's scripts property, for instance:
{
...
"scripts": { "nodemon": "nodemon index.js" },
...
}
This can then be executed by running npm run nodemon.
On the other hand running npm install -g nodemon or npm i -g nodemon installs nodemon on the global scope where it is referenced in your system PATH variable, that way you can easily call nodemon on the CLI and since it's full installation path is referenced in your system PATH variable it would execute like every other CLI command.
Browser is made available to the current project (where it keeps all of the node modules in node modules) after local installation. It will not be available as a command that the shell can resolve until you install it globally with npm install -g module, in which case npm will install it in a location where your path variable will resolve this command. Typically, this is only good for using a module like so var module = require('module');
This documentation will help.
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 can use parcel index.html to create a local development server, bundling and hot module replacement. But it have come to my attention that using npm run dev does kind of the same think, so my question is:
what is the difference between the two? and how npm run dev is making the bundling process?
NPM vs Parcel isn't a valid comparison. They are two separate things. You can use Parcel with both NPM and Yarn.
Parcel is a web application bundler that is comparable to Webpack
NPM is a package management system for node.
npm run * is a command that will execute any npm script specified within your package.json and has no exclusivity to Parcel. You can of course make an npm script that will execute Parcel commands.
If you go into your package.json file, you will see a scripts property. Within this object, you can define arbitrary scripts to run. There are reserved script names such as start, install, build among others, but for the most part, this is a "free-for-all" that enabled the developer to specify any arbitrary scripts to run. A few common scripts that you'll typically see scripts to bundle your project or run a linter.
Example of package.json
Webpack Example:
{
"scripts": {
"build": "webpack --config <your entry file>"
}
}
Parcel Example:
{
"scripts": {
"build": "parcel build <your entry file>"
}
}
I am new to npm and gulp. I am reading this css-tricks tutorial on gulp. In section Installing Gulp they installed gulp globally so that it could be used with command line from any location in the computer. In the next section Creating a Gulp Project they again installed gulp locally.
What I don't understand is if gulp is already installed globally and we can use it from anywhere then why install gulp twice? Why not just put a gulpfile.js with other package.json file?
You install gulp globally for using simple gulp command in your terminal and install gulp locally (with package.json dependency) in order not to lose the dependency, because you can install your project to any computer, call npm i and access gulp with ./node_modules/.bin/gulp without any additional installations
You don't even need to have installed gulp globaly. Just have it locally and put gulp commands in package.json scripts like this:
"scripts": {
"start": "gulp",
"speed-test": "gulp speed-test -v",
"build-prod": "gulp build-prod",
"test": "NODE_ENV=test jasmine JASMINE_CONFIG_PATH=spec/support/jasmine.json"
},
Than everyone working on same project can just npm install and start running commands without even having gulp globally installed.
npm start will run gulp
npm run speed-test will run gulp speed-test -v
npm run build-prod will run gulp build-prod
And of course add as many commands as you want there. And if someone from team have or wants to have gulp globally than they can run gulp commands directly from terminal.
I created a package.json and added the needed dependencies (grunt, bower, jasmine etc)
when I run
npm install
it correctly installs all the modules in
$pwd/node_modules/*
however when I try to USE those modules:
bower init; grunt init
i get
sh.exe: (bower / grunt / w.e) Command not found
I can solve this by using
npm install -g (package name)
but my understanding is the "-g" makes the install global? (is that correct?)
I want to be able to use the packages i installed locally...is this possible?
You can use npm run-scripts to create a command which will run the local copies of bower and grunt that you have installed.
In package.json, add a key like this:
"scripts": {
"init": "bower init; grunt init"
}
Then run the command npm run init.