找不到模块:错误:无法解析 webpack 5/Angular 14 中的"url"



使用[deepstream]运行项目时。Io-client-js][1]安装在angular 14中,出现以下错误:[1]: https://www.npmjs.com/package/deepstream.io-client-js

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "url": require.resolve("url/") }'
- install 'url'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "url": false }```

正如我们在错误中看到的,有2个步骤来添加填充,首先创建一个webpack.config.js文件tsconfig.json级别,并添加如下代码。

步骤1。webpack.config.js

module.exports = {
resolve: {
fallback: { "url": require.resolve("url/") }
}
};

步骤2。运行npm i url for- install 'url'

查看您的导入语句。确保使用:

import { Router } from '@angular/router'; 

代替express包中的router

参考原始错误信息,也可以使用webpack插件在可能的情况下自动使用polyfill:

通过运行:

将包添加到项目中
npm install node-polyfill-webpack-plugin

添加插件到webpack.config.js文件:

const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")

然后,在同一个文件中,将插件添加到config:

plugins: [
new NodePolyfillPlugin()
]

https://github.com/Richienb/node-polyfill-webpack-plugin

这个插件做了Prashant做的事情,还有很多其他类似的事情。对于有很多未解决的依赖项的大型项目,它可能很有用。

相关内容

  • 没有找到相关文章