Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reconcile global webpack install and local loaders

My package.json includes webpack and some loaders:

"devDependencies": {
  "babel-core": "^5.2.17",
  "babel-loader": "^5.0.0",
  "jsx-loader": "^0.13.2",
  "node-libs-browser": "^0.5.0",
  "webpack": "^1.9.4"
}

When I run webpack it's not in my path so it doesn't show as found. I installed it globally npm install -g webpack so the binary would appear in my path, but then it can't find the loader modules that were installed in ./node_modules that it needs to process my dependency tree:

$ webpack --progress --colors --watch
10% 0/1 build modules/usr/local/lib/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:206
                throw e;
                      ^
Error: Cannot find module 'jstransform/simple'```

What is the preferred solution here?

I can install my loaders globally, but I don't like that because of cross-project issues

I can try to run webpack out of node_modules (not sure how to be honest, add it to $PATH for every project?)

Or I can try to give my global webpack access to my node_modules folder, which also seems hacky.

Have I done something wrong, or is there a better community-approved way around this maybe common problem?

like image 962
Alex Mcp Avatar asked May 11 '15 16:05

Alex Mcp


People also ask

How to run webpack locally?

To run the local installation of webpack you can access its binary version as node_modules/. bin/webpack . Alternatively, if you are using npm v5. 2.0 or greater, you can run npx webpack to do it.

How do I install a specific version of webpack?

Open cmd on the node-modules folder outside of your project folder. Uninstall webpack and webpack-dev-server: npm uninstall webpack npm uninstall webpack-dev-server. Delete the node-modules folder and the package-lock. Open the node-modules again npm install [email protected] npm install [email protected].

How do I update npm webpack?

Simply us the terminal command npm install [email protected] in your IDE's terminal to update it, and you should be good to go! :) If this was supposed to be a self-answered question, note it must still be a separate question and answer meeting the usual quality standards.


1 Answers

I have a blog post called Managing Per-Project Interpreters and the PATH that details my methodology for this. I'll summarize here to address some of your key questions.

What is the preferred solution here?

Never (really, literally never) use npm -g. Just install normally within your project. Use your shell to set the PATH appropriately. I use zsh to do this automatically as detailed in the blog post above so if I cd into a project with ./node_modules/.bin, it gets put on my PATH automatically.

There are other ways that work including making aliases like alias webpack="./node_modules/.bin/webpack" and so on. But really just embrace changing your PATH and you'll have the most harmonious long-term experience. The needs of a multiproject developer are not met by old school unix everything lives in either /bin or /usr/bin.

  • If you use npm scripts (the "scripts" key in your package.json), npm automatically includes ./node_modules/.bin in your PATH during those scripts so you can just run commands and they will be found. So make use of npm scripts and if you make use of shell scripts that's an easy place to just do export PATH="$PWD/node_modules/.bin:$PATH".
like image 128
Peter Lyons Avatar answered Sep 24 '22 15:09

Peter Lyons