I am using npm 8.5.0 and node v16.14.2 on a big project. When I run eslint, I can choose to run it without npx, or I can run it with npx. There doesn't seem to be any difference. I'm writing some npm scripts that run eslint and I don't know whether to write npx eslint or eslint.
What is the difference between running npx eslint and eslint?
npx will download and run the package and is meant for interactive use where you just want to use a tool from the npm registry.
You shouldn't use it in your package.json's scripts section; instead just make sure the desired version of eslint is in your package's dev dependencies and use "eslint" in the scripts, so you're certain to use the correct version.
Related
I am currently debugging a project that uses ts-node to run locally. Do I have to install ts-node globally for this?
I did install it globally, but now i am seeing typescript errors that stems from diverging typescript versions. I fixed it by setting it to the same version.
Is there a way to use my project dependencies in my CLI to run the project? Or do I HAVE to install things globally?
By default, the node_modules folder has a .bin subdirectory. You can run the local version of ts-node while in the project directory.
Here's what you might type into your shell
cd /path/to/project
./node_modules/.bin/ts-node myFile.ts
Since npm#5.2.0, npm ships with the npx package, which lets you run commands from the node_modules/.bin directory in your current project. For example: npx ts-node -v.
If for whatever reason you have an older version of npm, you can install this package globally with npm install -g npx.
You can use npx <package-name> also for packages that aren't installed yet in your project (see its options).
Alternatively, you can put the following into your package.json:
"scripts": {
"start": "ts-node code/start.ts",
}
Commands in the scripts field also use the local version of a package.
Symptom:
I can't execute the npm packages directly that are not installed globally. But I can execute it by npm scripts. How to fix it?
For example:
I installed gulp under the project:
npm install gulp --save-dev
Then I try to execute it by
gulp
zsh: command not found: gulp
But if I add a npm script to package.json:
"scripts": {
"test": "mocha --require intelli-espower-loader && gulp test",
"start": "gulp"
},
Then run
npm start
It can get executed without problem.
P.S.
Same issue with the mocha package, I can't execute mocha directly but I can execute npm test without problem.
Help Wanted:
What I can do to fix that issue?
Notice:
I'm not saying that I want to execute them globally, I just want to execute them under the project.
I don't know where goes wrong, but they are executable not long ago, just don't work recently!
In addition to #Ion's answer: You might need to add the path to the environment variables. In windows OS, it would be %AppData%\npm. For packages installed locally, you should be able to run them like
.\node_modules\.bin\gulp
If you want to execute them globally install with the -g flag
npm install gulp -g
To run directly you could also do ./node_modules/.bin/gulp
That is because the package not installed globally doesn't create a reference in the root node_modules folder . If you want to specifically use the local installed version to run globally on terminal then go to you environment variables and set the path to ./node_modules/gulp/bin/gulp.js or alternatively configure your npm to use the mentioned location as the gulp and execute node_modules/.bin/gulp
You have two choices:
Install the executable modules globally:
npm install -g gulp
This is good if it's a tool you use often (for many different projects). For gulp specifically, the globally installed gulp will also check to see if the project has its own gulp installed (in node_modules) and run that version of gulp. This means that each project can have its own version of gulp. But not all tools have this feature.
Use npx:
npx gulp
Newer versions of npm ships with a command called npx which will search through your project's node_modules directory to find a module to execute. This means you don't need to install project specific tools globally. Apart form avoiding global installs of commands (which may or may not be a security issue) it also allows you to install project specific versions of tools.
If your npm installation does not have npx you can install it from npm itself:
npm install -g npx
I just installed Webpack, using:
npm install -g webpack
I then run webpack from the command line without parameters:
webpack
Each time I do this.
One CLI for webpack must be installed. These are recommended choices, delivered as separate packages:
- webpack-cli (https://github.com/webpack/webpack-cli)
The original webpack full-featured CLI.
- webpack-command (https://github.com/webpack-contrib/webpack-command)
A lightweight, opinionated webpack CLI.
We will use "npm" to install the CLI via "npm install -D".
Which one do you like to install (webpack-cli/webpack-command):
From webpack v4.0.0-alpha.1 the cli component is moved to another independant package called webpack-cli
CLI has been move to webpack-cli, you need to install webpack-cli to
use the CLI
From this github issue:
it was intentional to make webpack-cli not a dependency of webpack:
Less depenencies when only using the node.js API
Both projects can develop indiviually.
Breaking changes for webpack-cli doesn't mean breaking change for webpack and in reverse.
So, you just to install webpack-cli one time and it will work as expected.
Note: You can also install webpack-command
I had the same problem. I solved it by installing an older version of the webpack:
npm uninstall -g webpack
npm install -g webpack#3
For webpack 4 please use below command in the terminal:
npx webpack
So I'm in the process of publishing a package to npm. It is basically just a simple module that lets users make Ajax calls and can be configured in a few ways.
I have read that it is a good idea to test the install locally and tried that. I have packed the package via the "npm pack" command, change into another directory and then tried installing the packge via "npm install path-to-file-that-was-just-created.tgz".
So far everything works, I have a node_modules folder, that contains my bundled code.
However, is also has installed all the dependencies that I have listed as devDependencies in the package.json of my actual module, even though the only the bundled file is needed and no other depenedencies are defined.
I have tried updating the npm-shrinkwrap.json, and checked that every dependency has the dev property marked as true.
The goal is actually for the user to install this module and then have no dependencies installed, because they do not need babel or mocha, to run the module.
How can I exclude these from the packge?
Thanks!
https://docs.npmjs.com/cli/install
use the --production flag to avoid installing dev dependencies
For published modules, you don't need to do anything, when a user installs your library, only the non-dev dependencies will be installed
If you want your published module to have no dependencies but you still need to have some to build it you can also try to use this command before publishing:
npx json -f package.json -I -e "delete this.devDependencies"
This way only works in CI/CD.
Update: it turned out that npm pkg delete devDependencies does the same without any additional dependency
After running your install, you can prune dev dependencies by running this command:
npm prune --production
this will keep only production dependencies. Documentation from npm here:
If the --production flag is specified or the NODE_ENV environment
variable is set to production, this command will remove the packages
specified in your devDependencies
I've just installed the babel-cli module locally to my project but when I run babel test.js in the terminal it outputs that the *command babel is uknown*.
What I figured out is that npm is trying to find the module in my globally installed modules instead of my project modules, how can I fix it?
Sorry for the silly question but I'm not finding a solution.
You can either
Use the babel installed in you project - node_modules/.bin/babel test.js
Add a script in your package.json and run it via npm - npm run <script_name>. npm will use the version of babel installed in your project
When you run babel. It looks for global npm directory and can't find it there.
2 ways but one is redundant.
You can link your local path to global npm directory which is
redundant and wont work for the next project. Don't never do this.
Or install it globally. That how npm works for now.
There is a discussion on that. And here is a good article. http://www.joezimjs.com/javascript/no-more-global-npm-packages/