You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I get the following error simply by using require handlebars.
▶ webpack --display-modules
Hash: bdb0fe35e2f8bde783e5
Version: webpack 1.12.12
Time: 116ms
Asset Size Chunks Chunk Names
bundle.main.js 2.82 kB 0 [emitted] main
[0] ./src/index.js 150 bytes {0} [built]
[1] ./~/handlebars/lib/index.js 792 bytes {0} [built] [3 warnings] [3 errors]
WARNING in ./~/handlebars/lib/index.js
require.extensions is not supported by webpack. Use a loader instead.
WARNING in ./~/handlebars/lib/index.js
require.extensions is not supported by webpack. Use a loader instead.
WARNING in ./~/handlebars/lib/index.js
require.extensions is not supported by webpack. Use a loader instead.
ERROR in ./~/handlebars/lib/index.js
Module not found: Error: Cannot resolve 'file' or 'directory' ../dist/cjs/handlebars/compiler/printer in /home/abhisekp/MyProjects/JSApps/MadeWithLove-Webpack/node_modules/handlebars/lib
@ ./~/handlebars/lib/index.js 9:14-64
ERROR in ./~/handlebars/lib/index.js
Module not found: Error: Cannot resolve module 'fs' in /home/abhisekp/MyProjects/JSApps/MadeWithLove-Webpack/node_modules/handlebars/lib
@ ./~/handlebars/lib/index.js 17:11-24
ERROR in ./~/handlebars/lib/index.js
Module not found: Error: Cannot resolve 'file' or 'directory' ../dist/cjs/handlebars in /home/abhisekp/MyProjects/JSApps/MadeWithLove-Webpack/node_modules/handlebars/lib
@ ./~/handlebars/lib/index.js 7:17-50
My index.js
varhandlebars=require('handlebars');
My package.json
"dependencies": {
"handlebars": "^4.0.5"
}
Reacted by John Mee, J.Fischer, Amr Abdulrahman, wildsky, Bharadwaj Giridhar and Jukka Snellman
As mentioned in #1102 adding the line "browser": "dist/handlebars.js",
to handlebars' package.json solved this for me.
Reacted by xcatliu, Henri Podolski, Fellipe Cicconi, Felipe Oliveira, Sofia Kritikou, Josh and Bohdan Yurov
Reacted by Abhisek Pattnaik, J.Fischer, jimai119, Diego Pronesti, Richard van Laak, Last Night, Yair Ohana, manujPF, Oscar Nilsson, satyajit nayak and 2 more
Reacted by Abhisek Pattnaik, Will Fisher, elsemaani, Naz, Adrian Florescu, Flyher D, Gabriel Wicke, Ozan Müyesseroğlu, Jean-Elie Barjonet, ryzhman and 127 more
Reacted by Patrick Nwanah and Alex (Teh Sunn Liu)
Reacted by John Mee, Adrian Florescu, serluck, Badal Surana, Jason Awbrey, danyu, Hayden, angel8731, Matias Hernan Lauriti, J.Fischer and 14 more
Reacted by Levon Arakelyan, Adrian Florescu, Koller Konrád, serluck, rvpanoz, Badal Surana, Gabriel Robert, Alexey Masalov, Jason Awbrey, danyu and 22 more
Reacted by Badal Surana, Jason Awbrey, danyu, Hayden, Adam Lusted, Peter Šándor, angel8731, Matias Hernan Lauriti, Vladyslav, Valentin Lacour and 16 more
Reacted by Jason, 一个龟龟, Yu Ohashi, Mike Andrzejewski, Vinicius Faitão, Luis Cardozo, Tatiana Klevanets, kbitgood, Leonid and Alex Sokol
I also get this error when using handlebars with an Angular 2 CLI project. I can't change my webpack configuration because the Angular CLI team decided to hide webpack details from Angular CLI users and they don't officially support modifying the webpack configuration.
To get this to work in an angular 4 project I had to do this:
import * as handleBars from 'handlebars/dist/handlebars';
normally I'd just do this:
import * as handleBars from 'handlebars';
The line above works fine in a typescript project in node, but not in angular 4.
HandleBars is the only project where I have to import like this. It makes me concerned I'm going to have a issue down the road. It seems like HandleBars does not play nice with angular's webpack.
Reacted by TheY2T, Harald Scheckenbacher, James Scharett and Esteban Luchsinger
@swilliams-a3digital Getting Could not find a declaration file for module declare module 'handlebars' or adding @types/handlebars does not help. How did you solve this?
I get this error with webpack 5 create-react-app and I don't know why. Trying to build a simple website.
"dependencies": {
"handlebars": "^4.7.7",
}
import HandleBars from 'handlebars';
ERROR in ./node_modules/handlebars/lib/index.js 15:11-24
Module not found: Error: Can't resolve 'fs' in '/home/steve/source/repos/tmp/aster/node_modules/handlebars/lib'
EDIT: ok so this is a webpack 5 thing, they don't include the node polyfills anymore. And handlebars seems to be extra special and may have it's config wrong. Here's the specific webpack 5 handlebars issue which states the config is wrong: webpack/webpack#15007
I confirmed this works. Can you include this change?
EDIT: The import line you suggested doesn't work with webpack 5. The only workaround I have so far is to fork the lib, change the package.json browser conf as I posted, manually install the dev dependencies, build the package local, then it works. I'm going to submit a PR for the config update, if you could merge it would really great.
what is handlebars/dist/handlebars.min.js. Is this the location npm installs the handlebar after you run npm install? This issue is break for me in github action and i am using rush which installs the packages in the temp folder that is ignored in the check in.
For anyone trying to run handlebars within ts-node WITHOUT precompiling the templates (because, for example, you are using webpack's asset/source module) you can call Object.freeze(require.extensions) after you finish your setup to prevent handlebars from making further changes.
@nknapp I strongly agree with #1174 (comment) that the correct solution is to separate handlebars into two libs: handlebars and handlebars/register so that users can explicitly opt-in or out depending on their needs. I understand this would be a breaking change but I think it would avoid a lot of confusion going forward.
Activity
benjeffery commentedon Feb 4, 2016
As mentioned in #1102 adding the line
"browser": "dist/handlebars.js",to handlebars' package.json solved this for me.
methodbox commentedon May 15, 2016
Sounds like a problem with Webpack, not Handlebars.
The normal default path for any node module core files is /node_modules/module_name/dist.
That's where a node app will assume they are located; this means Webpack is handling require(); differently than node.
gilesbradshaw commentedon Jul 1, 2016
I got round this with ..
in my webpack config
abhisekp commentedon Jul 1, 2016
thanks @gilesbradshaw 👍 😄
rafde commentedon Aug 27, 2016
@abhisekp was this resolved? Please close if yes.
abhisekp commentedon Aug 27, 2016
@rafde I've not checked it but closing this issue as the issue has been made clear and a clear solution is suggested.
Thanks for the reminder. 😊
nkonev commentedon Oct 24, 2016
issue still reproducing if I require handlebars somewhere in .js:
but if I remove this line
issue is gone
flyher commentedon Nov 8, 2016
@gilesbradshaw
Thank you very much!
oocx commentedon Dec 12, 2016
I also get this error when using handlebars with an Angular 2 CLI project. I can't change my webpack configuration because the Angular CLI team decided to hide webpack details from Angular CLI users and they don't officially support modifying the webpack configuration.
abdel-ships-it commentedon May 11, 2017
@oocx You could just import the JS file from the node_modules folder like so
import 'handlebars/dist/handlebars.min.js';This works fine!
swilliams-a3digital commentedon Oct 20, 2017
To get this to work in an angular 4 project I had to do this:
import * as handleBars from 'handlebars/dist/handlebars';
normally I'd just do this:
import * as handleBars from 'handlebars';
The line above works fine in a typescript project in node, but not in angular 4.
HandleBars is the only project where I have to import like this. It makes me concerned I'm going to have a issue down the road. It seems like HandleBars does not play nice with angular's webpack.
nknapp commentedon Oct 20, 2017
I could have a look if anyone sets up a minimal example project that reproduces the issue. No promises as to the "when", though...
JannicBeck commentedon Jun 12, 2018
@swilliams-a3digital Getting
Could not find a declaration file for moduledeclare module 'handlebars' or adding @types/handlebars does not help. How did you solve this?seyfer commentedon Jun 14, 2018
For others looking here
This is the best solution I have found
https://github.com/valtech-nyc/brookjs/blob/master/packages/brookjs/webpack.config.js#L39-L43
here pcardune/handlebars-loader#110 (comment)
Thanks to @mAAdhaTTah
UPD:
11 remaining items
frankguo-dyedurham commentedon Dec 31, 2021
I am using handlebars library in a React Native project and running into the similar issue with the code:
import Handlebars from 'handlebars'I replace it with the code below:
import Handlebars from 'handlebars/dist/handlebars'It looks like it works now. Just try to give some other hints here for someone who runs into the issue.
BTW,
import Handlebars from 'handlebars/dist/cjs/handlebars'doesn't work. I guess it is because the react native project doesn't use webpack.Workaround stupid handlebars import bug
stevemk14ebr commentedon Mar 23, 2022
I get this error with webpack 5 create-react-app and I don't know why. Trying to build a simple website.
the main script does this:
handlebars.js/lib/index.js
Lines 17 to 21 in a9fe6d5
how is that supposed to work with browsers?
EDIT: ok so this is a webpack 5 thing, they don't include the node polyfills anymore. And handlebars seems to be extra special and may have it's config wrong. Here's the specific webpack 5 handlebars issue which states the config is wrong: webpack/webpack#15007
and here's the wider issue about webpack 5 changing polyfill behavior. react/create-react-app#11756
EDIT 2: Issue webpack/webpack#15007 suggests handlebars package.json be changed to:
However, this also still breaks because the dist folder doesn't exist! What is going on? I am so lost.
jaylinski commentedon Mar 23, 2022
@stevemk14ebr The
dist-folder exists within the distributed npm-package.If you want a browser-compatible version, you can try to import via
import Handlebars from 'handlebars/lib/handlebars';.You may still need to polyfill Node.js function since we didn't release the fix for #1593 yet.
stevemk14ebr commentedon Mar 23, 2022
Yea I figured it out, I forked the repo and the git install failed since build never ran and there's no prepare script. The fix for this is:
I confirmed this works. Can you include this change?
EDIT: The import line you suggested doesn't work with webpack 5. The only workaround I have so far is to fork the lib, change the package.json browser conf as I posted, manually install the dev dependencies, build the package local, then it works. I'm going to submit a PR for the config update, if you could merge it would really great.
Fix bundler issue with webpack 5
Fix bundler issue with webpack 5
Fix bundler issue with webpack 5
Fix bundler issue with webpack 5
Fix bundler issue with webpack 5
satyajitnayk commentedon Sep 22, 2023
Thanks a lot @gilesbradshaw❤️. This solution is still relevant today👍🏻.
patricknwanah commentedon Oct 17, 2023
how do i get this handlebars.min.js file?
patricknwanah commentedon Oct 17, 2023
what is handlebars/dist/handlebars.min.js. Is this the location npm installs the handlebar after you run npm install? This issue is break for me in github action and i am using rush which installs the packages in the temp folder that is ignored in the check in.
jarpoole commentedon Mar 4, 2024
For anyone trying to run handlebars within ts-node WITHOUT precompiling the templates (because, for example, you are using webpack's
asset/sourcemodule) you can callObject.freeze(require.extensions)after you finish your setup to prevent handlebars from making further changes.For example, running
In a new file called
handlebars.js:Now run
ts-nodeand require the above file:@nknapp I strongly agree with #1174 (comment) that the correct solution is to separate handlebars into two libs:
handlebarsandhandlebars/registerso that users can explicitly opt-in or out depending on their needs. I understand this would be a breaking change but I think it would avoid a lot of confusion going forward.Fix bundler issue with webpack 5