带有 npm 链接的样式组件导致错误:“试图插入新的样式标签,但给定的节点已卸载”

Roo*_*ode 6 npm reactjs npm-link styled-components

我创建了自己的 npm 包(使用 nwb 创建),其中使用了样式组件。在我的消费应用程序中,我还使用了样式组件。

问题是。通过 npm install. 但是,当npm link进入其他一些 react-router 路由时使用以下错误消息:

Error: Trying to insert a new style tag, but the given Node is unmounted!

* Are you using a custom target that isn't mounted?
* Does your document not have a valid head element?
* Have you accidentally removed a style tag manually?
Run Code Online (Sandbox Code Playgroud)

在我的 npm 包中,我将样式组件设置为对等依赖项和 devDependency,如下所示:

...
"peerDependencies": {
  "react": "16.x",
  "styled-components": "^3.4.4"
},
"devDependencies": {
  "karma-junit-reporter": "^1.2.0",
  "nwb": "0.23.x",
  "react": "^16.4.2",
  "react-dom": "^16.4.2",
  "styled-components": "^3.4.4"
},
...
Run Code Online (Sandbox Code Playgroud)

为什么样式组件在使用时无法工作npm link

Shu*_*pta 5

我认为这是因为您安装了两个实例styled-components

因此删除styled-components作为 devDependencypackage.json并尝试使用,$ npm link或者如果您使用 npm > 5.1.0 尝试使用npm link --only=production这将排除开发依赖项安装。

注意:node_modules运行前请删除$ npm link