How to deal with modules inside node_modules that depends .css files? - css

I'm working in a project where some of dependencies are a self made UI library components.
Those components has its proper .css file. The source structure is something like this:
|- src
|-|
|- components
|-|
| Component
|-|
|- index.js
|- Component.js
|- Component.css
The same structure is published in a build folder, with each .js file transpiled for a production compatible JavaScript format.
The project I consume this module vw using React-Starter-Kit and when I try to start the service using yarn start or npm start, I get the following error:
SyntaxError: Unexpected token .
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> ([... path to my component inside node_modules]:13:15)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
/pathtomyproject/runServer.js:67
throw new Error(`Server terminated unexpectedly with code: ${code} signal: ${signal}`);
I guess the problem is about babel trying to read .css files, but I would like to know if someone have a good approach to avoid this kind of problem, using the actual boilerplate I'm using right now (React Starter Kit).
UPDATE
I forgot to mention this. If I import that module using relative path sintax:
import Component, { Styles } from '../../../node_modules/#scope/my-project/Component';
It works as expected, obviously is nice to have syntatic sugar import
Thanks.

According to your answer, I think, that import has to be:
import Styles from './Component.css',
it's necessary to add .css extension to show that it's not .js and it has to be processed by css-loaders (in case if webpack is used) or other tools.

Related

Framer-motion import breaks page Next Js

Hi I am trying to import framer-motion into a page in Next Js.
import { motion } from "framer-motion"
This breaks the page. I get the following error in terminal:
/Users/.../Sites/.../node_modules/framer-motion/dist/es/index.js:1
export { MotionConfig, MotionConfigContext } from './context/MotionConfigContext.js';
^^^^^^
SyntaxError: Unexpected token 'export'
at wrapSafe (internal/modules/cjs/loader.js:931:16)
at Module._compile (internal/modules/cjs/loader.js:979:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
at Module.load (internal/modules/cjs/loader.js:879:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:903:19)
at require (internal/modules/cjs/helpers.js:74:18)
at eval (webpack-internal:///framer-motion:1:18)
at Object.framer-motion (/Users/.../Sites/.../.next/server/pages/work.js:446:1)
at __webpack_require__ (/Users/.../Sites/.../.next/server/pages/work.js:23:31)
I can't see any documentation on this problem anywhere.
Thanks,
I hit this same error. I noticed that Framer Motion (3.10.5) and Next (10.0.9) were both updated in the last 16 hours.
I ran npm update and it fixed this error for me but note that this command updates everything in your package.
If you are worried about breaking changes try updating just the two packages framer and next - npm update framer-motion next or yarn upgrade framer-motion next
I had the same issue, You just need to update your packages.
npm update

how to make react-native-paper work with next.js?

I installed this example https://github.com/zeit/next.js/tree/master/examples/with-react-native-web and I added react-native-paper to it , when ever I use it ,I get this error
Error: Cannot find module 'react-native'
Require stack:
- /Users/amesbahi/Code/with-react-native-web-app/node_modules/react-native-paper/lib/commonjs/styles/fonts.js
- /Users/amesbahi/Code/with-react-native-web-app/node_modules/react-native-paper/lib/commonjs/styles/DefaultTheme.js
- /Users/amesbahi/Code/with-react-native-web-app/node_modules/react-native-paper/lib/commonjs/core/theming.js
- /Users/amesbahi/Code/with-react-native-web-app/node_modules/react-native-paper/lib/commonjs/index.js
- /Users/amesbahi/Code/with-react-native-web-app/.next/server/static/development/pages/index.js
- /Users/amesbahi/Code/with-react-native-web-app/node_modules/next/dist/next-server/server/require.js
- /Users/amesbahi/Code/with-react-native-web-app/node_modules/next/dist/next-server/server/load-components.js
- /Users/amesbahi/Code/with-react-native-web-app/node_modules/next/dist/next-server/server/api-utils.js
- /Users/amesbahi/Code/with-react-native-web-app/node_modules/next/dist/next-server/server/next-server.js
- /Users/amesbahi/Code/with-react-native-web-app/node_modules/next/dist/server/next.js
- /Users/amesbahi/Code/with-react-native-web-app/node_modules/next/dist/server/lib/start-server.js
- /Users/amesbahi/Code/with-react-native-web-app/node_modules/next/dist/cli/next-dev.js
- /Users/amesbahi/Code/with-react-native-web-app/node_modules/next/dist/bin/next
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:954:17)
at Function.Module._load (internal/modules/cjs/loader.js:847:27)
at Module.require (internal/modules/cjs/loader.js:1016:19)
at require (internal/modules/cjs/helpers.js:69:18)
at Object.<anonymous> (/Users/amesbahi/Code/with-react-native-web-app/node_modules/react-native-paper/lib/commonjs/styles/fonts.js:1:279)
at Module._compile (internal/modules/cjs/loader.js:1121:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
at Module.load (internal/modules/cjs/loader.js:976:32)
at Function.Module._load (internal/modules/cjs/loader.js:884:14)
at Module.require (internal/modules/cjs/loader.js:1016:19)
at require (internal/modules/cjs/helpers.js:69:18)
at Object.<anonymous> (/Users/amesbahi/Code/with-react-native-web-app/node_modules/react-native-paper/lib/commonjs/styles/DefaultTheme.js:1:282)
at Module._compile (internal/modules/cjs/loader.js:1121:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
at Module.load (internal/modules/cjs/loader.js:976:32)
at Function.Module._load (internal/modules/cjs/loader.js:884:14)
here is a copy of my repo enter link description here
I've been struggling with this for a couple of days. Hope some one can help me
I had same approach and same issue as yours.
Here is how I made React Native Paper work in Next.js.
Build Next.js and React Native for Web(RNW) with Expo.io
Check this tutorial and make sure it's running on your local
Follow the Getting Started part and Using on the Web in React Native Paper Doc, however, regarding to next.config.js, this is all you need to do.
const { withExpo } = require("#expo/next-adapter");
const withImages = require("next-images");
const withFonts = require("next-fonts");
module.exports = withExpo(
withFonts(
withImages({
projectRoot: __dirname
})
)
);
Since Expo already provides these build-in plugin to load images and fonts, you don't need to manually add any loader for your webpack, just using it and it should work.

firebase functions:shell can't find custom functions configuration variables

I am trying to test my cloud functions locally. I have a custom functions configuration variable, so I used the command firebase functions:config:get > .runtimeconfig.json to generate a runtimeconfig.json file:
{
"stripe": {
"testkey": "someKey"
}
}
Then I use the command firebase functions:shell to start the emulator, but it prints the following error in my terminal:
+ functions: Emulator started at http://localhost:5001
! TypeError: Cannot read property 'testkey' of undefined
at Object.<anonymous> (C:\Users\Jesper\intergun\functions\lib\index.js:14:52)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at C:\Users\Jesper\AppData\Roaming\nvm\v8.16.0\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:532:33
at Generator.next (<anonymous>)
! We were unable to load your functions code. (see above)
- It appears your code is written in Typescript, which must be compiled before emulation.
- You may be able to run "npm run build" in your functions directory to resolve this.
For some reason it cannot find the variable, and it also seems like it thinks the code is written in TypeScript, even though it is pointing to the generated index.js file. I tried running npm run build but the result is the same.
The top of my index.ts looks like this:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import 'firebase-functions';
import * as Stripe from 'stripe';
admin.initializeApp();
const stripe = new Stripe(functions.config().stripe.testkey);
What am I missing?
I found the solution here: https://github.com/firebase/firebase-tools/issues/711
To generate .runtimeconfig.json, I used the command firebase functions:config:get > .runtimeconfig.json. However, this generated a file which seemed to be correct but apparently had invisible characters that shouldn't be there. Instead, I used this command: firebase functions:config:get | ac .runtimeconfig.json. This solved the problem.

import css from node_modules in meteor 1.4 with react

I want to use an npm package, namely react-date-picker in my Meteor 1.4 project using React. The react-date-picker package comes with some css that must be included in order to render correctly. I need to tell meteor to load node_modules/react-date-picker/index.css and include it with the rest of its css, but I'm at a loss on how to do this. Others have suggested that you can simply import the css from within the .jsx component like this
import 'react-date-picker/index.css'
but doing so results in a crashing server that chokes on the first line of the css (as it appears to be parsed as a standard javascript file)
(function (exports, require, module, __filename, __dirname) { .react-date-field {
^
) SyntaxError: Unexpected token .
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Module.Mp.load (/Users/gsferrer/.meteor/packages/babel-compiler/.6.9.1.7f3rvr++os+web.browser+web.cordova/npm/node_modules/reify/node/runtime.js:16:23)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at npmRequire (/Users/gsferrer/Projects/xxx/.meteor/local/build/programs/server/npm-require.js:129:10)
at Module.Mp.useNode (packages/modules-runtime/modules-runtime.js:69:1)
=> Exited with code: 1
=> Your application is crashing. Waiting for file change.
So what is the preferred method to import css from node_modules using Meteor with React?
As you say, it appears to be parsed as JS. However, importing a CSS this way is correct and usually works, so there must be something weird going on... So I'm going to try a wild guess: Try renaming the file from index.css to something else, like react-date-picker.css. It could be that the "index" name is triggering the JS interpretation.
I have been stuck for days on this issue too (I am new to the node.js/Meteor/React/JavaScript world). I needed to import a CSS file in node_modules/react-datagrid/index.css
My working solution (applied to a tiny application that should make it easy to understand; I only display a datagrid using hard-coded data) is available on my GitHub repository (commit 902c92c). The application is called "reactDataGrid", it is a subfolder of the repository, you just need to run "meteor" within that folder.
The key steps in my case were:
npm install webpack --save-dev to install web pack for the project
to create a .babelrc file with the content described here (and copied below). I believe this helps webpack parse the JSX syntax of react.
.babelrc
{
"presets": [
[
"es2015",
{
"modules": false
}
],
"react"
]
}
add require('react-datagrid/index.css') statement inside my client/main.jsx file, that tells webpack to include that CSS file in the dependency tree of my app. This is actually an instruction described in the README of the zippyui/react-datagrid GitHub repository. (Unfortunately stackoverflow limits to two links in my post, so I cannot link it here anymore)
I cannot guarantee that this is the preferred method of doing it, but I can say that it works for me. I hope it helps.
Best,
Kevin

Meteor - error after upgrading to 1.4

I was working in a project in Meteor 1.3 and today I upgraded to 1.4 and when i try to run the project after running meteor reset I get the following error:
While loading plugin `compileScss` from package `fourseven:scss`:
/Users/mazinalmaskari/.meteor/packages/fourseven_scss/.2.1.1.xb5zda++os.osx.x86_64+web.browser+web.cordova/plugin.compileScss.os.osx.x86_64/npm/compileScss/node_modules/node-sass/lib/index.js:22:11:
`libsass` bindings not found. Try reinstalling `node-sass`?
at getBinding
(/Users/mazinalmaskari/.meteor/packages/fourseven_scss/.2.1.1.xb5zda++os.osx.x86_64+web.browser+web.cordova/plugin.compileScss.os.osx.x86_64/npm/compileScss/node_modules/node-sass/lib/index.js:22:11)
at Object.<anonymous>
(/Users/mazinalmaskari/.meteor/packages/fourseven_scss/.2.1.1.xb5zda++os.osx.x86_64+web.browser+web.cordova/plugin.compileScss.os.osx.x86_64/npm/compileScss/node_modules/node-sass/lib/index.js:188:23)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load
(/Users/mazinalmaskari/.meteor/packages/meteorhacks_npm/.1.5.0.m71zaz++os+web.browser+web.cordova/plugin.initializing-npm-support.os/npm/initializing-npm-support/node_modules/node-echo/node_modules/coffee-script/lib/coffee-script/register.js:45:36)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous>
(/Users/mazinalmaskari/.meteor/packages/meteor-tool/.1.4.0-1.bf0k83++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/tools/isobuild/bundler.js:1633:22)
at Object.require
(/Users/mazinalmaskari/.meteor/packages/meteor-tool/.1.4.0-1.bf0k83++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/tools/isobuild/bundler.js:1607:31)
at <runJavaScript-18>:15:16
at <runJavaScript-18>:151:4
at <runJavaScript-18>:158:3
=> Your application has errors. Waiting for file change.
And when i remove the package fourseven:scss i get the following error:
While processing files with stylus (for target web.browser):
packages/ian:accounts-ui-bootstrap-3/accounts_ui.styl: Stylus compiler error: accounts_ui.styl:85:2
81| }
82| .btn-Twitter{
83| color: white;
84| background-color: #4099FF;
85| }
--------^
86|
87|
fs.exists is not a function
=> Your application has errors. Waiting for file change.
Any advice on what should i do here please? is there a way to downgrade to meteor 1.4?
I got the problem fixed by executing the following steps:
meteor remove fourseven:scss
meteor add fourseven:scss#=3.8.1'
for the second problem, I has to move stylus package and add it again because for some reason downloading ian:accounts-ui-bootstrap-3 doesn't come with the latest version of stylus
meteor remove stylus
meteor add stylus#=2.513.4
First try running a meteor reset (warning: this will wipe your local Mongo DB).
If that doesn't work, with the fourseven:scss package added to your project, try:
cd yourproject
npm uninstall node-sass
meteor npm install --save-dev node-sass
Try running your app as root
$ sudo meteor

Resources