I've tried looking at some of the answers to this similar question but none of the solutions I have attempted from there have worked.
I'm trying to create a function in an external javascript file and I would like to use that function in my main javascript file.
webpack.config.js
app
-- index.js
-- test.js
module.exports = {
devtool: "cheap-module-source-map",
entry: "./app/index.js",
output: {
filename: "public/bundle.js"
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query:
{
presets: ['es2015']
}
}
]
}
};
export default function test(message) {
console.log(message);
}
"use strict"
import test from './app/test.js';
When attempting to run the webpack command I get the error:
ERROR in ./app/index.js
Module not found: Error: Can't resolve './app/test.js'
I've tried changing the file path to a relative one with no luck and I've also tried adding multiple entry points in the webpack config file but still no luck.
A module not found error can occur for many different reasons: The module you're trying to import is not installed in your dependencies. The module you're trying to import is in a different directory. The module you're trying to import has a different casing.
import React from 'react'; import ReactDOM from 'react-dom'; import { createRoot } from 'react-dom/client'; import Switch from './components/Switch'; const root = ReactDOM. createRoot(document. getElementById('root')); root. render( <React.
Posted as answer so that it can be marked as answered, from comment:
Your import path is wrong, it looks like you should change import test from './app/test.js';
to import test from './test.js';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With