Weird behaviour on my OSX Yosemite: from one day to another all my node modules installed by npm install -g were not found via the Terminal.
I'm not sure if this is due to my node v4.0.0 install the day before.
Try this
echo $(npm config get prefix)/bin
you will get STRING, which should include to your .bash_profile such way
export PATH=$PATH:STRING
Install it globally
npm i -g webpack
If you will work with webpack, install webpack-dev-server too
npm i -g webpack-dev-server
After I Install this two command I also found errors when run the
webpack
command so I figure out the problem by changing the version of webpack so I Install
npm install webpack#2.1.0-beta.22
and every thing work fine for me.
I recommend you first learn a bit about npm and then webpack. You will struggle a lot. but finally you will find the right destination.
I've finally also used NVM for multiple NodeJS versions management and everything went back under control.
Webpack is in your ./node_modules/.bin/ folder so instead of giving just webpack as the command try this.
./node_modules/.bin/webpack
You have the npm bin command to get the folder where npm will install
executables.
You can use the scripts property of your package.json to use webpack
from this directory which will be exported.
"scripts": { "scriptName": "webpack --config etc..." } For example:
"scripts": { "build": "webpack --config webpack.config.js" } You can
then run it with:
npm run build Or even with arguments:
npm run build -- <args>
This allow you to have you webpack.config.js in the root folder of
your project without having webpack globally installed or having your
webpack configuration in the node_modules folder.
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
On windows for some reason when I run npm install it won't install devDependencies. AFAIK it should. If I run npm install --dev devDependencies are installed. I don't understand why npm install doesn't install devDependencies too, but installs only dependencies. What could be the reason? How can I fix it?
Maybe something is wrong with my package.json? It is listed below if it may be helpful:
{
"name": "try-brunch",
"version": "0.1.0",
"private": "true",
"devDependencies": {
"brunch": "^2.0.4",
"cssnano-brunch": "^1.1.5",
"javascript-brunch": "^1.8.0",
"sass-brunch": "^1.9.2",
"uglify-js-brunch": "^1.7.8"
},
"dependencies": {
"jquery": "^2.1.4"
}
}
Check the NPM docs for install
With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies."
The --only={prod[uction]|dev[elopment]} argument will cause either only devDependencies or only non-devDependencies to be installed regardless of the NODE_ENV."
Have you tried
npm install --only=dev
If you are worried that your package.json might be incorrect, best thing to do is this. Create a new folder, and run:
npm init --yes
Then:
npm install --save-dev brunch#^2.0.4
npm install --save-dev cssnano-brunch#^1.1.5
npm install --save-dev javascript-brunch#^1.8.0
npm install --save-dev sass-brunch#^1.9.2
npm install --save-dev uglify-js-brunch#^1.7.8
npm install jquery#^2.1.4 --save
And you should be good to go! Otherwise, will keep posting other options.
Check your npm configuration:
npm config list
npm gets its config settings from the command line, environment variables, and npmrc files. So check environment variables, and the npmrc file.
Still failing?
Ok, create a new folder, ideally somewhere else on your filesystem. ie. not in same folder hierarchy. For instance, C:\myNewFolder - the closer to the base C: drive the better.
Then run:
npm init --yes
Now run:
npm install underscore --save
and finally:
npm install mocha --save-dev
Does everything work as expected?
What I am trying to do is understand whether your problem is global, or something local to the previous folder and dependencies.
Check if npm config production value is set to true. If this value is true, it will skip over the dev dependencies.
Run npm config get production
To set it: npm config set -g production false
make sure you don't have env variable NODE_ENV set to 'production'.
If you do, dev dependencies will not be installed without the --dev flag
You can use the short way for installation dependencies only for development as follows:
npm i -D <dependencies-names>
I had a package-lock.json file from an old version of my package.json, I deleted that and then everything installed correctly.
I had a similar problem. npm install --only=dev didn't work, and neither did npm rebuild. Ultimately, I had to delete node_modules and package-lock.json and run npm install again. That fixed it for me.
I have the same issue because I set the NODE_ENV=production while building Docker. Then I add one more npm install --only=dev. Everything works fine. I need the devDependencies for building TypeSciprt modules
RUN npm install
RUN npm install --only=dev
Make sure your package.json is valid...
I had the following error...
npm WARN Invalid name: "blah blah blah"
and that, similarly, caused devDependencies not to be installed.
FYI, changing the package.json "name" to blah-blah-blah fixed it.
As of now you could use:
npm i --also=dev
So the way I got around this was in the command where i would normally run npm install or npm ci, i added NODE_ENV=build, and then NODE_ENV=production after the command, so my entire command came out to:
RUN NODE_ENV=build && npm ci && NODE_ENV=production
So far I haven't had any bad reactions, and my development dependencies which are used for building the application all worked / loaded correctly.
I find this to be a better solution than adding an additional command like npm install --only=dev because it takes less time, and enables me to use the npm ci command, which is faster and specifically designed to be run inside CI tools / build scripts. (See npm-ci documentation for more information on it)
In my case, the problem was that I had the NODE_ENV variable set to production in the same terminal session I ran npm install.
For my build to run properly I was not allowed to change the value of NODE_ENV so I forced npm to install all the dependencies by adding the --production=false flag to it: npm install --production=false as mentioned in the docs.
If you don't need NODE_ENV to be set to production you can simply type export NODE_ENV=development to your terminal to overwrite its value and run npm install again.
Got a similar error after running npm-check-updates -u. Solved it by removing node_modules folder and package-lock.json. After that a new npm install and everything worked.
My exception:
Failed to load parser '#typescript-eslint/parser' declared in
'package.json ยป eslint-config-react-app#overrides[0]': Cannot find
module '#typescript-eslint/parser'
As #Ale told, we can use npm i -D <some_module_name> or npm i --save-dev <some_module_name> now.
It seems command was changed at some point of node version.
Offical (npm dependencies and devDependencies) says following.
When you add the -D flag, or --save-dev, you are installing it as a development dependency, which adds it to the devDependencies list.
don't really see why I can't get this to run... I've done the following.
npm install -g webpack webpack-dev-server
confirmed that both installed successfully.
Now, when I try and execute webpack-dev-server, I get the following:
$ webpack-dev-server --inline --hot
zsh: command not found: 'webpack-dev-server'
Never run into this as all my other npm modules load/run fine, both locally and globally. I've tried installing it locally to a project as well (adding to package.json, npm install, yadda yadda..) and receive the same error.
Am I missing something?
Found a solution as per this answer on GitHub:
I was able to solve this problem on my machine. It appears to have been an issue with permissions.
I installed webpack and webpack-dev-server globally. However, even then, $ webpack-dev-server resulted in command not found (as stated above).
The problem was that npm installed the global packages to /usr/local/lib/node_modules which required root permissions.
In order to avoid having to use root permissions, I changed the directory in which global packages are to be installed to a directory in $HOME. To do this, I followed this guide: https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md
I installed webpack and webpack-dev-server globally again (this time without sudo) and verified that they have been installed in my new directory.
Now, I can finally run $ webpack-dev-server.
The problem is that npm installs the global packages to /usr/local/lib/node_modules which requires root permissions.
To avoid this you can use Use sudo npm i -g webpack webpack-dev-server.
It worked for me.
I have this in my package.json file (shortened version):
{
"name": "a-module",
"version": "0.0.1",
"dependencies": {
"coffee-script": ">= 1.1.3"
},
"devDependencies": {
"stylus": ">= 0.17.0"
}
}
I am using NPM version 1.1.1 on Mac 10.6.8.
When I run the following command from the project root, it installs both the dependencies and devDependencies:
npm install
I was under the impression that this command installed the devDependencies:
npm install --dev
How do I make it so npm install only installs dependencies (so production environment only gets those modules), while something like npm install --dev installs both dependencies and devDependencies?
The npm install command will install the devDependencies along other dependencies when run inside a package directory, in a development environment (the default).
In version 8.x and above use --omit=dev flag to install only regular dependencies:
npm install --omit=dev
This will install only dependencies, and not devDependencies, regardless of the value of the NODE_ENV environment variable.
If you use 6.x or an earlier version, you need to use the --only=prod flag instead.
Note:
Before v3.3.0 of npm (2015-08-13), the option was called --production, i.e.
npm install --production
You may also need --no-optional flag.
I run into that problem too! npm install is somewhat confusing and web posts keep bringing in the -d/--dev flags as if there is an explicit 'development' install mode.
npm install will install both "dependencies" and "devDependencies"
npm install --production will only install "dependencies"
npm install --dev will only install "devDependencies"
The new option is:
npm install --only=prod
If you want to install only devDependencies:
npm install --only=dev
If you have already installed all your dependencies, and you want to avoid having to download your production packages from NPM again, you can simply type:
npm prune --production
This will remove your dev dependencies from your node_modules folder, which is helpful if you're trying to automate a two step process like
Webpack my project, using dev dependencies
Build a Docker image using only production modules
Running npm prune in between will save you from having to reinstall everything!
If you read this POST in 2016, please achieve what you want by using
--only={prod[uction]|dev[elopment]}
argument will cause either only devDependencies or only non-devDependencies to be installed regardless of the NODE_ENV.
from: https://docs.npmjs.com/cli/install
When using "npm install" the modules are loaded and available throughout your application regardless of if they are "devDependencies" or "dependencies". Sum of this idea: everything which your package.json defines as a dependency (any type) gets installed to node_modules.
The purpose for the difference between dependencies/devDependencies/optionalDependencies is what consumers of your code can do w/ npm to install these resources.
Per the documentation: https://npmjs.org/doc/json.html...
If someone is planning on downloading and using your module in their
program, then they probably don't want or need to download and build
the external test or documentation framework that you use.
In this case, it's best to list these additional items in a
devDependencies hash.
These things will be installed whenever the --dev configuration flag
is set. This flag is set automatically when doing npm link or when
doing npm install from the root of a package, and can be managed like
any other npm configuration param. See config(1) for more on the
topic.
However, to resolve this question, if you want to ONLY install the "dependencies" using npm, the following command is:
npm install --production
This can be confirmed by looking at the Git commit which added this filter (along with some other filters [listed below] to provide this functionality).
Alternative filters which can be used by npm:
--save => updates dependencies entries in the {{{json}}} file
--force => force fetching remote entries if they exist on disk
--force-latest => force latest version on conflict
--production => do NOT install project devDependencies
--no-color => do not print colors
#dmarr try using npm install --production
npm will install dev dependencies when installing from inside a package (if there is a package.json in the current directory). If it is from another location (npm registry, git repo, different location on the filesystem) it only installs the dependencies.
I suggest to use npm ci. If you want to install only production-needed packages (as you wrote - without devDependencies) then:
npm ci --only=production
or
NODE_ENV=production npm ci
If you prefer oldschool npm install then:
npm install --production
or
NODE_ENV=production npm install
Here is good answer why you should use npm ci.
It's worth mentioning that you can use the NODE_ENV environment variable to achieve the same result. Particularly useful if you're containerizing your Node application (e.g. Docker).
NODE_ENV=production npm install
The above code will install all your dependencies but the dev ones (i.e. devDependencies).
if you need to use environment variables in your Dockerfile more information can be found here.
Environment variables are easy to overwrite whenever needed (e.g. if you want to run your test suite say on Travis CI). If that were the case you could do something like this:
docker run -v $(pwd):/usr/src/app --rm -it -e NODE_ENV=production node:8 npm install
NPM Documentation here
production
Default: false
Type: Boolean
Set to true to run in "production" mode.
devDependencies are not installed at the topmost level when running local npm install without any arguments.
Set the NODE_ENV="production" for lifecycle scripts.
Happy containerization =)
npm install --production --no-optional
It installs only deps from dependencies and will ignore optionalDependencies and devDependencies
Use npm install packageName --save this will add package in dependencies, if you use npm install packageName --save-dev then it devDependencies.
npm install packageName --save-dev should be used for adding packages for development purpose. Like adding TDD packages (Chai, mocha, etc). Which are used in development and not in production.
I have found that, when trying to install dev dependencies for a package that contains a node addon, you cannot avoid building the addon when running npm install --dev even if you just want to install the devDependencies. So, I had to go around npm's back:
node -e 'console.log( Object.keys( require( "./package.json" ).devDependencies ) );' | \
sed -e "s/^[^']*'//" -e "s/'.*$//" | \
xargs npm install
Or, better (and more succinctly) yet,
node -e 'Object.keys( require( "./package.json" ).devDependencies )
.map( function( item ){ console.log( item ) } );' | xargs npm install
I ran into a problem in the docker node:current-slim (running npm 7.0.9) where npm install appeared to ignore --production, --only=prod and --only=production. I found two work-arounds:
use ci instead (RUN npm ci --only=production) which requires an up-to-date package-lock.json
before npm install, brutally edit the package.json with:
RUN node -e 'const fs = require("fs"); const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8")); delete pkg.devDependencies; fs.writeFileSync("./package.json", JSON.stringify(pkg), "utf-8");'
This won't edit your working package.json, just the one copied to the docker container.
Of course, this shouldn't be necessary, but if it is (as it was for me), there's your hack.
Need to add to chosen answer: As of now, npm install in a package directory (containing package.json) will install devDependencies, whereas npm install -g will not install them.
npm install --production is the right way of installing node modules which are required for production. Check the documentation for more details
Now there is a problem, if you have package-lock.json with npm 5+. You have to remove it before use of npm install --production.