I have a problem like this. I have created a compile.js file to compile my solidity Contract. In there to handle file I am using the fs-extra module.
This is my compile.js file
const path = require('path');
const solc = require('solc');
const fs = require('fs-extra');
const buildPath = path.resolve(__dirname,'build');
fs.removeSync(buildPath);
const campaignPath= path.resolve(__dirname,'contracts','Campaign.sol');
const source = fs.readFileSync(campaignPath,'utf8');
const output= solc.compile(source,1).contracts;
fs.ensureDirSync(buildPath);
for(let contract in output){
fs.outputJsonSync(
path.resolve(buildPath,contract),
output[contract]
);
}
It works everything finely, without for each loop. When I compile this file with for loop it gives me an error saying that
C:\Users\tharindusa\WebstormProjects\Campaign\node_modules\solc\soljson.js:1
(function (exports, require, module, __filename, __dirname) { var Module;if(!Modu
le)Module=(typeof Module!=="undefined"?Module:null)||{};var moduleOverrides={};fo
r(var key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[k
ey]}}var ENVIRONMENT_IS_WEB=typeof window==="object";var ENVIRONMENT_IS_WORKER=ty
peof importScripts==="function";var ENVIRONMENT_IS_NODE=typeof process==="object"
&&typeof require==="function"&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;var EN
VIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WOR
KER;if(ENVIRONMENT_IS_NODE){if(!Module["print"])Module["print"]=function print(x)
{process["stdout"].write(x+"\n")};if(!Module["printErr"])Module["printErr"]=funct
ion printErr(x){process["stderr"].write(x+"\n")};var nodeFS=require("fs");var nod
ePath=require("path");Module["read"]=function read(filename,binary){filename=node
Path["normalize"](filename);var ret=nodeFS["readFileSy
Error: ENOENT: no such file or directory, open 'C:\Users\tharindusa\WebstormProje
cts\Campaign\ethereum\build\:Campaign.json'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.writeFileSync (fs.js:1299:33)
at Object.writeFileSync (C:\Users\tharindusa\WebstormProjects\Campaign\node_m
odules\fs-extra\node_modules\jsonfile\index.js:117:13)
at Object.outputJsonSync (C:\Users\tharindusa\WebstormProjects\Campaign\node_
modules\fs-extra\lib\json\output-json-sync.js:15:12)
at Object.<anonymous> (C:\Users\tharindusa\WebstormProjects\Campaign\ethereum\compile.js:15:8)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
Can someone help me to solve this problem? .Thank you
Your filename (:Campaign.json) contains a colon, which is invalid in Windows, where colons are only used with drive letters.
I had run into the same problem while compiling my contracts.The issue with your code is that it tries to create a file named :Campaign which is not allowed in windows.
I used the following code to get around it
for(let contract in output){
fs.outputJSONSync(
path.resolve(buildPath,contract.replace(/:/g,'')+'.json'),
output[contract]
);
}
Related
i am having a hell of time trying to set up a custom repl app. I did a bunch of googling and managed to wrange the code below and got a working repl shell.
script.js
const repl = require("repl")
function evaluate(command, context, filename, callback) {
callback(null, command)
}
repl.start({
prompt: ": ",
eval: evaluate
});
so after getting this prototype working i decided to add a module to split the routing of the command processing. I made a tracker.js module and required it in my script.js
tracker.js
export default class Tracker {
static Process(command) {
console.log(command)
}
}
script.js added the require statement.
const repl = require("repl")
const Tracker = require("./tracker")
function evaluate(command, context, filename, callback) {
callback(null, command)
}
repl.start({
prompt: ": ",
eval: evaluate
});
after i reran this i got an error saying,
SyntaxError: Unexpected token 'export'
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (C:\Users\e212034\repository\repl\script.js:2:17)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
PS C:\Users\e212034\repository\repl>
so i went back to google and found some tips saying to add type:module to the package.json. so i did this and reran and now i get this error,
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'C:\Users\e212034\repository\repl\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at file:///C:/Users/e212034/repository/repl/script.js:1:14
at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:65:12)
PS C:\Users\e212034\repository\repl>
i tried changing the module to .mjs no luck. i tried changing the type to commonjs and the module to .cjs no luck on either.
any ideas on how to resolve this issue?
Good day everyone,
First off, thanks for always being such an amazing community. You all really are helping me a ton with learning and bettering my programming and development!
I have a small question related to the module.exports within Node.js. The function below runs with no issues when called on directly:
const fs = require('fs')
const {nanoid} = require('nanoid')
const createStormDB = () => {
return new Promise((resolve, reject) => {
try{
const id = nanoid(4)
const date = new Date() // Create date string for file naming
let dateString = `${date.toISOString().split('T')[0]}` // Create date string for file naming
let fileName = `${dateString}_deals_${id}.stormdb` // Create date string for file naming
fs.openSync(`../StormDB/${fileName}`, 'w')
resolve(fileName)
}catch(err){
reject(err)
}
})
}
module.exports = createStormDB
It creates a file with a specific name within in specific folder. But when I use module.exports = createStormDB I am greeted with the following error:
(node:12516) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '../StormDB/2021-07-19_deals_gYmJ.stormdb'
at Object.openSync (fs.js:476:3)
at C:\Node\Pipedrive-Connector\PipeDriveRequest\scripts\createStormDBFile.js:11:16
at new Promise (<anonymous>)
at createStormDB (C:\Node\Pipedrive-Connector\PipeDriveRequest\scripts\createStormDBFile.js:5:12)
at Object.<anonymous> (C:\Node\Pipedrive-Connector\PipeDriveRequest\play.js:7:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Is there something I am misunderstanding when it comes to exporting modules? I'm importing this module using the require option! Thanks so much for the help!
The .. relative path in the function is relative to the calling scripts current working directory, not the directory the file is in.
Assuming from your path setup and description the database is in: C:\Node\Pipedrive-Connector\PipeDriveRequest\StormDB
If you want the database path to remain relative to the javascript file containing the function, use __dirname
const path = require('path')
const db_path = path.join(__dirname, '..', 'StormDB', filename)
fs.openSync(db_path, 'w')
I'm building an Alexa skill (deploying via CLI), and all has been going well up until needing to make a few Http calls.
I decided to use first axios, then require in order to do so.
When I installed axios using npm, all seemed well. Adding just the "const axios= require('axios');" line caused my lambda function to start complaining about "Unable to import module 'index'" in the Cloudwatch logs, however, and specifically calling out the line in index.js where I make that require statement.
Removed axios, tried require...same deal.
Any thoughts?
Not actually using the packages yet even, it complains on the require line if I uncomment it.
/* eslint-disable func-names */
/* eslint-disable no-console */
/* eslint-disable no-restricted-syntax */
const error_handler = require('./error_handler');
const globals = require('./globals');
const helper_functions = require('./helper_functions');
const intents_aquarium = require('./intents_aquarium');
const intents_built_in = require('./intents_built_in');
const intents_conversion = require('./intents_conversion');
const intents_helper = require('./intents_helper');
const intents_tank = require('./intents_tank');
const intents_unhandled = require('./intents_unhandled');
const skillBuilder = globals.Alexa.SkillBuilders.standard();
//const request = require('request');
exports.handler = skillBuilder
.addRequestHandlers(
intents_built_in.launchRequest,
intents_built_in.exitHandler,
intents_built_in.sessionEndedRequest,
intents_built_in.helpIntent,
intents_built_in.yesIntent,
intents_built_in.noIntent,
intents_aquarium.aquariumCreateIntentHandler,
intents_aquarium.aquariumCreateSimpleImperial,
intents_conversion.aquariumVolumeIntentGallonsToLitres,
intents_conversion.aquariumVolumeIntentLitresToGallons,
intents_helper.thankYou,
intents_tank.tankObservation,
intents_built_in.fallbackHandler,
intents_unhandled.unhandledIntent,
)
.addErrorHandlers(error_handler.errorHandler)
.withTableName('Tank-Mate')
.withAutoCreateTable(true)
.lambda();
Error looks like this:
Unable to import module 'index': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/index.js:16:17)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
...which to me looks like it's complaining about the index.js require line.
For instance, in my project directory, I have:
|--bar.js
|--dir
|--foo.txt
|--readfile.js
readfile.js:
const fs = require('fs');
var foo = fs.readFileSync('foo.txt', 'utf8');
console.log(foo);
module.exports = {foo};
Running node readfile.js, everything works perfectly.
bar.js:
const readfile = require('./dir/readfile');
console.log(read.foo);
Running node bar.js, I get:
fs.js:663
return binding.open(pathModule.toNamespacedPath(path),
^
Error: ENOENT: no such file or directory, open 'foo.txt'
at Object.fs.openSync (fs.js:663:18)
at Object.fs.readFileSync (fs.js:568:33)
at Object.<anonymous> (/Users/fterh/Documents/Projects/playground/dir/readfile.js:3:14)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
Fabians-MacBook-Pro:playground fterh$
I know it has to do with require('./dir/readfile') in bar.js, because Node then tries to search for "foo.txt" in the same directory as "bar.js". Currently, my fix is to use path.dirname(__filename) to get absolute paths, which would work regardless of whether I'm running the module directory or requiring it. I'm wondering if there is a more elegant way of doing things.
Use of require.resolve within readfile.js as follows:
const fs = require('fs');
let foo = fs.readFileSync(require.resolve('./foo.txt'), 'utf8');
console.log(foo);
module.exports = {foo};
Note: in the original question for bar.js it may have been intended to write: console.log(readfile.foo);.
require.resolve:
... return the resolved filename
Use __dirname to construct your path as that will always point to the directory where your module was loaded from, regardless of the current directory. This is one of the variables that is passed into a module so it has a unique value in the scope of each module and it's purpose is for exactly what you want (to do file operations relative to your module's directory).
const fs = require('fs');
const path = require('path');
var foo = fs.readFileSync(path.join(__dirname, 'foo.txt'), 'utf8');
console.log(foo);
module.exports = {foo};
Reference info for __dirname here.
I want to write a log module for my app, which will create a non-exist file according to the time the module is initialized (with the init function in the module is called).
However, when I try to create the new log file with fs.openSync(log_file_name,'a') It always gets an error.
I appreciate anyone that tell me why it fails to create the new file .
const fs=require('fs');
const path=require('path');
const moment=require('moment');
var time=function()
{
return moment().format('YYYY-MM-DD[_]hh:mm:ss:SSS');
};
//init
var fd; // file descriptor
function init(log_dir)
{
var log_file_name=path.join(log_dir,time()+'.log');
this.fd=fs.openSync(log_file_name,'a');
}
init(__dirname);
The error is like below:
"C:\Program Files (x86)\JetBrains\PhpStorm 2016.1\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" index.js
fs.js:634
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open 'I:\twesix\lib\twesix_nodejs\log\2016-05-19_01:34:52:621.log'
at Error (native)
at Object.fs.openSync (fs.js:634:18)
at init (I:\twesix\lib\twesix_nodejs\log\index.js:15:16)
at Object.<anonymous> (I:\twesix\lib\twesix_nodejs\log\index.js:24:1)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Function.Module.runMain (module.js:575:10)
Process finished with exit code 1
I'm using windows 10 and my node.js version is 6.1.0
The problem is that the string returned by moment().format('YYYY-MM-DD[_]hh:mm:ss:SSS'); contains colons (:), which are not allowed in the name of the files on windows.
Change the format() to something that doesn't contain invalid characters for windows file names, like:
return moment().format('YYYY-MM-DD[_]hh-mm-ss-SSS');