Search code examples
javascriptnode.jsyeomanyeoman-generatorstenciljs

Error: Cannot find Module '../utils/composeObjs' after installing generator-stencil globally


I'm writing a generator for stenciljs at https://github.com/AkashGutha/generator-stencil

I have a small utility function sitting at the root of the project. It's just one file utils/composeObjects.js that exports one function composeObjs. I'm using this function in all the sub-generators in the generator. I'm using it in these place:

  • app/index.js
  • page/index.js
  • component/index.js

I'm testing my generator locally using npm link and then when i do yo stencil in a empty folder the generator works without any issue.

However when i npm publish it and try to run yo stencil it throws the following error.

module.js:538
    throw err;
    ^

Error: Cannot find module '../utils/composeObjects'
    at Function.Module._resolveFilename (module.js:536:15)
    at Function.Module._load (module.js:466:25)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/Akash/.npm-global/lib/node_modules/generator-stencil/app/index.js:5:21)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)

after searching i found out that require doesn't work as expected when a package is installed globally. But, i've looked at some other generators on github and some of them use some kind of util function and they work fine when installed globally.

I'm also suspecting that this might be because the main module running is the main yeoman, because the yo stencil tells the yeoman cli to use that generator.

I need to get rid of this error also would be niec to have some insight into what causes this module error.

Here is the github issue: https://github.com/AkashGutha/generator-stencil/issues/1


Solution

  • Sounds like your generator might be removing your util file from the npm bundle. This might be cause by an .npmignore file or if the file/folder isn't included in your package.json files property.