0

假设我们在存储库中generate-mygenerator。因此,我们generator.js在根目录中有一个文件。

如果我想跑步gen gh-repo怎么办?

我收到以下错误:

Error: Cannot find module 'lazy-cache'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/remi/d/dev/generate-package/utils.js:3:13)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
/home/remi/d/dev/generate/bin/generate.js:78
if (err) return app.emit('error', err);
                  ^

TypeError: Cannot read property 'emit' of undefined
at /home/remi/d/dev/generate/bin/generate.js:78:23
at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/base-runner/index.js:122:7)
at Liftoff.execute (/home/remi/d/dev/generate/node_modules/liftoff/index.js:203:12)
at module.exports (/home/remi/d/dev/generate/node_modules/flagged-respawn/index.js:51:3)
at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/liftoff/index.js:195:5)
at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/liftoff/index.js:170:7)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
at Function.Module.runMain (module.js:686:11)
at startup (bootstrap_node.js:187:16)

生成器完成后我是否要重命名generator.js然后重命名?generator.js.wait

重现错误的步骤:

存储库中的示例generate-package

remi@poremil:~
$ cd d/dev/generate-package/
remi@poremil:~/d/dev/generate-package:master
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
remi@poremil:~/d/dev/generate-package:master
$ node -v
v8.9.4
remi@poremil:~/d/dev/generate-package:master
$ npm -v
5.6.0
remi@poremil:~/d/dev/generate-package:master
$ ls
docs  generator.js  index.js  LICENSE  package.json  README.md  templates  test  utils.js

运行generate-gh-repo

remi@poremil:~/d/dev/generate-package:master
$ gen gh-repo
[23:33:06] starting generate
[23:33:06] using generator ~/d/dev/generate-package/generator.js
Error: Cannot find module 'lazy-cache'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/remi/d/dev/generate-package/utils.js:3:13)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
/home/remi/d/dev/generate/bin/generate.js:78
  if (err) return app.emit('error', err);
                      ^

TypeError: Cannot read property 'emit' of undefined
    at /home/remi/d/dev/generate/bin/generate.js:78:23
    at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/base-runner/index.js:122:7)
    at Liftoff.execute (/home/remi/d/dev/generate/node_modules/liftoff/index.js:203:12)
    at module.exports (/home/remi/d/dev/generate/node_modules/flagged-respawn/index.js:51:3)
    at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/liftoff/index.js:195:5)
    at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/liftoff/index.js:170:7)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    at Function.Module.runMain (module.js:686:11)
    at startup (bootstrap_node.js:187:16)

或相同的运行generate-git

remi@poremil:~/d/dev/generate-package:master
$ gen git
[23:33:16] starting generate
[23:33:16] using generator ~/d/dev/generate-package/generator.js
Error: Cannot find module 'lazy-cache'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/remi/d/dev/generate-package/utils.js:3:13)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
/home/remi/d/dev/generate/bin/generate.js:78
  if (err) return app.emit('error', err);
                      ^

TypeError: Cannot read property 'emit' of undefined
    at /home/remi/d/dev/generate/bin/generate.js:78:23
    at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/base-runner/index.js:122:7)
    at Liftoff.execute (/home/remi/d/dev/generate/node_modules/liftoff/index.js:203:12)
    at module.exports (/home/remi/d/dev/generate/node_modules/flagged-respawn/index.js:51:3)
    at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/liftoff/index.js:195:5)
    at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/liftoff/index.js:170:7)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    at Function.Module.runMain (module.js:686:11)
    at startup (bootstrap_node.js:187:16)
4

1 回答 1

0

原则

只要全局安装并且您的文件中generate-gh-repo没有指定任务,全局生成器就应该运行。gh-repogenerator.js

如果您的generator.js文件中有一个任务与您尝试运行的全局安装的生成器同名,则该任务将运行。这是因为 generate 将以比全局生成器更高的优先级解决任务。

您可以通过运行 gen 来运行全局生成器gh-repo:default(替换gh-repo为您尝试运行的实际生成器)。这告诉 generate 在生成器上查找默认任务gh-repo。如果您没有像这样gh-repogenerator.js文件中注册生成器:

app.register('gh-repo', require('generate-gh-repo'));

然后 generate 应该找到全局安装的生成器。

它应该工作

当您运行 gen gh-repo 时,它应该可以正常工作。

为避免此错误,只需安装本地 npm 依赖项

我只是重现了错误行为,只是试图在尚未安装本地节点模块的其他生成器的根文件夹中运行全局生成器!

存储库中的示例generate-package

安装 npm 依赖项:

remi@poremil:~/d/dev/generate-package:master
$ npm install
npm WARN registry Unexpected warning for https://registry.npmjs.org/: Miscellaneous Warning EINTEGRITY: sha1-x6vpzIuHwLqodrGf3oP9RkeX44w= integrity checksum failed when using sha1: wanted sha1-x6vpzIuHwLqodrGf3oP9RkeX44w= but got sha512-D5JUjPyJbaJDkuAazpVnSfVkLlpeO3wDlPROTMLGKG1zMFNFRgrciKo1ltz/AzNTkqE0HzDx655QOL51N06how==. (4087 bytes)
npm WARN registry Using stale package data from https://registry.npmjs.org/ due to a request error during revalidation.
npm notice created a lockfile as package-lock.json. You should commit this file.
added 982 packages in 38.844s

并再次运行generate-gh-repo

remi@poremil:~/d/dev/generate-package:master
$ gen gh-repo
[02:31:11] starting generate
[02:31:11] using generator ~/d/dev/generate-package/generator.js
[02:31:11] ✔ running tasks: [ 'gh-repo' ]
[02:31:11] starting gh-repo
[02:31:11] starting gh-repo:default task
[02:31:11] starting gh-repo:gh-repo task

? Found saved GitHub authentication. Would you like to use it? (Y/n)

generate-git

remi@poremil:~/d/dev/generate-package:master
$ gen git
[02:31:00] starting generate
[02:31:01] using generator ~/d/dev/generate-package/generator.js
[02:31:01] ✔ running tasks: [ 'git' ]

问题已经解决了 !

于 2018-01-24T02:45:38.273 回答