I published an npm package today and wanted to try it on CodeSandBox. It works if I npm install package-name in a local machine but importing same dependency on a CodeSandBox projects gives this error:
package.json
{
"name": "package-name",
"version": "1.0.20",
"private": false,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.4.1",
"#testing-library/user-event": "^7.2.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.4.0"
},
"scripts": {
"start": "react-scripts start",
"build": "NODE_ENV=production babel src/lib --out-dir dist --copy-files --ignore __tests__,spec.js,test.js,__snapshots__",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"keywords": [
"react"
],
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"main": "dist/index.js",
"module": "dist/index.js",
"files": [
"dist",
"README.md"
],
"repository": {
"type": "git",
},
"devDependencies": {
"#babel/cli": "^7.8.4",
"#babel/core": "^7.8.4",
"#babel/runtime": "^7.8.4"
}
}
My compiled files have these kind of imports at top:
import _defineProperty from "#babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "#babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "#babel/runtime/helpers/esm/slicedToArray";
I followed this tutorial. After I run npm run build, a dist folder creates in the main directory. Then I npm publish. I did try npm install package-name on a local machine and it works but as I said it does not work on CodeSandBox.
Try adding your devDependencies to dependencies. devDependencies are not exported in build and that is why maybe your babel/runtime is failing.
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.4.1",
"#testing-library/user-event": "^7.2.1",
"#babel/cli": "^7.8.4",
"#babel/core": "^7.8.4",
"#babel/runtime": "^7.8.4",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.4.0"
}
and then build and try.
Another way without needing to merge your devDependencies and dependencies is to set up your Codesandbox project in a Container Environment. Note that you have to set this before creation — as of writing you cannot switch between Container and Client environments after the fact.
In addition, if you are going to be importing a project into Codesandbox from Github, it implicitly sets the environment to Client. To workaround this, create a sandbox.config.json file at the root of your project with the following contents:
{
"template": "node"
}
Make sure this file exists before import. Creating it after the fact will not work.
Related
I'm trying to set up eslint for a basic React project (Javascript only, no Typescript). When linting my project with npx eslint ./, I get the following error:
ESLint: 8.31.0
Error: Failed to load plugin 'import' declared in '.eslintrc.js »
eslint-config-airbnb » /Users/me/project/node_modules/eslint-config-airbnb-base/index.js »
/Users/me/project/node_modules/eslint-config-airbnb-base/rules/imports.js':
Cannot find module 'typescript/package.json'
I'm not really sure what's going on here, as my project doesn't use any Typescript. Some additional information that might help:
I followed this tutorial to set up eslint, prettier, and editorconfig for working in a team environment.
Here is my .eslintrc.js file:
module.exports = {
"env": {
"browser": true,
"es2021": true
},
"extends": [
"plugin:react/recommended",
"airbnb",
'prettier'
],
"overrides": [
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
}
}
I do have a .eslintignore file in the root directory of my project that contains node_modules.
Here is my package.json:
{
"name": "project",
"version": "0.1.0",
"private": true,
"dependencies": {
"#emotion/react": "^11.10.5",
"#emotion/styled": "^11.10.5",
"#fontsource/roboto": "^4.5.8",
"#mui/material": "^5.11.4",
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"axios": "^1.2.2",
"firebase": "^9.15.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-firebaseui": "^6.0.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "npx eslint ./",
"format": "npx prettier --write ."
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"eslint": "^8.31.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.0",
"eslint-plugin-jsx-a11y": "^6.7.0",
"eslint-plugin-react": "^7.32.0",
"eslint-plugin-react-hooks": "^4.6.0",
"prettier": "2.8.2"
}
}
Finally here is my file tree:
.vscode
node_modules
public
src
.editorconfig
.eslintignore
.eslintrc.js
.gitignore
.prettierignore
.prettierrc.json
package-lock.json
package.json
README.md
I found a somewhat questionable solution by installing typescript related packages for eslint:
npm install typescript #typescript-eslint/parser #typescript-eslint/eslint-plugin --save-dev
After installing these packages, npx eslint ./ works fine, but I don't think I should need to install typescript-related plugins/parsers for eslint when my project doesn't use typescript and I set eslint to ignore the node_modules folder. I'm also unsure how installing these packages might affect the linting behavior (I didn't make any changes to .eslintrc.js or any other config files after installing those packages).
Does anyone know of a better solution that doesn't involve installing these additional packages? Or are these packages actually necessary despite my project not using Typescript?
Thank you in advance for the help!
I'm trying to create a Desktop App which is using create-nw-react-app and Sqlite.
Since trying to compile native modules with NW.js is a nightmare, I'm trying to start a server with express before the frontend loads using node-main feature in package.json.
But when calling nw-react-scripts start, I found that the node-main script is not executed. If I open a separate terminal running the express server it works.
How can I tell NW to start the server before starting react application?
my package.json
{
"name": "ultimo",
"node-main": "api/server.js",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"#types/jest": "^26.0.15",
"#types/node": "^12.0.0",
"#types/nw.js": "^0.13.13",
"#types/react": "^17.0.0",
"#types/react-dom": "^17.0.0",
"better-sqlite3": "^8.0.1",
"body-parser": "^1.20.1",
"cors": "^2.8.5",
"express": "^4.18.2",
"express-session": "^1.17.3",
"nw-react-scripts": "4.0.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"typescript": "^4.1.2",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "nw-react-scripts start",
"build": "nw-react-scripts build",
"test": "nw-react-scripts test",
"eject": "nw-react-scripts eject"
},
"eslintConfig": {
"extends": [
"nw-react-app",
"nw-react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"main": "index.html",
"nwBuilder": {
"//": "https://github.com/nwjs-community/nw-builder",
"platforms": [
"osx64",
"win32",
"win64"
],
"version": "latest",
"flavor": "normal",
"buildDir": "./build",
"cacheDir": "./cache",
"macIcns": "./src/logo.icns"
},
"devDependencies": {
"concurrently": "^7.6.0",
"wait-on": "^7.0.1"
}
}
I have tried to modify to script start into concurrently \"node api/server.js\" \"nw-react-scripts start\" and it works, but it's only for development.
You haven't provided enough information. This works just fine with NW.js 0.71.0:
package.json:
{
"name": "test",
"main": "index.html",
"node-main": "node-main.js",
}
node-main.js:
console.log('hello world');
index.html:
Test
Run the app, inspect background page, note that it shows "hello world" in the console. If yours doesn't work, there's a bug in api/server.js.
Edit: This could be caused by nw-react-scripts. Since create-nw-react-app doesn't seem to be maintained anymore, you may want to look into other options. You can use the following as an initial template: https://github.com/nwutils/nw-react-example
When running npm run build on a project where I've changed something on its package.json, produces the exact same output as if I hadn't done any changes after running npm run build again.
I'm trying to upload the build folder to Pinata and it's producing the same hash (CID), so both folders (pre and post modification) are exactly the same.
What could be happening here?
This is my package.json file:
{
"name": "react-template",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.2.0",
"#testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-copy-to-clipboard": "^5.1.0",
"react-dom": "^18.1.0",
"react-icons": "^4.6.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"react-toastify": "^9.0.8",
"sass": "^1.51.0",
"uuid": "^9.0.0",
"web-vitals": "^2.1.4",
"web3": "^1.8.1",
"tailwindcss": "^3.1.8"
},
"scripts": {
"start": "PORT=3001 react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"homepage":"./"
}
Thanks!
npm run build just runs react-scripts build.
If you updated your dependencies, you need to npm install or npm update them first.
I am new to creating the npm package.
I have created my first react npm package using the following blog - https://www.codementor.io/#peterodekwo/create-a-simple-react-npm-package-in-simple-steps-using-cra-w966okagi
and it was published successfully and work fine in react as expected.
So what's the problem.
When I install my npm package and implement it. I have to use the following code to import.
import Card from 'owaiz-test/dist/Card';
import Button from 'owaiz-test/dist/Button';
https://prnt.sc/1cpkvsy
The problem with the dist folder. I don't want to use the dist folder to be written.
I want something like
import Card from 'owaiz-test/Card';
import Button from 'owaiz-test/Button';
I know it's compiling the code from my package.json file mentioned below.
"publish:npm": "rm -rf dist && mkdir dist && babel ./src/components -d dist --copy-files"
But, How can I make changes in the code above or any other solution?
Update:
ADD Package.json
{
"name": "owaiz-test",
"version": "0.1.1",
"private": false,
"babel": {
"presets": [
"#babel/preset-react"
]
},
"dependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"publish:npm": "rm -rf dist && mkdir dist && babel ./src/components -d dist --copy-files"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/cli": "^7.5.5",
"#babel/preset-react": "^7.0.0"
}
}
Update 2
{
"name": "owaiz-test",
"version": "0.1.1",
"private": false,
"main": "dist/index.cjs.js",
"browser": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"babel": {
"presets": [
"#babel/preset-react"
]
},
"peerDependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"publish:npm": "rm -rf dist && mkdir dist && babel ./src/index.js -d dist --copy-files"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/cli": "^7.5.5",
"#babel/preset-react": "^7.0.0"
}
}
You need to edit package.json
Create index.js file as a main entry for your package
import Card from './components/Card'
import Button from './components/Button'
export { Card, Button }
Use index.js during bundling:
"publish:npm": "...babel ./src/index.js ..."
Set main and browser fields(module for esm packages can skip, types for TypeScript definitions):
"main": "dist/index.cjs.js",
"browser": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
Docs
Also need to add react and react-dom to peerDependencies not to dependencies, so it will not be bundled with your code What is the correct way of adding a dependency to react in your package.json for a react component
UPD
Article from question is total crap, those are better:
https://blog.logrocket.com/the-complete-guide-to-publishing-a-react-package-to-npm/
https://dev.to/jimjunior/how-to-create-an-npm-library-from-react-components-2m2
But still can be some caveats.
I'm running into a huge number of vulnerabilities. There are 583 vulnerabilities all associated with the package minimist
My package.json is as such:
{
"name": "weather-wizard",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:5000",
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.4.1",
"#testing-library/user-event": "^7.2.1",
"axios": "^0.19.2",
"chart.js": "^2.9.3",
"eslint-plugin-flowtype": "^3.13.0",
"minimist": "^1.2.5",
"moment": "^2.24.0",
"node-sass": "^4.13.1",
"react": "^16.13.0",
"react-animated-weather": "^4.0.0",
"react-chartjs-2": "^2.9.0",
"react-dom": "^16.13.0",
"react-places-autocomplete": "^7.2.1",
"react-scripts": "3.4.0",
"typescript": "^3.8.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
What is the best way to manage these vulnerabilities?
When you see some issue like this, you need to first check on the github repository if they are already notified and create an issue so they can fix it soon.
They are taking care of this in the following issue: https://github.com/facebook/create-react-app/issues/8672
Solution:
For npm users:
npm install minimist --save-dev
eg: (minimist version: 1.2.5)
Add Resolution key adjacent to dependency key into package.json file
{
"resolutions": {
"minimist": "^1.2.5"
}
}
Add below line inside script key into package.json
example:
"scripts": {
"preinstall": "npx npm-force-resolutions"
}
Remove node_modules, and then run command: npm install.
While npm audit fix fixes dependency