node.js reading ts file as js in the npm root directory - node.js

I've got a Nodejs CLI written with typescript. It is dynamically reading an external file init.ts.
const {init} = require(initFunctionFile)
The goal is that other packages can run the CLI using their own init.ts file. If init.ts is in a plain directory, it works fine. But if it's in the global npm root directory, or if a package containing init.ts is not stored locally and is executed using npx, I'm getting the error:
~/.nvm/versions/node/v14.9.0/lib/node_modules/package-name/.../init.ts:5
init: async function(command: string, codeDir: string){
^
SyntaxError: Unexpected token ':'
It seems that node thinks the file is js. What is happening here, and what's a better way to do this?

Related

SvelteKit and Postgres implementation solution

Has anyone successfully deployed a SvelteKit app using npm pg to Netlify/Vercel/Cloudflare? My local dev implementation works just fine with how I have it set up ( db.ts file with a query function in lib/server, and then using endpoint actions and the load function in +page.server.ts files)
My build errors are as follows:
node_modules/pg-connection-string/index.js:3:18: ERROR: Could not resolve "url" node_modules/pg-connection-string/index.js:4:17: ERROR: Could not resolve "fs" node_modules/pg-pool/index.js:2:29: ERROR: Could not resolve "events" node_modules/pg-protocol/dist/parser.js:9:41: ERROR: Could not resolve "assert" node_modules/pg/lib/client.js:3:27: ERROR: Could not resolve "events"
And many of the above display a previous log message with something along the lines of:
✘ [ERROR] Could not resolve "buffer"
node_modules/safe-buffer/index.js:3:21:
3 │ var buffer = require('buffer')
╵ ~~~~~~~~
The package "buffer" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
I've been trying to get it to just build and it seems like it isn't able to use the pg package because it isn't a true node server environment. For each adapter it attempts to build with (except the node adapter) it refuses to build anything to do with the pg npm package. I could be wrong about the why, but my question about the how remains.
My hope is to avoid something like Prisma (which hasn't been working for me either) and I am trying to do this as "intended" meaning that I want to use SvelteKit as both the front end and the true backend. So an additional express server or the like is not the solution I'm looking for.
EDIT: I have also successfully deployed the app to Azure using the node adapter, but pg AND Postgres.js both do not work.

Using require within an NPM package

I've written a custom NPM package that will spin up a mocked Apollo GraphQL server for me with some custom settings.
In my /bin folder I have a file, server.js which is responsible for spinning up the server.
In package.json I've set up my command like this:
...
"bin": {
"mock-server": "./src/server.js"
},
...
So when I run the command mock-server from the parent project it will execute the server.js file.
All good so far, but the problem is that once I start trying to require dependencies I run into this error:
$ use-env mock-server
/Users/dev/projects/share-vde-frontend/node_modules/.bin/mock-server: line 1: syntax error near unexpected token `('
/Users/dev/projects/share-vde-frontend/node_modules/.bin/mock-server: line 1: `const { ApolloServer } = require("apollo-server");'
error Command failed with exit code 2.
My knowledge of writing npm modules is just based on what I've seen in packages I've been using in the past, so maybe I'm missing something key. Do I need some special measures when requiring imports? Or do I need to build and transpile the code? I'm using ES6 syntax, but I feel confident that anyone using this package will be on modern Node.js versions (the package is private and only to be used within our organisation).
Based on the comment from #jonrsharpe above, I solved it by added shabang to the start of the server.js file:
#!/usr/bin/env node
const { ApolloServer } = require("apollo-server");
...

no such file or directory, scandir '/sql/' issue var sql = new SQLBuilder('SQLServer'); on creating the object of json-sql-builder2 module

I'm getting this below error on deploying my aws lambda package on node 8.10.0 but it works fine in my local windows.
let modules = tools.walk(path.join(__dirname, `../sql/`));
on logging the _dirname it was showing '/' even though its is 'var/task'
lambda uploaded package has src
src/index.js
sql
sql/somemodules
this folders and files inside the uploaded package.
FYI: lambda package has bundled using webpack.
2019-05-09T04:23:22.996Z 6010d98c-2788-4c96-b972-49361876948c Error: ENOENT: no such file or directory, scandir '/sql/'
at Object.fs.readdirSync (fs.js:904:18)
at Object.walk (/var/task/src/index.js:144899:16)
at SQLBuilder._loadModules (/var/task/src/index.js:144709:23)
at new SQLBuilder (/var/task/src/index.js:144298:8)
at handler (/var/task/src/index.js:76189:15)

My cli engine(npm package) cant find path to read file when install on another folder

I am building an app that auto completes some type of file. When i run node index.js in the program folder i get the correct i results.
Although i want to make it an npm package that can work as a cli engine. For example i want to write the command generate and the code to produce the results.
In order to auto complete the file i have to read some data that i have stored in a .csv file that comes along with my program.
When i try to run generate command under another folder it can read that file.
I am very new to cli and i don't understand yet quite well how thing work.
Thanks in advance for the help.
Here is the code of my cli conversion.
#!/usr/bin/env node
const program = require("commander");
const {
predict
} = require("./classifier");
program.version("1.0.0").description("ESLint Rules Generator");
program
.command("generate")
.description("Generate ESLint Rules")
.action(() => {
predict();
});
program.parse(process.argv);
Here is the problematic line:
let str = fs.readFileSync("data_files/rules.csv", "utf-8");
This is the error i get: ENOENT: no such file or directory, open 'data_files/rules.csv'

Electron file path in package issue

I wonder how to get the correct path to file what should be loaded by fs.readFile:
File to load is in the app's root directory.
Starting the app in cli with npm start
var data = fs.readFileSync('settings.json');
works
After packing and starting electron.exe it will lead to
'Error: ENOENT: no such file or directory'
I can fix this for the packaged app with loading by
var data = fs.readFileSync(path.join(process.resourcesPath, 'app', 'settings.json'));
but then the file isn't found runing the app by npm start.
What is the correct way to determine the path to a file in any environement?

Resources