Canjot find csv-parser installed globally - javascript

I have a standalone file called run.js in a directory xyz:
const fs = require('fs');
const csv = require('csv-parser');
const inputFile = 'input.csv';
fs.createReadStream(inputFile)
.pipe(csv())
.on('data', (row) => {
const json = JSON.stringify(row);
const id = row['_id'];
const filename = `${id}.doc.json`;
fs.writeFileSync(filename, json);
})
.on('end', () => {
console.log('Conversion complete!');
});
I am trying to run this using node run and I am getting the following:
node:internal/modules/cjs/loader:959
throw err;
^
Error: Cannot find module 'csv-parser'
I have installed csv-parser using npm install -g csv-parser
csv-parser --version gives me the version number confirming that it's installed.
What could be the issue here ?

Related

Cannot read properties of undefined (reading 'match') at getLocalIdent

i'm trying to make build script. so i can build with webpack it kinda like this
build.server.js
process.env.BABEL_ENV = 'production';
process.env.NODE_ENV = 'production';
process.on('unhandledRejection', err => {
throw err;
});
require('../config/env');
const fs = require('fs-extra');
const webpack = require('webpack');
const config = require('../config/webpack.config.server');
const paths = require('../config/paths');
function build() {
console.log('Creating server build');
fs.emptyDirSync(paths.ssrBuild);
let compiler = webpack(config);
return new Promise((resolve, reject) => {
compiler.run((err, status) => {
if (err) {
console.log(err);
return;
}
console.log(status.toString());
});
});
}
build();
after i code this and type node scripts/build.server.js , this error comes out.
Cannot read properties of undefined (reading 'match')
at getLocalIdent (C:\Users\jhs00\OneDrive\바탕 화면\공부자료\ssrProject\ssrproject\node_modules\react-dev-utils\getCSSModuleLocalIdent.js:20:49)
getLoslIdent is in here (webpack.config.server.js)
const nodeExternals = require('webpack-node-externals')
const webpack = require('webpack');
const paths = require('./paths');
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
...
i searched similar question and tried delete package-lock.json, node_modules and reinstall it. but didn't work. what should i do?

How can i make an application using Node.js, which can separate all the files with same extension?

How to write code in node.JS in which we select a directory and the code automatically separates all the files of that selected directory with the same extension and put then in a separate folder.
Something like this will work. Using fs module and path module
Which will first check the extension and rename file (move) to new folder.
You can make changes accordingly. Use if
const testFolder = './';
const fs = require('fs');
var path = require('path')
var oldPath = 'old/path/file.txt'
var newPath = 'new/path/file.txt'
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
const ext = path.extname(file);
fs.rename(oldPath, newPath, function (err) {
if (err) throw err
console.log('Successfully renamed - AKA moved!')
})
});
});

When I try to run npm test with mocha, in my cmd it says "Error: Cannot find module 'path' " does anyone know how to solve this?

When I try to run npm test with mocha, in my cmd it says Error: Cannot find module 'path'
I am a total beginner in this area and just trying to write some really simple code. I am using Mocha as my test frame, it worked totally fine yesterday and today it's down.
I tried npm install -D #types/path, it did not work out.
Here is my code in Inbox.test.js file:
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');//capitalize when working with a constructor function
const web3 = new Web3(ganache.provider());//local instance
const {interface, bytecode} = require('../compile');
let accounts;
let inbox;
beforeEach(async ()=>{
//Get a list of all accounts
web3.eth.getAccounts()
accounts = await web3.eth.getAccounts();
//Use one of the accounts to deploy
//the contrct
inbox = await new web3.eth.Contract(JSON.parse(interface))
.deploy({data: bytecode, arguments:['Hi there!']})
.send({from: accounts[0], gas:'1000000'});//account[0] is the one
//who is really deploying the contract
});
describe('Inbox', () => {
it('deploys a contract', ()=>{
console.log(inbox);
});
});
Here is code in my compile.js file:
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const inboxPath = path.resolve(__dirname, 'contract', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');
module.exports = solc.compile(source,1).contracts[':Inbox'];
The test frame Mocha should run through my code and print out the smart contract I created, instead it shows a error that says I am missing a module 'path'.
Here is the error I am getting:
Error: Cannot find module 'path '
Require stack:
- C:\Users\Alex Zhu\Desktop\Inbox\compile.js
- C:\Users\Alex Zhu\Desktop\Inbox\test\Inbox.test.js
- C:\Users\Alex Zhu\Desktop\Inbox\node_modules\mocha\lib\mocha.js
- C:\Users\Alex Zhu\Desktop\Inbox\node_modules\mocha\lib\cli\one-and-dones.js
- C:\Users\Alex Zhu\Desktop\Inbox\node_modules\mocha\lib\cli\options.js
- C:\Users\Alex Zhu\Desktop\Inbox\node_modules\mocha\lib\cli\cli.js
- C:\Users\Alex Zhu\Desktop\Inbox\node_modules\mocha\lib\cli\index.js
- C:\Users\Alex Zhu\Desktop\Inbox\node_modules\mocha\bin\_mocha

Workbox in Create React App without eject

I'm trying to configure Workbox in CRA without eject. Anyone succeeded?
After hours trialing and error I succeeded to have workbox in CRA. Here's how I did:
First,yarn add -D workbox-build
Next, create a file called build-sw.js in the root folder with:
const fs = require('fs-extra');
const pathmodule = require('path');
const workbox = require('workbox-build');
function build() {
const cwd = process.cwd();
const pkgPath = `${cwd}/node_modules/workbox-sw/package.json`;
const pkg = require(pkgPath);
const readPath = `${cwd}/node_modules/workbox-sw/${pkg.main}`;
let data = fs.readFileSync(readPath, 'utf8');
let path = `${cwd}/build/workbox-sw.js`;
console.log(`Writing ${path}.`);
fs.writeFileSync(path, data, 'utf8');
data = fs.readFileSync(`${readPath}.map`, 'utf8');
path = `${cwd}/build/${pathmodule.basename(pkg.main)}.map`;
console.log(`Writing ${path}.`);
fs.writeFileSync(path, data, 'utf8');
workbox
.injectManifest({
globDirectory: 'build',
globPatterns: ['**/*.{html,js,css,png,jpg,json}'],
globIgnores: ['sw-default.js', 'service-worker.js', 'workbox-sw.js'],
swSrc: './src/sw-template.js',
swDest: 'build/sw-default.js'
})
.then(_ => {
console.log('Service worker generated.');
});
}
try {
build();
} catch (e) {
console.log(e);
}
After that, create a file in src/sw-template.jswith:
(Have in mind that in this file is where you have to put your own cache strategy. See Docs for more info)
workbox.setConfig({
debug: true
});
workbox.core.setLogLevel(workbox.core.LOG_LEVELS.debug);
workbox.precaching.precacheAndRoute([]);
workbox.skipWaiting();
workbox.clientsClaim();
workbox.routing.registerRoute('/', workbox.strategies.networkFirst());
Finally, in src/registerServiceWorker.js change:
- const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
+ const swUrl = `${process.env.PUBLIC_URL}/sw-default.js`;
And in package.json change:
- "build": "react-scripts build && sw-precache --config=sw-precache-config.js'",
+ "build": "react-scripts build && yarn sw",
+ "sw": "node build-sw.js"
Hope it helps!
With the merge of this PR, Create React App 2 now support supports Workbox out of the box, since it now uses workbox-webpack-plugins internally.
Take a look at the official docs to learn more: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

/bin/sh: 1: node: not found with child_process.exec

I tried to run a nodejs script with the built in child_process module and it works fine until i give it options. Specially when i add the env property to the options object.
let exec = require('child_process').exec;
exec('node random.js', { env: {} }, (err) => {
console.log(err);
})
Then i get this error: /bin/sh: 1: node: not found.
I have node installed with nvm, maybe that is the cause, but don't know why.
If you exec a new shell from your script this don't have the same environment of the parent shell (your script).
So you have to provide all the needed environment.
In your case I see 2 way you could do.
First: you create a node command with the full path:
let exec = require('child_process').exec;
let node_cmd = '/path/to/my/node/node';
exec(node_cmd + ' random.js', { env: {} }, (err) => {
console.log(err);
});
So you could use env variables to handle the path, or just change it when you need.
Second, pass the path variable to the command:
let exec = require('child_process').exec;
let env_variables = 'PATH='+process.env.PATH;
let cmd = env_variables + ' node random.js';
exec(cmd, { env: {} }, (err) => {
console.log(err);
});
Another way is using the dotenv package.

Categories

Resources