node.js local modules :cannot find module error - javascript

I am trying to implement local modules in my application
1.Project root folder i have created folder named test with a file named index.js
module.exports = {
myFunction:function(){
console.log('ok');
}
}
2.Added the following in package.json in the root folder
"dependencies": {
"test-module": "file:test"
}
3.When i try to import var module = require('test-module'); in app.js i got this error
Cannot find module 'test-module'

you can provide a path to a local directory that contains a package
{
"name": "baz",
"dependencies": {
"bar": "file:../foo/bar"
}
}
and perform npm install -s or npm install --save reference

To add on #Blaze answer, if you follow the steps (Local Paths) to install a local module, it will sort out for you the local dependency in your package.json:
npm i ./test --save
That will produce the correct local dependency entry in your dependencies in the root package.json:
"test-module": "file:test"
assuming test-module is the name in the local dep package.json.
This is how it should look like:

Make sure your test folder has a package.json.
test/package.json should have a "name" field with the value "test-module" (ie, same name as the dependency in your root package.json.
My files:
test/package.json
{
"name": "test-module",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
test/index.js
module.exports = {
t:() => console.log('t')
};
package.json
{
"name": "t",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"test-module": "file:test"
}
}
app.js
t = require('test-module');
t.t();
This is working for me.

Related

Failed launching test session: Error: Couldn't initialise "#wdio/cucumber-framework"

I am setting up the wdio using command "npm init wdio" and try to run the test files. it is giving below error
Failed launching test session: Error: Couldn't initialise "#wdio/cucumber-framework". [0-0] Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/formatter/helpers/event_data_collector' is not defined by
Below is my package.json file
"name": "wdio-new",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"wdio": "wdio run wdio.conf.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#wdio/cli": "^7.16.12",
"#wdio/cucumber-framework": "^7.16.12",
"#wdio/local-runner": "^7.16.12",
"#wdio/spec-reporter": "^7.16.11",
"chromedriver": "^96.0.0",
"wdio-chromedriver-service": "^7.2.2"
}
}
I managed to fix this issue by reinstalling node and checking the option to "Automatically install the necessary tools" during setup

Cannot import Axios to use with Parcel-Bundler

I am trying to use axios for a http request in my app.js file, but I always get the error message:
Uncaught SyntaxError: Cannot use import statement outside a module.
I created the js folder, files and other js packages using parcel-bundler and run browser-sync to start the server.
What am I missing? Please help I am new to JavaScript. I am on macOS Big Sur 11.5.1, node v14.17.4
import axios from 'axios'
document.addEventListener('DOMContentLoaded', () => {
const form = document.querySelector('form')
form.addEventListener('submit', async (event) => {
event.preventDefault()
const username = document.querySelector('input').value
const response =
await axios.get(`https://api.github.com/users${username}`)
console.log(response.data)
})
})
In the closest (from the root of project directory) package.json file, add or update "type" field with a value of "module". It will ensure that all your .js, .ts and .mjs files are interpreted as ES modules.
...
{
...
"type": "module"
...
}
...
Where can I add it in this file?
{
"name": "githubUser",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1"
}
}
Add "type": "module" to your package.json
{
// ...
"type": "module",
// ...
}
Update:
It should looks like this:
{
"name": "githubUser",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1"
}
}

rollup.js_Bundle automatic update

I'm trying to biuld bundling through rollup.js in VSCode.
My directory:
----MyProject
--------\node_modules
-----------\.bin
-----------\rollup
--------index.js
--------index.html
--------bundle.js
--------package-lock.json
--------package.json
In my .html file I have connection with bundle.js, all changes which I'm doing in index.js must automatically be updated in bundle.js. But it's only working when I run in terminal this command: rollup index.js --file bundle.js
My package.json:
{
"name": "npm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"rollup": "^2.34.2"
}
}
What do I need to do to make this system works automatically?
Firstly, I didn't have config file, so I've created rollup.config.js:
import serve from 'rollup-plugin-serve'
import livereload from 'rollup-plugin-livereload'
const watch = process.env.ROLLUP_WATCH
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'iife'
},
plugins: [
watch && serve('dist'),
watch && livereload()
]
}
Then I added these 2 scripts into package.json:
"build": "rollup -c",
"dev": "rollup -c -w"
And run in terminal: npm run dev
My credits to vladshcherbin for help!

Run a npm bin script from a dependencies package.json

I want to create 2 seperate packages - the first should only have package-2 as a dependency and package-2 can have anything required for that package.
My problem is that one of the dependencies in package-2 (let's call it package-3) has a bin script. I'd like to expose that for package-1 to use but it's not work.
When running npm run start in package 1 I get the following error:
./bin/sh package-3 is not a command
{
"name": "package-1",
"scripts": {
"start": "package-2 start",
},
"dependencies": {
"package-2": "1.0.0",
}
}
{
"name": "package-2",
"scripts": {
"start": "package-3 start",
},
"dependencies": {
"package-3": "1.0.0",
}
}
{
"name": "package-3",
"bin" : {
"package-3": "cli-with-start-command",
},
}

npx webpack command cannot find module webpack.config.js

I'm following the official Webpack getting started guide and I get an error on the Using a Configuration section. It says to create a webpack.config.js file with:
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist')
}
};
I then run the following command:
npx webpack --config webpack.config.js
The error I get is:
Cannot find module '/Users/Documents/Web_Development/tone/webpack.config.js'
The guide does not seem to give any ideas of what could be wrong here. Also my code editor is telling me there is an error with const path = require('path'); saying "Expected a JSON Object, array or literal;
My Directory structure:
webpack.config.json
package.json.lock
package.json
node_modules/
dist/
index.html
main.js
src/
index.js
package.json:
{
"name": "tone",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.3",
"webpack": "^4.29.3",
"webpack-cli": "^3.2.3"
},
"dependencies": {
"lodash": "^4.17.11"
}
}
The solution was to change the webpack.config.json file to webpack.config.js.
You have to make sure that the webpack.config.js file is in the root of your project.

Categories