2
votes

I'm trying to publish a react library to npm, but in my local npm builds I get errors due to scss files. I'm using babel to transpile to es5 but the final files are still requiring scss files.

An example is that my original console.js file has the following import:

import './console.scss'

My transpiled console.js file that is causing the error contains the following:

require('./console.scss')

I get an error: Error: Cannot find module './console.scss'

I'm testing this by creating a local npm package. My process is:

npm run build
npm run build:babel
npm pack 

I then install the package in another project to test it, where I'm getting the errors.

This is my package.json:

 {
  "name": "",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "scripts": {
    "start": "nodemon server.js",
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "build-watch": "webpack -w",
    "build-babel": "babel src --presets babel-preset-es2015  --out-dir dist"
  },
  "author": "",
  "license": "ISC",
  "babel": {
    "presets": [
      "es2015",
      "react"
    ]
  },
  "dependencies": {
    "react": "^15.5.4",
    "react-dom": "^15.5.4"
  },
  "devDependencies": {
    "babel": "^6.23.0",
    "babel-cli": "^6.24.1",
    "babel-core": "^6.24.1",
    "babel-loader": "^6.4.1",
    "babel-plugin-css-modules-transform": "^1.2.7",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "css-loader": "^0.28.0",
    "express": "^4.15.2",
    "html-loader": "^0.4.5",
    "node-sass": "^4.5.2",
    "node-sass-loader": "^0.1.7",
    "nodemon": "^1.11.0",
    "path": "^0.12.7",
    "sass-loader": "^6.0.3",
    "style-loader": "^0.16.1",
    "webpack": "^2.3.3"
  }
}

This is my webpack config:

'use strict';

const webpack = require('webpack')
const path = require('path')

module.exports = {
    entry: "./src/indexLocal.js",
    output: {
        path: path.join(__dirname, 'src'),
        filename: 'bundle.js',
    },
    module: {
        loaders: [
        {
            test: /.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            query: {
                presets: ['react', 'es2015'],
            },
        },
        {
        test: /\.scss$/,
        loaders: ['style-loader', 'css-loader', 'sass-loader'],
        }
        ,{
            test: /.html$/,
            loader: 'html-loader',
            query: {
                minimize: true
            }
        }
        ]
    }
};

How do I ensure my npm package contains the original styling without running into errors due to importing scss files? Cheers

2
Is your npm package meant to publish the Webpack bundle, or a normal structured directory? You've got two separate build tools and it's not clear why both affect this question. If you're using babel's CLI to publish, then there is nothing there to compile the .scss files for Node. - loganfsmyth
My npm package is supposed to publish a normal structured dir. The webpack build is for local dev but I thought I'd include it in the question if anyone wanted to see. What would I use to compile the .scss files for Node? - Darkzuh

2 Answers

2
votes

When you run build-babel script it creates dist folder with your transpiled code but it doesn't move your sass files so you need another npm script to move your sass file to the dist folder.

0
votes

I'm running into that same problem, however I'm working on a highly configurable monorepo code base. You can either do as @Nomz said in his answer, or use this package https://www.npmjs.com/package/babel-plugin-sass-export

If you download that package, make sure to add the plugin to your babel configuration, they haven't mentioned that in their installation section (maybe it's too obvious?)

This package will handle the scss files for you, but make sure to read the warnings/issues on their NPM page, this shouldn't be used for production.

In my situation, I'll be using either Webpack to bundle things together, or figure out a solution using Gulp to handle the bundling.