未找到模块:错误无法解析“child_process”,如何解决?

Posted

技术标签:

【中文标题】未找到模块:错误无法解析“child_process”,如何解决?【英文标题】:Module not found: Error can't resolve 'child_process', how to fix? 【发布时间】:2019-06-24 20:19:22 【问题描述】:

我正在尝试创建一个 JupyterLab 扩展,它使用打字稿。

我已经成功添加了包“@types/node”,允许我使用诸如'require('http')'之类的包。

但是,一旦我尝试使用子进程,使用 'require("child_process")' 我在尝试构建扩展时会收到以下错误。

ModuleNotFoundError: Module not found: Error: Can't resolve 'child_process' in '/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/lib'
    at factory.create (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/webpack/lib/Compilation.js:535:10)
    at factory (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/webpack/lib/NormalModuleFactory.js:397:22)
    at resolver (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/webpack/lib/NormalModuleFactory.js:130:21)
    at asyncLib.parallel (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/webpack/lib/NormalModuleFactory.js:224:22)
    at /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/neo-async/async.js:2825:7
    at /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/neo-async/async.js:6886:13
    at normalResolver.resolve (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/webpack/lib/NormalModuleFactory.js:214:25)
    at doResolve (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:184:12)
    at hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:15:1)
    at resolver.doResolve (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:37:5)
    at hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:15:1)
    at hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:12:1)
    at resolver.doResolve (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:42:38)
    at hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn41 (eval at create (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:381:1)
    at resolver.doResolve (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/ModuleKindPlugin.js:23:37)
    at hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:15:1)
    at hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:15:1)
    at args (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/forEachBail.js:30:14)
    at hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:15:1)
    at resolver.doResolve (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:37:5)
    at hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:15:1)
    at hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5) resolve 'child_process' in '/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/lib' Parsed request is a module   using description file: /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/package.json (relative path: ./lib)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/lib/node_modules doesn't exist or is not a directory
      /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/node_modules doesn't exist or is not a directory
      /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/node_modules doesn't exist or is not a directory
      /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/node_modules doesn't exist or is not a directory
      /home/fionn/anaconda3/envs/jupyterlab-ext/share/node_modules doesn't exist or is not a directory
      /home/fionn/anaconda3/envs/jupyterlab-ext/node_modules doesn't exist or is not a directory
      /home/fionn/anaconda3/envs/node_modules doesn't exist or is not a directory
      /home/fionn/anaconda3/node_modules doesn't exist or is not a directory
      /home/fionn/node_modules doesn't exist or is not a directory
      /home/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory
      looking for modules in /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules
        using description file: /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
      looking for modules in /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules
        using description file: /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/package.json (relative path: .)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
          using description file: /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/package.json (relative path: ./node_modules/child_process)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process is not a file
            .wasm
              Field 'browser' doesn't contain a valid alias configuration
              /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules/child_process doesn't exist
            .wasm
              Field 'browser' doesn't contain a valid alias configuration
              /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process.wasm doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules/child_process.wasm doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules/child_process.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules/child_process.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process.json doesn't exist
              /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules/child_process.json doesn't exist
            as directory
              existing directory
                use ./index.js from main in package.json
                  using description file: /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/package.json (relative path: .)
                    Field 'browser' doesn't contain a valid alias configuration
            as directory
              /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules/child_process doesn't exist
                    using description file: /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/package.json (relative path: ./index.js)
                      no extension
                        Field 'browser' doesn't contain a valid alias configuration
                        /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js doesn't exist
                      .wasm
                        Field 'browser' doesn't contain a valid alias configuration
                        /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js.wasm doesn't exist
                      .mjs
                        Field 'browser' doesn't contain a valid alias configuration
                        /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js.mjs doesn't exist
                      .js
                        Field 'browser' doesn't contain a valid alias configuration
                        /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js.js doesn't exist
                      .json
                        Field 'browser' doesn't contain a valid alias configuration
                        /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js.json doesn't exist
                      as directory
                        /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js doesn't exist
                using path: /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index
                  using description file: /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/package.json (relative path: ./index)
                    no extension
                      Field 'browser' doesn't contain a valid alias configuration
                      /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index doesn't exist
                    .wasm
                      Field 'browser' doesn't contain a valid alias configuration
                      /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.wasm doesn't exist
                    .mjs
                      Field 'browser' doesn't contain a valid alias configuration
                      /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.mjs doesn't exist
                    .js
                      Field 'browser' doesn't contain a valid alias configuration
                      /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js doesn't exist
                    .json
                      Field 'browser' doesn't contain a valid alias configuration
                      /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.json doesn't exist error Command failed with exit code 1.

我已经用谷歌搜索了,但仍然不知道如何解决这个问题。

任何指针或信息都将不胜感激。

我的 packages.json 文件。


  "name": "jupyerlab_xkdc",
  "version": "0.1.0",
  "description": "Short description",
  "keywords": [
    "jupyter",
    "jupyterlab",
    "jupyterlab-extension"
  ],
  "homepage": "https://github.com/my_name/myextension",
  "bugs": 
    "url": "https://github.com/my_name/myextension/issues"
  ,
  "license": "BSD-3-Clause",
  "author": "Fionn McKnight",
  "files": [
    "lib/**/*.d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf",
    "style/**/*.css,eot,gif,html,jpg,json,png,svg,woff2,ttf"
  ],
  "main": "lib/index.js",
  "types": "lib/index.d.ts",
  "repository": 
    "type": "git",
    "url": "https://github.com/my_name/myextension.git"
  ,
  "scripts": 
    "build": "tsc",
    "clean": "rimraf lib",
    "prepare": "npm run clean && npm run build",
    "watch": "tsc -w"
  ,
  "dependencies": 
    "@jupyterlab/application": "^0.19.1",
    "@jupyterlab/apputils": "^0.19.1",
    "@phosphor/coreutils": "^1.3.0",
    "@phosphor/messaging": "^1.2.2",
    "@phosphor/widgets": "^1.6.0",
    "@types/jquery": "^3.3.29",
    "@types/node": "^10.12.19",
    "child_process": "^1.0.2",
    "npm": "^6.7.0"
  ,
  "devDependencies": 
    "@types/node": "^10.12.19",
    "rimraf": "^2.6.1",
    "typescript": "~3.1.1"
  ,
  "jupyterlab": 
    "extension": true
  

【问题讨论】:

这甚至是全栈错误吗? 你能告诉我们你的 package.json 吗? 是的,当我转到文件夹 'child_process' 时,它是空的,除了一个自述文件指出“此包当前未使用,但以前被另一个包占用”。 添加了我的 packages.json 我想知道***.com/a/59381357/470749是否相关。 【参考方案1】:

只需放入 package.json

"browser":
"child_process": false
  

你在使用 webpack

【讨论】:

我添加了,不再出现错误,谢谢。抱歉,我对这一切都很陌生,我不确定我是否使用 webpack,我怎么知道?另外,你能解释一下为什么我需要将它添加到我的 package.json 中吗?谢谢! browser: child_process: false 添加到 package.json 意味着:告诉 webpack 模块可以为这个 modules.weback 隐式获取一个空对象由 node_modules 使用..您也可以显式使用它。如果您显式使用,则必须在 webpack.config.js 文件中定义它配置 好的,但我不希望它是一个空对象,我有'const spawn = require("child_process").spawn',现在返回未定义。 使用 node.js 内部子进程模块。这可能会有所帮助或尝试同步版本的 spawn 这可能会有所帮助 [nodejs.org/api/child_process.html] 同样的问题 [***.com/questions/44305603/…]【参考方案2】:

我在尝试运行使用 webpackstorybook.js 时遇到了类似的错误。

对于 Webpack

您可以将以下内容添加到 webpack 的配置中以修复构建 -

// webpack.config.js
...,
node: 
    child_process: "empty"
    // fs: "empty", // if unable to resolve "fs"
,

您也可以将其他冲突模块设置为"empty",如果它们负责破坏构建。

Reference for Webpack's config.node

【讨论】:

【参考方案3】:

在我使用的较新版本中:

// webpack.config.js
...,
resolve: 
    extensions: [".ts", ".js"],
    fallback: 
      "child_process": false,
      // and also other packages that are not found
    
  ,

【讨论】:

【参考方案4】:

在使用 next.js 时随机遇到这个问题 我通过清除解决了它,

.next
node_modules 

文件夹 猜猜它可能适用于在下一个 js 中遇到此客户端进程错误的人

【讨论】:

遇到了同样的问题...你是如何清除它们的?重新创建应用程序? 我刚刚清除了答案中提到的文件夹。当我执行 npm i 和 npm run 时,它没有任何问题。 感谢您的反馈。我的问题是 firebase 的导入不正确。我从其他地方复制/粘贴了导入语句,这导致了同样的错误。也许它可以帮助某人。 :)【参考方案5】:

在页面中导入pathfs 后,我在next.js 中得到了这个。

通过将它们放在getStaticProps() 函数中来解决它。

【讨论】:

【参考方案6】:

同样的事情发生

找出原因

错误地将导入添加到创建问题的文件中

import  debug  from "webpack";

【讨论】:

【参考方案7】:

我在这件事上被屏蔽了几次。 据我了解,从浏览器执行一些 child_process (实际上是在执行一些 cmd )似乎是不可能的,这在某些时候也是有意义的。 给出的解决方案对我有用,在下面添加到package.json 正在工作

  "browser":  "fs": false, "child_process": false 

但是不要将它添加到您的 package.json 中(意味着您的应用程序之一),而是将 package.json 从库中添加到您正在使用的位置 require('child_process') 是。

这已经完成了,这个修复只是对编译器说即使你的代码或依赖项的某处需要这个库(而不是崩溃)也可以没有库。如果你想在你的 App 中使用 child_process 恐怕会很复杂。

这是我帖子的source

【讨论】:

以上是关于未找到模块:错误无法解析“child_process”,如何解决?的主要内容,如果未能解决你的问题,请参考以下文章

未找到模块:错误:无法解析 JSON

“未找到模块:错误:无法解析模块”错误在 signrequest-client 角度

未找到模块:错误:无法使用 Webpack 解析“fs”

Angular 构建 - 未找到模块:错误:无法解析“控制台”

未找到模块:错误:无法解析“./router”| Vue路由器

未找到模块:无法解析“