Parcel+Sass+npm: "Error: Failed to install #parcel/transformer-sass: Callback must be a function. Received undefined - node.js

I'm trying to use Parcel to build a library, and am trying to import an index.scss file into my index.js file, it either fails to 'install #parcel/transformer-sass' if I don't include it as a dependency, or 'cannot find module "#parcel/transformer-sass"' when I do install it. Notably, Parcel will build successfully if I import an index.css' file into index.js` instead.
Does anyone know what I'm doing wrong?
Installing npm i #parcel/transformer-scss as a dependency, for which I get the error:
× Build failed.
Error: Failed to install #parcel/transformer-sass: Callback must be a function. Received undefined
Error: Failed to install #parcel/transformer-sass: Callback must be a function. Received undefined
at install
(C:\...\nvm\v14.18.0\node_modules\parcel\node_modules\#parcel\package-manager\lib\installPackage.js:131:11)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async PromiseQueue._runFn
(C:\...\nvm\v14.18.0\node_modules\parcel\node_modules\#parcel\utils\lib\PromiseQueue.js:88:7)
at async PromiseQueue._next
(C:\...\nvm\v14.18.0\node_modules\parcel\node_modules\#parcel\utils\lib\PromiseQueue.js:75:5)
Installing #parcel/transformer-sass and #parcel/config-default, for which I receive the error:
× Build failed.
#parcel/core: Cannot find Parcel plugin "#parcel/transformer-sass"
C:\...\nvm\v14.18.0\node_modules\parcel\node_modules\#parcel\config-default\index.json:25:23
24 | "*.{styl,stylus}": ["#parcel/transformer-stylus"],
> 25 | "*.{sass,scss}": ["#parcel/transformer-sass"],
> | ^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot find module "#parcel/transformer-sass"
26 | "*.less": ["#parcel/transformer-less"],
27 | "*.{css,pcss}": ["#parcel/transformer-postcss", "#parcel/transformer-css"],
I also tried creating a .parcelrc of various configurations, including:
{
"transformers":{".scss":"#parcel/transformer-scss"}
}
and
{
"extends":"#parcel/config-default",
"transformers":{".scss":"#parcel/transformer-scss"}
}
which throw similar errors.
Here's my package.json:
"name": "frontend",
"version": "1.0.0",
"description": "",
"source": "src/index.js",
"targets": {
"default": {
"distDir": "../staticfiles"
}
},
"main": "index.js",
"module": "module.js",
"scripts": {
"watch": "parcel watch",
"build": "parcel build"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"parcel": "latest"
},
"dependencies": {
"#parcel/config-default": "^2.0.0",
"#parcel/transformer-sass": "^2.0.0",
"#popperjs/core": "^2.10.2",
"bootstrap": "^5.1.3",
"bootstrap-table": "^1.18.3",
"jquery": "^3.6.0"
}
}
here's my index.js:
import "./index.scss";
import 'bootstrap';
import 'bootstrap-table';
import $ from "jquery";
console.log($)
window.jQuery = $;
window.$ = $;
console.log("hello world");
$(function() {
$('#table').bootstrapTable()
})
Here's my index.scss:
#import "bootstrap/scss/bootstrap";
#import "bootstrap-table/dist/bootstrap-table";
// #import "bootstrap-table/dist/extenstions/filter-control/bootstrap-table-filter-control";
$body-color: slateblue;
body {
color: $body-color;
font-family: sans-serif;
}

The problem of Callback must be a function. Received undefined occurs when using parcel in a project directory that has a parent directory with an (unrelated) yarn.lock file.
To reproduce run touch ~/yarn.lock and then run parcel in a subdirectory of ~/.
To fix the issue delete any yarn.lock files in parent directories.

I had a problem with running the parcel, probably due to a node.js update, I updated the parcel and installed #parcel/transformer-sass there: https://www.npmjs.com/package/#parcel/transformer-sass.
After that, the parcel works fine.

Related

Bad autocomplete in eslint-config-react-app: 'react/cjs/react.development'

Background
For a very simple ReactJS project, I wanted to
add ESLint capabilities :
npm install --save-dev eslint-config-react-app eslint#^8.0.0
package.json after installing ESLint :
{
"name": "reactjs-app",
"scripts": {
"dev": "next dev"
},
"dependencies": {
"next": "^12.1.4",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"devDependencies": {
"eslint": "^8.12.0",
"eslint-config-react-app": "7.0.0"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
}
}
In the above package.json, none of the three dependencies next,
react, react-dom, depends on any ESLint package – neither
directly nor indirectly.
So all installed ESLint packages are dependencies of
eslint-config-react-app.
All the files needed for the project are in a zip file
available for download.
To try it out, just download, unpack and run npm install.
1
index.js :
// index.js
import { useState } from 'react';
function HomePage() {
const [showParagraph, setShowParagraph] = useState(true);
const showParagraphHandler = useCallback(() => {
setShowParagraph((prevToggle) => !prevToggle);
}, []);
console.log('App RUNNING');
console.log(showParagraph);
return (
<div className='app'>
<h1>Hi there!</h1>
<button onClick={showParagraphHandler}>A button</button>
</div>
);
}
export default HomePage;
The question
An observant reader will notice that the import for useCallback is
missing.
But autocompletion (in VS Code Ctrl + space)
wrongly suggests to import from
react/cjs/react.development, or from
react/cjs/react.production.min,
instead of from react which would have been more correct.
Why does this happen? – Is there a bug fix?
^ click to enlarge
References
README for eslint-config-react-app
All the project files in a zip file
Suggested solutions for the bug in this question
1
For me, the npm install command took about 5 minutes to complete.
The npm install command downloads and installs all packages in
package.json – including indirect packages.
Running npm run dev from the command line should start the
application in your default web browser.
Why does this happen? – Is there a bug fix?
It seems the reason is that #types/react is a missing dependency in
eslint-config-react-app so the obvious bug fix is to add
#types/react manually to your project by running :
npm install #types/react --save-dev
VS Code's autocompletion through Ctrl + space
now correctly suggests react.
1
Installing #types/react adds "#types/react": "^18.0.0", in your
package.json under "dependencies" :
{
"name": "reactjs-app",
"scripts": {
"dev": "next dev"
},
"dependencies": {
"#types/react": "^18.0.0",
"next": "^12.1.4",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"devDependencies": {
"eslint": "^8.12.0",
"eslint-config-react-app": "7.0.0"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
}
}
1
If it doesn't work, try restarting VS Code.

Node js + TypeSc + Babel "Error: Cannot find module 'koa'"

I have cloned a demo . after installing koa,koa-router etc, I got an error. Here is my index.ts file
import Koa from "koa"
import Router from "koa-router"
import logger from "koa-logger"
import json from "koa-json"
const app = new Koa()
const router = new Router()
router.get("/",async(ctx: any,next: any)=>{
ctx.body = {
meg:"Hello world"
}
await next
})
app.use(logger())
app.use(json())
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000,()=>console.log("app is running at 3000"))
Here is my package.json
{
"name": "babel-typescript-sample",
"version": "0.7.2",
"license": "MIT",
"scripts": {
"type-check": "tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch",
"build": "npm run build:types && npm run build:js",
"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline"
},
"devDependencies": {
"#babel/cli": "^7.8.3",
"#babel/core": "^7.8.3",
"#babel/plugin-proposal-class-properties": "^7.8.3",
"#babel/preset-env": "^7.8.3",
"#babel/preset-typescript": "^7.8.3",
"#types/koa": "^2.11.3",
"typescript": "^3.7.5"
},
"dependencies": {
"koa": "^2.11.0",
"koa-bodyparser": "^4.3.0",
"koa-json": "^2.0.2",
"koa-logger": "^3.2.1",
"koa-router": "^8.0.8"
}
}
my file structure loos like:
/-------
-lib
-index.js
-index.d.ts
-node_modules
-src
-index.ts
Actually,before this ,when I run npm run build,I got error src/index.ts:2:20 - error TS2307: Cannot find module 'koa-router'. I write koa-router.d.ts myself, but I don't think it's a great idea, How do you guys resolve?
You have a few options:
Install koa-router typings with npm install --save-dev #types/koa-router.
Set moduleResolution inside tsconfig.json to node. See this for differences. Note that this option only works if the dependency has typings included. In this case, it does not, so first option would be more correct.
Edit paths (points to directory) or types (points to .d.ts) inside tsconfig.json to point to your typings. Yes, writing your typings ("by hand") for existing dependency is generally considered a bad idea, since it could update and therefore break your typings. If a dependency does not have typings, you can contribute to it by generating it using JSDoc, if it uses JavaScript.

Unable to run a node.js file with #babel/preset-env

I'm trying to run a simple node.js file which needs the #babel/preset-env preset. The moment I run the js file, I get a message saying
Requires Babel “7.0.0-0” but was loaded with “6.26.3”
To replicate the issue, please try the following in a new folder:
1. Run the following commands
npm init
npm install #babel/register
npm install #babel/core#^7.2.2
npm install #babel/preset-env
Create a .babelrc file with the following
{
"presets": ["#babel/preset-env"],
"plugins": []
}
Create a sample emp.jsx with the following
import React from "react";
class CommentBox extends React.Component {}
Create a parse.js file with the following
require('babel-register')({presets: ['env', 'react']});
let Emp = require('./emp.jsx');
Now run the parse.js file by running
node parse.js
You should see the error mentioned above. I have been trying to fix with for a long time now. Please help.
Many Thanks
followed your instructions and using #babel/register instead with
this package.json run with no issues
tasted on
node : v8.11.2
yarn : 1.12.3
paese.json
require('#babel/register')({});
let Emp = require('./emp.jsx');
console.log(Emp)
packge.json
{
"name": "sof",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.2.2",
"#babel/preset-env": "^7.2.3",
"#babel/register": "^7.0.0"
},
"dependencies": {
"react": "^16.7.0"
}
}
Found the problem. The .babelrc file that contained a reference to #babel/preset-env. Removed it and the js file compiled just fine.

How can I use Node.js native import/export in version 9

I want to use Node native support for import/export with NPM packages. To start I'm running cuccumberjs version 3.1.0. However when I run this command:
/usr/local/bin/node --experimental-modules ./node_modules/.bin/cucumber-js
I get an error:
(node:42011) ExperimentalWarning: The ESM module loader is experimental.
SyntaxError: Unexpected token import
/full/path/to/test/directory/features/support/getWindowLocation.js:2
import {runJsInBrowser} from "./runJsInBrowser.mjs";
^^^^^^
All my automation test run fine until I try to convert one of my scripts to a ES module and import it. Here is an snippet from the file:
runJsInBrowsers.mjs
export default async function runJsInBrowser(
browserDriver,
windowFunc,
waitForReturn = true,
timeOut = 10000
) {
...
};
--
package.json
{
"name": "tests",
"version": "0.0.1",
"description": "Test for testing.",
"main": "index.js",
"license": "UNLICENSED",
"dependencies": {
"chromedriver": "^2.0",
"cucumber": "^3.1",
"selenium-webdriver": "^3.0"
},
"devDependencies": {
}
}

npm link not working on windows?

I'm using node v0.10.32. Windows 8.1.
My objective is to link a node application as a node_module in another main app.
I go to my-module folder and do
npm link
Then, I go to the main-app folder and do
npm link my-module
This is the result
c:\dev\main-app>npm link my-module
unbuild my-module#0.0.2
c:\dev\main-app\node_modules\my-module -> C:\Users\Nizar\AppData\Roaming\npm\node_modules\my-module -> C:\dev\my-module
But, the linkage does NOT seem to work, require('my-module') throws the following error
c:\dev\main-app>node app.js
module.js:340
throw err;
^
Error: Cannot find module 'my-module'
at Function.Module._resolveFilename (module.js:338:15)
my-module is indeed v0.0.2.
I can access it from main-app/node_module/my-module
This folder exists C:\Users\Nizar\AppData\Roaming\npm\node_modules\my-module
my-module package.json has "name": "my-module"
Moreover, %NODE_PATH% is correctly set:
c:\dev\main-app>echo %NODE_PATH%
C:\Users\Nizar\AppData\Roaming\npm\node_modules
Ideas?
There are a few things to try. On Windows, npm link is handled by creating junction points. Issuing a dir node_modules command should result in a line like:
01/15/2016 11:02 AM <JUNCTION> my-module [C:\Users\Nizar\AppData\Roaming\npm\node_modules\my-module]
Assuming that's there, then the problem is more than likely:
A lack of an index.js file (which is the default filename node uses to resolve modules)
You're using a different file than index.js as the main file of your module, in which case you need to tell node what that file is, by using the main key in your package.json file.
For example (taken from here):
{
"name": "node-js-sample",
"version": "0.2.0",
"description": "A sample Node.js app using Express 4",
"main": "index.js", // <-- LIKE THIS
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.13.3"
},
"engines": {
"node": "4.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/heroku/node-js-sample"
},
"keywords": [
"node",
"heroku",
"express"
],
"author": "Mark Pundsack",
"contributors": [
"Zeke Sikelianos <zeke#sikelianos.com> (http://zeke.sikelianos.com)"
],
"license": "MIT"
}

Resources