Seeking guidance on integrating npm within a .Net Core 6 MVC project. I have included the file webpack.config.js
with the following configuration:
module.exports = {
entry: [
"babel-polyfill",
"./src/main"
],
output: {
publicPath: "/js/",
path: path.join(__dirname, "/wwwroot/js/"),
filename: "main.build.js"
}
};
In addition to the automatically generated files package.json
and package-lock.json
after running npm init
and installing libraries,
I have referenced the file main.build.js
in my HTML as follows:
<script src="~/js/main.build.js" asp-append-version="true"></script>
In my
program.cs
, I have added:app.UseDefaultFiles();
app.UseStaticFiles();
Challenges I'm encountering include:
- Receiving an error
require is not defined
when usinglet Web3 = require('web3');
inmain.build.js
- Getting the error
when converting theCannot use import statement outside a module
require
statement toimport Web3 from "web3"
- Adding
type="module"
to
results in<script src="~/js/main.build.js" type="module" asp-append-version="true"></script>
Failed to resolve module specifier "web3". Relative references must start with either "/", "./", or "../"
- Attempting to specify the relative path starting with
../../node_modules/
like
leads toimport Web3 from "../../node_modules/web3";
GET https://localhost:7101/node_modules/web3 net::ERR_ABORTED 404
- I intend to utilize npm packages in multiple js files, not just within
main.build.js
. What further modifications are needed inwebpack.config.js
?
Any pointers, tutorials, assistance, or recommendations would be greatly appreciated. A sample GitHub repository demonstrating npm usage in a .Net Core 6 MVC project would be beneficial.
My current project structure can be visualized here: https://i.sstatic.net/WcEmJ.png