Svelte: Uncaught ReferenceError: exports is not defined - javascript

Background
I am trying to use dgraph-js-http in a Svelte application.
Error
The initial error I was facing was:
Uncaught ReferenceError: buffer is not defined
<anonymous> http://localhost:5000/build/bundle.js:6947
I'm including this as maybe my solution to this was not the best, and that hopefully there is a way to solve both this issue, and the current blocker issue.
However, after a trial-and-error approach (see below for the various things I've tried) I'm now stuck.
Current error
After solving the above error, I am now blocked on this error:
Uncaught ReferenceError: define is not defined
Attempts to resolve
None of the following have worked.
Add <script src="https://bundle.run/stream#0.0.2"></script> to index.html
Run npm install stream
Add import * as Stream from "stream" to the App.svelte file
Add import * as Stream from "stream" to the stores.js file
Add preferBuiltins:true to rollup.config.js
Add npm install --save-dev rollup-plugin-node-polyfills
Add preferBuiltins:false to rollup.config.js. This resolved prior errors and resulted in the new error of: Uncaught ReferenceError: define is not defined
Add npm install --save define. This, now results in the current blocker:
Uncaught ReferenceError: exports is not defined

I think you missed type: "module" attribute in package.json. Here, the type attribute represents whether the type of javascript interpreter is commonjs or babel. type: "module" indicates that you are going to use babel as the ECMA script interpreter.

Related

Cannot read properties of undefined (reading 'isAbsolute')

I was editing this program yesterday and it worked fine the whole time. The program is created using parcel, today when started up the script and the dev server was broken. This is the error i recived:
*Uncaught TypeError: Cannot read properties of undefined (reading 'isAbsolute')
at parcelRequire.../node_modules/tar/lib/strip-absolute-path.js.path (strip-absolute-path.js:2:9)
at newRequire (main.fb6bbcaf.js:47:24)
at localRequire (main.fb6bbcaf.js:53:14)
at parcelRequire.../node_modules/tar/lib/write-entry.js.minipass (write-entry.js:39:27)
at newRequire (main.fb6bbcaf.js:47:24)
at localRequire (main.fb6bbcaf.js:53:14)
at parcelRequire.../node_modules/tar/lib/pack.js.minipass (pack.js:28:20)
at newRequire (main.fb6bbcaf.js:47:24)
at localRequire (main.fb6bbcaf.js:53:14)
at parcelRequire.../node_modules/tar/lib/create.js../high-level-opt.js (create.js:6:14)*
I cant seem to find anyone else with the same error. This is strip-aboslute-path.js 2:9 where the error happens:
//unix absolute paths are also absolute on win32, so we use this for both
const { isAbsolute, parse } = require('path').win32
I’ve tried:
npm install, npm install tar, npm i -g npm and
changing the names of folders in the Path to the src file that included "å, ä, ö",
Would love get this running again, any suggestions on what to try next?
Solved: This probably happened due to me trying to access a variable named x and vscode auto adding "import {x} from tar" at the top of the js file. When removing the import the program was working again.

What is the correct way to use modules in Node.js? [duplicate]

I got an error when using this line of code
const sass = require('gulp-sass')(require('sass'));
The error output is :
Requiring external module babel-register
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:\xampp-clean\htdocs\myfirsttheme\package.json' contains "type": "module". T
o treat it as a CommonJS script, rename it to use the '.cjs' file extension.
I have changed
const sass = require('gulp-sass')(require('sass'));
the error disappeared but I am getting this error :
Requiring external module babel-register
TypeError: Cannot read property 'prod' of undefined
and this line is causing the error :
const PRODUCTION = yargs.argv.prod;
Do anyone has an idea?
I have tried a lot and googled this error but I did not find a solution , do anyone has an idea about this ?
Thanks in advance
Can you check the "type" part in the package.json file? If the type part is module, you will get this error. You have to make it Commonjs.
work for me , only change in package.json file, this line:
"type":"module"
to
"type":"commonjs"
First of all add "type":"commonjs" to package.json.
Downgrade your package version. If you are using gulp-imagemin version 8.0.0 then install older version like npm i gulp-imagemin#7.1.0.
That's all now enjoy coding...

Can get a prisma graphql to compile

When I run npm run dev I get this error and I have no idea what is causing it
I have installed all the packages and reinstalled them.
I have applied all of the migrations.
I have re-cloned the repo and done all the steps again and I cant seem to get past this error.
Error: Upload was already defined and imported as a type, check the docs for extending types
at extendError (C:\Users\coope\dev\paypixl\backend\node_modules\nexus\src\builder.ts:1743:10)
at SchemaBuilder.addType (C:\Users\coope\dev\paypixl\backend\node_modules\nexus\src\builder.ts:602:15)
at C:\Users\coope\dev\paypixl\backend\node_modules\nexus\src\builder.ts:1696:14
at C:\Users\coope\dev\paypixl\backend\node_modules\nexus\src\utils.ts:147:41
at Array.forEach (<anonymous>)
at eachObj (C:\Users\coope\dev\paypixl\backend\node_modules\nexus\src\utils.ts:147:20)
at SchemaBuilder.traverseArgs (C:\Users\coope\dev\paypixl\backend\node_modules\nexus\src\builder.ts:1693:12)
at SchemaBuilder.maybeTraverseOutputFieldType (C:\Users\coope\dev\paypixl\backend\node_modules\nexus\src\builder.ts:1688:12)
at Object.addField (C:\Users\coope\dev\paypixl\backend\node_modules\nexus\src\builder.ts:1640:29)
at ObjectDefinitionBlock.field (C:\Users\coope\dev\paypixl\backend\node_modules\nexus\src\definitions\definitionBlocks.ts:712:22)
[ERROR] 10:03:59 Error: Upload was already defined and imported as a type, check the docs for
extending types

ReferenceError: exports is not defined

I'm running tests using karma and one dependency of my project has a dependency on es-cookie and it seems to be giving me problems when running the tests.
I tried adding a commonjs preprocessor but it gives a syntax error probably because commonjs doesn't work with the other files. I tried chaining it with another preprocessor but it still doesn't work.
preprocessors: {
'packages/**/test/**/*.test.js': ['globals'],
'node_modules/**/*.js': ['commonjs'],
},
The tests should run smoothly but this error pops up when there is no commonjs preprocessor
Uncaught ReferenceError: exports is not defined
at node_modules/es-cookie/src/es-cookie.js:17:1
ReferenceError: exports is not defined
UPDATE:
This was the problematic line:
exports.__esModule = true;
I tried changing it to
export const __esModule = true;
And all my tests ran successfully. However, is it allowed to change a node module dependency's code, and is there a way to preprocess it in Karma?

Uncaught SyntaxError in jquery.d.ts file

I'm trying to transition my jQuery project into a Typescript project. However, when I include the jQuery typings from the DefinitelyTyped Github using this method:
///<reference path="../../typings/jquery.d.ts"/>
I get "Uncaught SyntaxError: Unexpected identifier" on line 27 of jquery.d.ts, shown below
declare module 'jquery' {
export = jQuery;
}
I've done a bunch of searching about this problem, but I can't seem to find any solutions to this problem. What am I doing wrong?
(I am using WebStorm as my IDE)
EDIT:
Ok, I followed some instructions below and installed the jQuery typings via the npm terminal. However, when I remove the ///reference import, I now get an error in Webstorm: TS2304 cannot find name '$'. However, the code compiles to js and can run in the browser fine. It's just annoying to have those errors in my console in Webstorm. Is there something I need to add to get Webstorm to recognize jQuery?
most recommended practice nowaday is instead of ///reference import, install separate type packages: https://www.npmjs.com/package/#types/jquery as devdependency. (and ensure you're using latest ts compiler supports this)

Categories

Resources