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

One*_*Two 20 node.js npm typescript jupyter-lab

我正在尝试创建一个 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) 在工厂 (/home/fionn/anaconda3/envs/jupyterlab-ext/ share/jupyter/lab/staging/node_modules/webpack/lib/NormalModuleFactory.js:397:22) 在解析器 (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/webpack/ lib/NormalModuleFactory.js:130:21) 在 asyncLib.parallel (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/webpack/lib/NormalModuleFactory.js:224:22) 在 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/neo-async/async.js:2825:7 在 /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) 在 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), :15:1) 在解析器。doResolve (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:37:5) 在 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), :15:1) 在 hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter /lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5)在 _fn0(在创建时评估(/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules) /tapable/lib/HookCodeFactory.js:32:10), :12:1) 在解析器。doResolve (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:42:38) 在 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), :381:1) 在 resolver.doResolve (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter /lab/staging/node_modules/enhanced-resolve/lib/ModuleKindPlugin.js:23:37) 在 hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced -resolve/lib/Resolver.js:238:5)在_fn0(在创建时评估(/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/tapable/lib/HookCodeFactory.js:32:10),:15:1)在 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), :15:1) 在 args (/home/fionn/anaconda3/ envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/forEachBail.js:30:14) 在 hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/share/ jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5)在_fn0(在创建时评估(/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/tapable/lib/HookCodeFactory.js:32:10),:15:1)在 resolver.doResolve (/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:37:5) 在 hook.callAsync (/home/ fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5)在_fn0(在创建时评估(/home/fionn/anaconda3/envs/ jupyterlab-ext/share/jupyter/lab/staging/node_modules/tapable/lib/HookCodeFactory.js:32:10), :15:1) 在 hook.callAsync (/home/fionn/anaconda3/envs/jupyterlab-ext/ share/jupyter/lab/staging/node_modules/enhanced-resolve/lib/Resolver.js:238:5) 解析 'child_process'/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/lib' 解析请求是使用描述文件的模块:/home/fionn/anaconda3/envs/jupyterlab-ext/share /jupyter/lab/staging/node_modules/jupyerlab_xkdc/package.json(相对路径:./lib)字段“浏览器”不包含有效的别名配置解析为模块/home/fionn/anaconda3/envs/jupyterlab-ext/ share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/lib/node_modules 不存在或不是目录 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/node_modules 不存在' t 存在或不是目录 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/node_modules 不t 存在或不是目录 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/node_modules 不存在或不是目录 /home/fionn/anaconda3/envs/jupyterlab-ext/share/node_modules不存在或不是目录 /home/fionn/anaconda3/envs/jupyterlab-ext/node_modules 不存在或不是目录 /home/fionn/anaconda3/envs/node_modules 不存在或不是目录 /home/fionn/anaconda3/node_modules 不存在或不是目录 /home/fionn/node_modules 不存在或不是目录 /home/node_modules 不存在或不是目录 /node_modules 不存在't 存在或不是使用描述文件在 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules 中查找模块的目录:/home/fionn/anaconda3/envs/ jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/package.json(相对路径:./node_modules)字段“浏览器”不包含在 /home/fionn/anaconda3/ 中查找模块的有效别名配置envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules 使用描述文件:/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/package.json(相对路径:./node_modules ) 字段“浏览器”不包含使用描述文件的有效别名配置:/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/package.json(相对路径:.)无扩展名字段“浏览器”不包含使用描述的有效别名配置文件:/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/package.json(相对路径:./node_modules/child_process)无扩展名字段“浏览器”不包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process 不是文件 .wasm 字段“浏览器”不t 包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules/child_process 不存在 .wasm 字段“浏览器”不包含有效别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process.wasm 不存在 .mjs 字段“浏览器”不包含有效的别名配置 /home/fionn /anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules/child_process.wasm 不存在 .mjs 字段“浏览器”不存在t 包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process.mjs 不存在 .js 字段“浏览器”不包含有效的别名配置/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules/child_process.mjs 不存在 .js 字段“浏览器”不包含有效的别名配置 /home /fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process.js 不存在 .json 字段“浏览器”不存在t 包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules/child_process.js 不存在 .json 字段“浏览器”不包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process.json 不存在 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter /lab/staging/node_modules/jupyerlab_xkdc/node_modules/child_process.json 不存在,因为目录现有目录使用 package.json 中 main 中的 ./index.js 使用描述文件:/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/package.json(相对路径:.)字段“浏览器”不包含作为目录/home的有效别名配置/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/jupyerlab_xkdc/node_modules/child_process 不存在使用描述文件:/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter /lab/staging/node_modules/child_process/package.json(相对路径:./index.js)无扩展名字段'浏览器'不包含有效的别名配置/home/fionn/anaconda3/envs/jupyterlab-ext/share /jupyter/lab/staging/node_modules/child_process/index.htmljs 不存在 .wasm 字段“浏览器”不包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js.wasm 不'不存在 .mjs 字段 'browser' 不包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js.mjs 不包含存在 .js 字段“浏览器”不包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js.js 不包含t 存在 .json 字段“浏览器”不包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js.json 不存在作为目录 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js 不存在使用路径:/home/fionn/anaconda3/envs/jupyterlab-ext/ share/jupyter/lab/staging/node_modules/child_process/index 使用描述文件:/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/package.json(相对路径:. /index) 无扩展字段 'browser' 不包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index 不存在 .wasm 字段 'browser' 不包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.wasm 不存在 .mjs 字段“浏览器”不包含有效的别名配置/home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.mjs 不存在 .js 字段“浏览器”不存在t 包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.js 不存在 .json 字段“浏览器”不包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.json 不存在错误命令失败,退出代码为 1。t 包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.json 不存在错误命令失败,退出代码为 1。t 包含有效的别名配置 /home/fionn/anaconda3/envs/jupyterlab-ext/share/jupyter/lab/staging/node_modules/child_process/index.json 不存在错误命令失败,退出代码为 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
  }
}
Run Code Online (Sandbox Code Playgroud)

Shu*_*ari 28

只需放入 package.json

"browser":{
"child_process": false
}  
Run Code Online (Sandbox Code Playgroud)

你在用 webpack

  • 将 **browser: { child_process: false }** 添加到 package.json 意味着:告诉 webpack 模块可以为这个模块获取一个空对象。它也很明确。如果您明确使用,则必须在 webpack.config.js 文件中定义它的配置 (7认同)
  • 好的,但我不希望它是一个空对象,我有'const spawn = require("child_process").spawn',它现在返回未定义。 (2认同)

Ant*_*nen 14

在较新的版本中我使用:

// webpack.config.js
...,
resolve: {
    extensions: [".ts", ".js"],
    fallback: {
      "child_process": false,
      // and also other packages that are not found
    }
  },
Run Code Online (Sandbox Code Playgroud)


Nik*_*hak 7

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

对于 Webpack

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

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

Run Code Online (Sandbox Code Playgroud)

您还可以将其他冲突模块设置为"empty"是否负责破坏构建。

Webpack 的 config.node 参考


小智 5

我在这件事上被封锁了好几次。据我了解,从浏览器执行某些 child_process (实际上是执行某些 cmd )似乎是不可能的,这在某些时候也是有意义的。给出的解决方案对我有用,添加以下内容package.json即可

  "browser": { "fs": false, "child_process": false }
Run Code Online (Sandbox Code Playgroud)

不要将其添加到您的 package.json (指的是您的应用程序之一)上,而是package.json从 lib 中添加它,然后您正在使用它 require('child_process')

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

这是我帖子的来源