Bundling amazon-sp-api with webpack - javascript

I'm, trying to generate a bundle from a javascript to get some requests from Amazon Selling Partner API. Actually, I have a mockup of a getOrders request which works in Node.js by using the
[amazon-sp-api client package] (https://www.npmjs.com/package/amazon-sp-api#setting-credentials-from-environment-variables).
As I need to get the script working in Oracle Netsuite, I would need to copy that script to the Netsuite account as a unique-file bundle.
So, I have generated the bundle I need but it launches a TypeError when trying to execute it as a unique file with Node. Indeed, the error header looks like this:
...readdirSync(__dirname + '/resources').reduce((eps, ep) => {
^
TypeError: readdirSync is not a function
It appears to be some issue about my webpack.config.js as I'm using a basic config and it probably requires specific details to lead with the dependencies.
There is a dependency I use in my script, which at once use third-party dependencies:
const SellingPartnerAPI = require('amazon-sp-api');
My webpack.config.js looks like this:
const path = require('path');
//const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
//This property defines where the application starts
entry:'./app.js',
//This property defines the file path and the file name which will be used for deploying the bundled file
output:{
path: path.join(__dirname, '/dist'),
filename: 'get_orders.js'
},
//Setup loaders
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
},
// Setup plugin to use a HTML file for serving bundled js files
plugins: [
/* new HtmlWebpackPlugin({
template: './src/index.html'
})*/
]
}
And the package.json is this:
{
"name": "orders-importer-bundle",
"version": "1.0.0",
"description": "Project to create a test blundle to import orders from Amazon to Netsuite using SP-API and SuiteScript.",
"main": "app.js",
"scripts": {
"pack": "webpack-dev-server --mode development --open --hot",
"dev": "webpack --mode development",
"build": "webpack --mode production",
"start": "node ./app.js"
},
"repository": {
"type": "git",
"url": "main"
},
"keywords": [
"webpack",
"bundle",
"netsuite",
"amazon",
"sp-api"
],
"author": "Roberto Carlos Rodriguez",
"license": "ISC",
"dependencies": {
"amazon-sp-api": "^0.7.1"
},
"devDependencies": {
"#babel/cli": "^7.17.6",
"#babel/core": "^7.17.7",
"#babel/preset-env": "^7.16.11",
"#babel/preset-react": "^7.16.7",
"babel-core": "^6.26.3",
"babel-loader": "^8.2.3",
"html-webpack-plugin": "^5.5.0",
"babel-preset-env": "^1.7.0",
"webpack": "^5.70.0",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.7.4"
}
}

Related

Cannot publish app via github pages using webpack, why?

I'm building a simple application using Tailwindcss, JavaScript & Webpack and trying to publish it via Github Pages.
While running the app via local host works fine, I am encountering a problem when trying to publish the app via gh-pages. I'm receiving the error message "404 File not found", why is that? Are there any dependencies missing in the webpack.config.js or package-json file?
Any help is appreciated.
Please also see github repo: https://github.com/e-d-i/shoppingCart
package.json
"name": "shopping-cart",
"version": "1.0.0",
"description": "A simple shopping cart using HTML, CSS / Tailwind CSS, JavaScript & Webpack",
"main": "webpack.config.js",
"dependencies": {
"css-loader": "^6.3.0",
"gh-pages": "^3.2.3",
"mini-css-extract-plugin": "^2.3.0",
"postcss": "^8.3.9",
"postcss-loader": "^6.1.1",
"tailwindcss": "^2.2.16",
"webpack": "^5.56.1",
"webpack-cli": "^4.8.0"
},
"homepage": "https://e-d-i.github.io/shoppingCart/",
"devDependencies": {},
"scripts": {
"build": "webpack --config webpack.config.js",
"deploy": "gh-pages -d build"
},
"keywords": [],
"author": "e-d-i",
"license": "ISC"
}```
**postcss.config.js**
```const tailwindcss = require("tailwindcss");
module.exports = {
plugins: [
tailwindcss
],
};```
**webpack.config.js**
```const path = require("path");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
mode: "development",
entry: "./src/script.js",
output: {
filename: "main.js",
path: path.resolve(__dirname,"./build")
},
plugins: [new MiniCssExtractPlugin({
filename: "styles.css",
})],
module: {
rules: [
{
test:/\.css$/,
use:[
MiniCssExtractPlugin.loader,
"css-loader",
"postcss-loader"
]
}
]
}
}```
You should add a "predeploy" in your package.json like so:
"predeploy": "npm run build"
and run it locally before you deploy to github pages.
source: https://dev.to/yuribenjamin/how-to-deploy-react-app-in-github-pages-2a1f

Webpack not building the most recent changes

I am working on a fairly simple project from https://medium.com/ethereum-developers/the-ultimate-end-to-end-tutorial-to-create-and-deploy-a-fully-descentralized-dapp-in-ethereum-18f0cf6d7e0e
Since the tutorial doesn't focus on the frontend part(webpack and babel and other things), I picked up these steps from different places.
Now I was trying to build the front using webpack and http-server, but I realized that it is not updating with the changes that I am making to the file.
webpack.config.js
const path = require('path')
module.exports = {
entry: path.join(__dirname, 'src/js', 'index.js'), // Our frontend will be inside the src folder
output: {
path: path.join(__dirname, 'dist'),
filename: 'build.js' // The final file will be created in dist/build.js
},
module: {
rules: [{
test: /\.css$/, // To load the css in react
use: ['style-loader', 'css-loader'],
include: /src/
}, {
test: /\.jsx?$/, // To load the js and jsx files
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['#babel/preset-env', '#babel/preset-react']
}
}]
}
}
package.json
{
"name": "test-app",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.10.2",
"#babel/preset-env": "^7.10.2",
"#babel/preset-react": "^7.10.1",
"babel-loader": "^8.1.0",
"css-loader": "^3.5.3",
"json-loader": "^0.5.7",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"style-loader": "^1.2.1",
"web3": "^0.20.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
},
"directories": {
"test": "test"
},
"dependencies": {},
"description": ""
}
I build it using
npx webpack --config webpack.config.js
and then serve it
http-server dist/
How do I fix this? And is this even the right way to do it?
Thanks.
U have already webpack-cli installed in your dependencies so u dont have to add config in command:
First Add Webpack Script in your Package.json:
"scripts": {
"watch": "webpack --watch",
},
When u run npm run watch --watch webpack will continue to watch for changes in any of the resolved files.
And for Server I recommend you webpack-dev-server
npm i webpack-dev-server
can be used to quickly develop an application
module.exports = {
//...
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000
}
};
And add it to your npm script
"scripts": {
"watch": "webpack --watch",
"start": "webpack-dev-server --hot --open",
}
Now we can run npm start from the command line and we will see our browser automatically loading up our page. If you now change any of the source files and save them, the web server will automatically reload after the code has been compiled.
Advise: you must add html file in dist or plugins for webpack HtmlWebpackPlugin

page doesn't load on refresh - react-router-dom

I have basic react app with the following pages: Home, Profile, contact, and experience. I have set up the routes for each page but when I go to a different page other than home, it renders but when I refresh page, the page doesn't load.
I noticed if I add the # before the page name, for example http://localhost:1234/#/profile the page renders. So i'm confused why I need to the # which I don't want and I'm using react-router-dom, so there is no need for #.
I added the historyApiFallback to my webpack.config but that doesn't work. Can anyone help me with this? I'm new to react and I want to learn as much as possible. Your help will be appreciated!
App.jsx
import React, { Component } from "react";
import Navbar from "./components/navbar";
import Intro from "./components/introPage";
import Experience from "./components/experiencePage";
import Profile from "./components/profilePage";
import Contact from "./components/contactPage";
import { BrowserRouter as Router, Link, Route } from 'react-router-dom';
class App extends Component {
render(){
return (
<Router>
<div className="pageSections">
<Navbar />
<div className="navContent">
<Route exact path="/" component={Intro}/>
<Route path="/experience" component={Experience}/>
<Route path="/profile" component={Profile}/>
<Route path="/contact" component={Contact}/>
</div>
</div>
</Router>
);
}
}
export default App;
navbar.jsx
import React, { Component } from "react";
import { Link } from "react-router-dom";
class Navbar extends Component {
render(){
return (
<div className="navFrame">
<Link to="/">
<div className="topNav"><div className="navBar"><h3>Marteen</h3></div></div>
</Link>
<Link to="/profile">
<div className="rightNav"><div className="navBar"><h3>Profile</h3></div></div>
</Link>
<Link to="/experience">
<div className="bottomNav"><div className="navBar"><h3>Experience</h3></div></div>
</Link>
<Link to="/contact">
<div className="leftNav"><div className="navBar"><h3>Contact</h3></div></div>
</Link>
</div>
);
}
}
export default Navbar;
webpack.config.js
const webpack = require('webpack');
const config = {
entry: __dirname + '/js/index.jsx',
output: {
path: __dirname + '/dist',
filename: 'bundle.js',
},
resolve: {
extensions: ['.js', '.jsx', '.css']
},
module: {
rules: [
{
test: /\.jsx?/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.scss?/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
devServer: {
historyApiFallback: true,
contentBase: './',
hot: true
}
};
module.exports = config;
package.json
{
"main": "index.js",
"scripts": {
"build": "webpack -p --progress --config webpack.config.js",
"dev-build": "webpack --progress -d --config webpack.config.js",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --progress -d --config webpack.config.js --watch",
"start": "npm run open",
"open": "concurrently \"http-server -a localhost -p 1234\" \"open http://localhost:1234\""
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"concurrently": "^3.6.1",
"css-loader": "^0.28.11",
"http-server": "^0.11.1",
"node-sass": "^4.7.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"sass-loader": "^6.0.7",
"style-loader": "^0.20.3",
"webpack": "^4.1.1",
"webpack-cli": "^2.0.12"
},
"dependencies": {
"npm": "^5.10.0"
}
}
Update
package.json
{
"name": "fullstack_profile",
"version": "1.0.0",
"description": "fullstack profile with flask and react",
"main": "index.js",
"scripts": {
"build": "webpack -p --progress --config webpack.config.js",
"dev-build": "webpack --progress -d --config webpack.config.js",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack-dev-server --hot --progress --mode development",
"start": "npm run open",
"open": "concurrently \"http-server -a localhost -p 1234\" \"open http://localhost:1234\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/medev21/fullstack_profile.git"
},
"author": "martin",
"babel": {
"presets": [
"es2015",
"react"
]
},
"license": "ISC",
"bugs": {
"url": "https://github.com/medev21/fullstack_profile/issues"
},
"homepage": "https://github.com/medev21/fullstack_profile#readme",
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"concurrently": "^3.6.1",
"css-loader": "^0.28.11",
"http-server": "^0.11.1",
"node-sass": "^4.7.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"sass-loader": "^6.0.7",
"style-loader": "^0.20.3",
"webpack": "^4.1.1",
"webpack-cli": "^2.0.12",
"webpack-dev-server": "^3.1.5"
},
"dependencies": {
"npm": "^5.10.0"
}
}
webpack.config
const webpack = require('webpack');
const config = {
entry: __dirname + '/js/index.jsx',
output: {
path: __dirname + '/dist',
filename: 'bundle.js',
},
resolve: {
extensions: ['.js', '.jsx', '.css']
},
module: {
rules: [
{
test: /\.jsx?/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.scss?/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
devServer: {
contentBase: __dirname + '/dist',
compress: false,
port: 1234,
historyApiFallback: {
index: 'index.html'
}
}
};
module.exports = config;
The reason why http://localhost:1234/#/profile works is because # doesn't reload the page. It behaves the same as an anchor tag in HTML where you stay on the same page but scroll to a specific section of it. For example, the below scrolls you to the portfolio section of the page.
Portfolio
If you omit the #, this is different. This tells your server to reload the page and visit that location. In your case http://localhost:1234/profile is unknown by the server as you have not specified it on the server side. In these cases, you need to either create the route or proxy the request when the route is not found.
As you are using react-router which is a client-side router, the same file needs to be served by the server and therefore you should go with the proxy option.
When using http-server the docs say you can add the -P or --proxy flag to specify it.
-P or --proxy Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com
In your case update the below in your package.json.
"open": "concurrently \"http-server -a localhost -p 1234 -P http://localhost:1234\" \"open http://localhost:1234\""
An alternative is to use webpack-dev-server for development. It'll improve your developers experience a lot as it supports hot reloading when a component changes.
install it using npm install --save-dev webpack-dev-server
Add the below to your webpack config.
devServer: {
contentBase: __dirname + '/dist',
compress: false,
port: 1234,
historyApiFallback: {
index: 'index.html' // assuming this is your entry point file that loads your bundle.
}
},
Then in your package.json add a script for it and run it using npm run watch
"watch": "webpack-dev-server --hot --progress --mode development",
NOTE: Make sure that in your dist folder index.html is present. If it isn't you will run into issues.
I've created a basic project on GitHub so you have a working example.
Solution to Express and at least to Heroku deployment
I want to share my solution that finally allowed the user to access every link in my React App.
After reading these proposed solutions and this helpful article, I could manage to make this works with this following code in my main server.js file:
// Serve static files such as images, CSS files, and JavaScript files for the React frontend app
app.use(express.static(path.join(__dirname, 'client/build')))
// This solves the "Not found" issue when loading an URL other than index.html.
app.get('/*', (req, res) => { //n3
res.sendFile(path.join(__dirname + '/client/build/index.html'), err => {
if (err) { res.status(500).send(err) }
})
})
Make sure to always point the path to build which is where all the files are loaded in the production environment.
After that, all urls are working well finally!

Webpack css-loader: "Module not found: Error: Can't resolve 'main.css' in ..."

I am trying to include my css in the server hosted by webpack-dev-server. For that to happen, I apparently have to use style-loader and css-loader together, in order to bundle the css into the JavaScript.
I can't get it to work.
I follow the instructions here, yet I get the following error:
ERROR in ./src/index2.js
Module not found: Error: Can't resolve 'main.css' in C:\Users\magnusga\Downloads\Programming\TestPrograms\test\src'
# ./src/index2.js 1:0-27
# multi (webpack)-dev-server/client?http://localhost:8080 ./src/index2.js
I know for certain that main.css is in the same folder as index2.js
My Settings
index2.js
import css from 'main.css';
// ...much more code
webpack.config.js
module.exports = {
entry: {
app: './src/index2.js'
},
devtool: 'inline-source-map',
devServer: {
contentBase: './dist'
},
module: {
rules: [
{
test: /\.css$/,
exclude: /node_modules/,
use: ['style-loader', 'css-loader']
}
],
},
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: 'Development',
template: 'src/index.html',
inject: 'head'
})
],
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index2.js",
"dependencies": {
"rxjs": "^5.5.6"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"clean-webpack-plugin": "^0.1.17",
"css-loader": "^0.28.11",
"extract-text-webpack-plugin": "^3.0.2",
"html-webpack-plugin": "^2.30.1",
"postcss-loader": "^2.1.3",
"sass-loader": "^6.0.7",
"style-loader": "^0.20.3",
"webpack": "^3.10.0",
"webpack-dev-server": "^2.11.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"buildb": "babel src --watch --out-dir built --copy-files",
"watch": "webpack --watch",
"start": "webpack-dev-server",
"build": "webpack"
},
"author": "",
"license": "ISC"
}
One Fix
One fix is to use import css from './main.css'; instead of import css from 'main.css'; (note the ./ infront of the file name).
That does not feel right though, because the css-loader site shows that it should be the latter, not the former.
Is it a typo in the docs?
Thank you.
It is not really a typo. If you import it like this:
import css from 'main.css';
Webpack thinks, that you want to import a module, and searches for this file under node_modules. This is necessary, when you for example installed the bootstrap package and want to import its css. So when your css file comes from a dependency, you import that dependency like this. But when you want to import a lokal file, always use relative paths.
So it must be: import css from './main.css';
Further Reading:
https://webpack.js.org/concepts/module-resolution/#module-paths
https://webpack.js.org/configuration/resolve/#resolve-modules

use LESS in webpack and es6

I'm following a lecture on Angular With Webpack.
I am trying to add the less loader and keep getting an error.
ERROR in ./src/app.js
Module not found: Error: Cannot resolve 'file' or 'directory' ../style.less in D:\projects\dev\webpack-angular-demo/src
# ./src/app.js 3:0-24
My webpack.config.js is :
module.exports = {
context: __dirname + '/src',
entry:'./app.js',
module:{
loaders:[
{
//create working environment for es6 need to npm i babel-loader babel-core babel-preset-es2015 -D
//https://github.com/babel/babel-loader
test:/\.js$/,
exclude:'/node_modules',
loader:'babel',
query: {
presets: ['es2015']
}
},
{
//take less convert to css and inject to style tag need to: npm i css-loader less-loader less style-loader -D
//https://github.com/webpack/less-loader
test:/\.less$/,
exclude:'/node_modules',
loader:"style!css!less"
}
]
}
};
My app.js is:
import '../style.less';
class Log{
constructor(){
console.log("sdfsdf");
}
}
new Log();
Inside the src directory i have the app.js , index.html and style.less files.
finally , this is my package.json file:
{
"name": "webpack-angular-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.10.4",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"css-loader": "^0.23.1",
"less": "^2.7.1",
"less-loader": "^2.2.3",
"style-loader": "^0.13.1",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"
}
}
any idea why i'm getting this error ?
Thanks
i had the exact same problem like you. i managed to solve that with tiny change.
path.join(__dirname, 'src')
instead of using:
__dirname + '/src/
now, make sure that your webpack.config.js look like:
var path = require('path');
module.exports = {
context: path.join(__dirname, 'src'),
...
and when I import the style.less i used like above
import './style.less';
If all files are in the same directory (src), the first line in app.js should be:
import './style.less';

Categories

Resources