Skip to content

webpack + require handlebars error #1174

Closed

Description

@abhisekp

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

var handlebars = require('handlebars');

My package.json

  "dependencies": {
    "handlebars": "^4.0.5"
  }

Activity

  1. benjeffery commented on Feb 4, 2016

    @benjeffery

    As mentioned in #1102 adding the line
    "browser": "dist/handlebars.js",
    to handlebars' package.json solved this for me.

  2. methodbox commented on May 15, 2016

    @methodbox

    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.

  3. gilesbradshaw commented on Jul 1, 2016

    @gilesbradshaw

    I got round this with ..

    resolve: {
        alias: {
           handlebars: 'handlebars/dist/handlebars.min.js'
        }
    }
    

    in my webpack config

  4. abhisekp commented on Jul 1, 2016

    @abhisekp
    Author

    thanks @gilesbradshaw 👍 😄

  5. rafde commented on Aug 27, 2016

    @rafde

    @abhisekp was this resolved? Please close if yes.

  6. abhisekp commented on Aug 27, 2016

    @abhisekp
    Author

    @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. 😊

  7. nkonev commented on Oct 24, 2016

    @nkonev

    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

  8. flyher commented on Nov 8, 2016

    @flyher

    @gilesbradshaw
    Thank you very much!

  9. oocx commented on Dec 12, 2016

    @oocx

    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.

  10. abdel-ships-it commented on May 11, 2017

    @abdel-ships-it

    @oocx You could just import the JS file from the node_modules folder like so

    import 'handlebars/dist/handlebars.min.js';

    This works fine!

  11. swilliams-a3digital commented on Oct 20, 2017

    @swilliams-a3digital

    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.

  12. nknapp commented on Oct 20, 2017

    @nknapp
    Collaborator

    I could have a look if anyone sets up a minimal example project that reproduces the issue. No promises as to the "when", though...

  13. JannicBeck commented on Jun 12, 2018

    @JannicBeck

    @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?

  14. seyfer commented on Jun 14, 2018

    @seyfer

    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:

    //fix handlebars warnings
    config.resolve.alias = {
        ...config.resolve.alias,
        'handlebars/runtime': 'handlebars/dist/cjs/handlebars.runtime',
        'handlebars': 'handlebars/dist/cjs/handlebars.runtime',
    };
    
  15. 11 remaining items

  16. frankguo-dyedurham commented on Dec 31, 2021

    @frankguo-dyedurham

    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.

  17. added a commit that references this issue on Mar 20, 2022
  18. stevemk14ebr commented on Mar 23, 2022

    @stevemk14ebr

    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'
    

    the main script does this:

    function extension(module, filename) {
    var fs = require('fs');
    var templateString = fs.readFileSync(filename, 'utf8');
    module.exports = handlebars.compile(templateString);
    }

    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:

    "browser": {
        "./lib/index.js": "./dist/cjs/handlebars.js",          /// <------ this
        "./runtime": "./dist/cjs/handlebars.runtime.js"
      },
    

    However, this also still breaks because the dist folder doesn't exist! What is going on? I am so lost.

  19. jaylinski commented on Mar 23, 2022

    @jaylinski
    Member

    @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.

  20. stevemk14ebr commented on Mar 23, 2022

    @stevemk14ebr

    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:

    "browser": {
        "./lib/index.js": "./dist/cjs/handlebars.js",
        "./runtime": "./dist/cjs/handlebars.runtime.js"
      },
    

    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.

  21. added 5 commits that reference this issue on May 15, 2022
    6a07b62
    a8261b2
    9bff03f
    c6c6bbb
    e3a5448
  22. satyajitnayk commented on Sep 22, 2023

    @satyajitnayk

    I got round this with ..

    resolve: {
        alias: {
           handlebars: 'handlebars/dist/handlebars.min.js'
        }
    }
    

    in my webpack config

    Thanks a lot @gilesbradshaw❤️. This solution is still relevant today👍🏻.

  23. patricknwanah commented on Oct 17, 2023

    @patricknwanah

    how do i get this handlebars.min.js file?

  24. patricknwanah commented on Oct 17, 2023

    @patricknwanah

    I got round this with ..

    resolve: {
        alias: {
           handlebars: 'handlebars/dist/handlebars.min.js'
        }
    }
    

    in my webpack config

    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.

  25. jarpoole commented on Mar 4, 2024

    @jarpoole

    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.

    For example, running

    In a new file called handlebars.js:

    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 ts-node and require the above file:

    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: 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.

  26. added a commit that references this issue on Sep 9, 2026
    d31dba5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions