-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
webpack + require handlebars error #1174
Comments
As mentioned in #1102 adding the line |
Sounds like a problem with Webpack, not Handlebars. That's where a node app will assume they are located; this means Webpack is handling require(); differently than node. |
I got round this with ..
in my webpack config |
thanks @gilesbradshaw 👍 😄 |
@abhisekp was this resolved? Please close if yes. |
@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. 😊 |
issue still reproducing if I require handlebars somewhere in .js: var Handlebars = require('handlebars');
Handlebars.registerHelper('greet', function(name) {
return 'Hello, name=' + name + '!';
}); but if I remove this line var Handlebars = require('handlebars'); issue is gone |
@gilesbradshaw |
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. |
@oocx You could just import the JS file from the node_modules folder like so
This works fine! |
To get this to work in an angular 4 project I had to do this: normally I'd just do this: 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. |
I could have a look if anyone sets up a minimal example project that reproduces the issue. No promises as to the "when", though... |
@swilliams-a3digital Getting |
For others looking here This is the best solution I have found Thanks to @mAAdhaTTah UPD:
|
This is retarded. The workaround is to replace: import Handlebars from 'handlebars' with: import Handlebars from 'handlebars/dist/cjs/handlebars' |
@catamphetamine if you have a good solution to this issue, you are welcome to file a pull request. As long as it doesn't break anything, I will merge and publish a new version. Things like About the "Yet they neglect and ignore": The problem with handlebars is that there are not a lot collaborators and at the moment, as far as I can see, I am the only active one. Well, more or less active. It 23:26 now and this is the time I usually come to work on my private projects, because I have a family a daytime job and other hobbies. So when you write "Yet they neglegt and ignore", there isn't a lot of "they". There is almost certainly no "they" that is getting paid to work on this project. I help people, because it's fun to talk to people and because it is fun to solve problems, fix bugs and write tests. Because I'm using Handlebars myself in a couple of projects, and because it feels good to stand in the stage-light from time to time. But I don't feel that it is my duty to do so. So I pick the issues that I like, mostly the ones that are quick and clear. So, if you are using Handlebars and you have a problem and noone is going to solve it, maybe you should try to contribute a solution yourself. I'll reopen the issue though, because it does not seem to be solved. |
I'm doing opensource for fun too, though not as popular as this library. As for the possible solution, it would be splitting the package into Anyway, By the way, I wasn't referring specifically to you because you didn't leave a negative comment before in this issue.
And @rafde and his comment:
Your comment didn't claim Webpack being "wrong" or anything so no wrong words from your side:
You didn't say that "Webpack is doing it wrong, close the issue" so your comments are ok. |
@nknapp |
Worked for me as of this comment. I agree with @catamphetamine though. Better documentation is worth a lot and it wouldn't kill anyone to add that line to the installation page. We all build great things and want people to use them. But guess what, if the documentation sucks... so does the project because no one but you will be able to understand it. And remember... people fear what they don't understand. -end rant- I do however appreciate your efforts on the library. |
Actually, I'm not trying to make people use Handlebars. I'm just trying to help people have already decided to use it. I am currently building a new documentation site (whenever I have some time left) and I will include it there. |
Should be resolved in #1102 |
This comment from @gilesbradshaw at Jul 1, 2016 is still relevant today:
To make this work in combination with Angular 12, I had to do the following:
From: "build": {
"builder": "@angular-devkit/build-angular:browser", To: "build": {
"builder": "@angular-builders/custom-webpack:browser",
...
"options": {
"customWebpackConfig": {
"path": "./custom-webpack.config.ts"
},
...
} Note there would be other options as well. And you shouldn't literally place Also change this: "serve": {
"builder": "@angular-devkit/build-angular:dev-server", To this: "serve": {
"builder": "@angular-builders/custom-webpack:dev-server", Finally, add custom-webpack.config.ts to your project root: import { Configuration } from 'webpack';
export default {
resolve: {
alias: {
handlebars: "handlebars/dist/handlebars.min.js"
}
},
} as Configuration; Be sure to check out the README of @angular-builders/custom-webpack as there might be more changes you'd need to make in angular.json and the rest of your project. I hope this helps someone! |
Hm, strange to find out that this issue hasn't been fixed yet |
I am using handlebars library in a React Native project and running into the similar issue with the code: I replace it with the code below: It looks like it works now. Just try to give some other hints here for someone who runs into the issue. BTW, |
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: 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. facebook/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. |
@stevemk14ebr The If you want a browser-compatible version, you can try to import via You may still need to polyfill Node.js function since we didn't release the fix for #1593 yet. |
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. |
As explained in issue #1844 and in issue webpack/webpack#15007 (comment), the way we used the `browser`-field was wrong. The main reason for using the `browser`-field is the requirement of `require('fs')` in the main-entry-file. The workaround for this was using `require('handlebars/lib/handlebars')`, but now it will also work via `require('handlebars')` for bundlers that respect the `browser`-field. The `"./runtime"`-config was removed, because it didn't have any effect. In order to detect regressions, the webpack-integration test was extended to test with different webpack versions. Fixes #1174 Closes #1844
As explained in issue #1844 and in issue webpack/webpack#15007 (comment), the way we used the `browser`-field was wrong. The main reason for using the `browser`-field is the requirement of `require('fs')` in the main-entry-file. The workaround for this was using `require('handlebars/lib/handlebars')`, but now it will also work via `require('handlebars')` for bundlers that respect the `browser`-field. The `"./runtime"`-config was removed, because it didn't have any effect. In order to detect regressions, the webpack-integration test was extended to test with different webpack versions. Fixes #1174 Closes #1844
As explained in issue #1844 and in issue webpack/webpack#15007 (comment), the way we used the `browser`-field was wrong. The main reason for using the `browser`-field is the requirement of `require('fs')` in the main-entry-file. The workaround for this was using `require('handlebars/lib/handlebars')`, but now it will also work via `require('handlebars')` for bundlers that respect the `browser`-field. The `"./runtime"`-config was removed, because it didn't have any effect. In order to detect regressions, the webpack-integration test was extended to test with different webpack versions. Fixes #1174 Closes #1844
As explained in issue #1844 and in issue webpack/webpack#15007 (comment), the way we used the `browser`-field was wrong. The main reason for using the `browser`-field is the requirement of `require('fs')` in the main-entry-file. The workaround for this was using `require('handlebars/lib/handlebars')`, but now it will also work via `require('handlebars')` for bundlers that respect the `browser`-field. The `"./runtime"`-config was removed, because it didn't have any effect. In order to detect regressions, the webpack-integration test was extended to test with different webpack versions. Fixes #1174 Closes #1844
As explained in issue #1844 and in issue webpack/webpack#15007 (comment), the way we used the `browser`-field was wrong. The main reason for using the `browser`-field is the requirement of `require('fs')` in the main-entry-file. The workaround for this was using `require('handlebars/lib/handlebars')`, but now it will also work via `require('handlebars')` for bundlers that respect the `browser`-field. The `"./runtime"`-config was removed, because it didn't have any effect. In order to detect regressions, the webpack-integration test was extended to test with different webpack versions. Fixes #1174 Closes #1844
Thanks a lot @gilesbradshaw❤️. This solution is still relevant today👍🏻. |
how do i get this handlebars.min.js file? |
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 For example, running In a new file called const fs = require('fs');
require.extensions['.hbs'] = function (module, filename) {
const content = fs.readFileSync(filename, { encoding: 'utf-8' });
return module._compile(
`module.exports = ${JSON.stringify(content)}`,
filename,
);
};
Object.freeze(require.extensions); Now run npx ts-node -r ./handlebars.js script.ts @nknapp I strongly agree with #1174 (comment) that the correct solution is to separate handlebars into two libs: |
I get the following error simply by using require handlebars.
My
index.js
My
package.json
The text was updated successfully, but these errors were encountered: